@ax-llm/ax 19.0.8 → 19.0.10

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,21 +1,21 @@
1
- "use strict";var Bu=Object.create;var Lr=Object.defineProperty;var ju=Object.getOwnPropertyDescriptor;var qu=Object.getOwnPropertyNames;var zu=Object.getPrototypeOf,Hu=Object.prototype.hasOwnProperty;var Ku=(i,e)=>{for(var t in e)Lr(i,t,{get:e[t],enumerable:!0})},Ui=(i,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of qu(e))!Hu.call(i,r)&&r!==t&&Lr(i,r,{get:()=>e[r],enumerable:!(n=ju(e,r))||n.enumerable});return i};var Kn=(i,e,t)=>(t=i!=null?Bu(zu(i)):{},Ui(e||!i||!i.__esModule?Lr(t,"default",{value:i,enumerable:!0}):t,i)),Wu=i=>Ui(Lr({},"__esModule",{value:!0}),i);var ud={};Ku(ud,{AxACE:()=>Yo,AxACEOptimizedProgram:()=>Or,AxAI:()=>dr,AxAIAnthropic:()=>Vt,AxAIAnthropicModel:()=>Jn,AxAIAnthropicVertexModel:()=>Qn,AxAIAzureOpenAI:()=>Zt,AxAICohere:()=>en,AxAICohereEmbedModel:()=>er,AxAICohereModel:()=>Zn,AxAIDeepSeek:()=>tn,AxAIDeepSeekModel:()=>tr,AxAIGoogleGemini:()=>nn,AxAIGoogleGeminiEmbedModel:()=>Zr,AxAIGoogleGeminiEmbedTypes:()=>$s,AxAIGoogleGeminiModel:()=>nr,AxAIGoogleGeminiSafetyCategory:()=>eo,AxAIGoogleGeminiSafetyThreshold:()=>to,AxAIGrok:()=>hn,AxAIGrokEmbedModels:()=>Qs,AxAIGrokModel:()=>pr,AxAIGroq:()=>on,AxAIGroqModel:()=>rr,AxAIHuggingFace:()=>sn,AxAIHuggingFaceModel:()=>io,AxAIMistral:()=>an,AxAIMistralEmbedModels:()=>js,AxAIMistralModel:()=>or,AxAIOllama:()=>ln,AxAIOpenAI:()=>Xt,AxAIOpenAIBase:()=>ke,AxAIOpenAIEmbedModel:()=>Jt,AxAIOpenAIModel:()=>Xn,AxAIOpenAIResponses:()=>cn,AxAIOpenAIResponsesBase:()=>sr,AxAIOpenAIResponsesImpl:()=>un,AxAIOpenAIResponsesModel:()=>Qt,AxAIOpenRouter:()=>pn,AxAIRefusalError:()=>Se,AxAIReka:()=>dn,AxAIRekaModel:()=>ar,AxAIServiceAbortedError:()=>Fe,AxAIServiceAuthenticationError:()=>St,AxAIServiceError:()=>He,AxAIServiceNetworkError:()=>De,AxAIServiceResponseError:()=>Rt,AxAIServiceStatusError:()=>Qe,AxAIServiceStreamTerminatedError:()=>ot,AxAIServiceTimeoutError:()=>nt,AxAITogether:()=>mn,AxAITogetherModel:()=>ur,AxAIWebLLM:()=>gn,AxAIWebLLMModel:()=>cr,AxAgent:()=>_r,AxApacheTika:()=>Bo,AxAssertionError:()=>At,AxBalancer:()=>Vr,AxBaseAI:()=>Ee,AxBaseOptimizer:()=>Ze,AxBootstrapFewShot:()=>Ln,AxContentProcessingError:()=>dt,AxDB:()=>Io,AxDBBase:()=>it,AxDBCloudflare:()=>fn,AxDBManager:()=>Co,AxDBMemory:()=>Ot,AxDBPinecone:()=>xn,AxDBWeaviate:()=>An,AxDefaultCostTracker:()=>Rr,AxDefaultResultReranker:()=>Uo,AxDockerSession:()=>rs,AxEmbeddingAdapter:()=>os,AxEvalUtil:()=>Zl,AxFlow:()=>Er,AxFlowDependencyAnalyzer:()=>$n,AxFlowExecutionPlanner:()=>Dn,AxFlowSubContextImpl:()=>jn,AxFlowTypedSubContextImpl:()=>ts,AxFluentFieldType:()=>$e,AxFunctionError:()=>Ar,AxFunctionProcessor:()=>br,AxGEPA:()=>_n,AxGEPAFlow:()=>Xo,AxGen:()=>ve,AxGenerateError:()=>Tr,AxHFDataLoader:()=>Qo,AxInstanceRegistry:()=>Rn,AxJSRuntime:()=>zt,AxJSRuntimePermission:()=>$i,AxJudge:()=>Pn,AxLLMRequestTypeValues:()=>bs,AxLearn:()=>Jo,AxMCPClient:()=>as,AxMCPHTTPSSETransport:()=>cs,AxMCPStreambleHTTPTransport:()=>us,AxMediaNotSupportedError:()=>st,AxMemory:()=>yn,AxMiPRO:()=>es,AxMockAIService:()=>uo,AxMultiServiceRouter:()=>co,AxOptimizedProgramImpl:()=>It,AxProgram:()=>Pt,AxPromptTemplate:()=>Ut,AxProviderRouter:()=>ho,AxRateLimiterTokenUsage:()=>rn,AxSignature:()=>Oe,AxSignatureBuilder:()=>Ir,AxSimpleClassifier:()=>qo,AxSimpleClassifierClass:()=>jo,AxSpanKindValues:()=>Is,AxStepContextImpl:()=>On,AxStopFunctionCallException:()=>$t,AxStringUtil:()=>Do,AxSynth:()=>Nn,AxTestPrompt:()=>zo,AxTokenLimitError:()=>Wn,AxTraceLogger:()=>Fn,agent:()=>_u,ai:()=>Xa,ax:()=>We,axAIAnthropicDefaultConfig:()=>Ms,axAIAnthropicVertexDefaultConfig:()=>Ra,axAIAzureOpenAIBestConfig:()=>Oa,axAIAzureOpenAICreativeConfig:()=>va,axAIAzureOpenAIDefaultConfig:()=>Ps,axAIAzureOpenAIFastConfig:()=>ka,axAICohereCreativeConfig:()=>_a,axAICohereDefaultConfig:()=>Ns,axAIDeepSeekCodeConfig:()=>Na,axAIDeepSeekDefaultConfig:()=>Ls,axAIGoogleGeminiDefaultConfig:()=>Ds,axAIGoogleGeminiDefaultCreativeConfig:()=>Da,axAIGrokBestConfig:()=>Ya,axAIGrokDefaultConfig:()=>yo,axAIHuggingFaceCreativeConfig:()=>Ua,axAIHuggingFaceDefaultConfig:()=>Bs,axAIMistralBestConfig:()=>Ba,axAIMistralDefaultConfig:()=>lo,axAIOllamaDefaultConfig:()=>qs,axAIOllamaDefaultCreativeConfig:()=>ja,axAIOpenAIBestConfig:()=>Hr,axAIOpenAICreativeConfig:()=>Kr,axAIOpenAIDefaultConfig:()=>_t,axAIOpenAIFastConfig:()=>Wr,axAIOpenAIResponsesBestConfig:()=>qa,axAIOpenAIResponsesCreativeConfig:()=>za,axAIOpenAIResponsesDefaultConfig:()=>ir,axAIOpenRouterDefaultConfig:()=>Hs,axAIRekaBestConfig:()=>Wa,axAIRekaCreativeConfig:()=>Va,axAIRekaDefaultConfig:()=>lr,axAIRekaFastConfig:()=>Ja,axAITogetherDefaultConfig:()=>Ws,axAIWebLLMCreativeConfig:()=>Qa,axAIWebLLMDefaultConfig:()=>Js,axAnalyzeChatPromptRequirements:()=>Ha,axAnalyzeRequestRequirements:()=>Nt,axBaseAIDefaultConfig:()=>be,axBaseAIDefaultCreativeConfig:()=>Le,axBuildActorDefinition:()=>ps,axBuildResponderDefinition:()=>ds,axCheckMetricsHealth:()=>Za,axCreateDefaultColorLogger:()=>ys,axCreateDefaultOptimizerColorLogger:()=>wi,axCreateDefaultOptimizerTextLogger:()=>nu,axCreateDefaultTextLogger:()=>Vi,axCreateFlowColorLogger:()=>Mr,axCreateFlowTextLogger:()=>cu,axCreateJSRuntime:()=>yu,axDefaultFlowLogger:()=>pu,axDefaultMetricsConfig:()=>ei,axDefaultOptimizerLogger:()=>wr,axDefaultOptimizerMetricsConfig:()=>Ri,axGetCompatibilityReport:()=>Ma,axGetFormatCompatibility:()=>Pa,axGetMetricsConfig:()=>tl,axGetOptimizerMetricsConfig:()=>ou,axGetProvidersWithMediaSupport:()=>Ea,axGlobals:()=>ce,axModelInfoAnthropic:()=>Yn,axModelInfoCohere:()=>Yr,axModelInfoDeepSeek:()=>Xr,axModelInfoGoogleGemini:()=>no,axModelInfoGrok:()=>Ao,axModelInfoGroq:()=>oo,axModelInfoHuggingFace:()=>so,axModelInfoMistral:()=>ao,axModelInfoOpenAI:()=>Yt,axModelInfoOpenAIResponses:()=>zr,axModelInfoReka:()=>go,axModelInfoTogether:()=>fo,axModelInfoWebLLM:()=>xo,axProcessContentForProvider:()=>mo,axRAG:()=>Nu,axScoreProvidersForRequest:()=>Jr,axSelectOptimalProvider:()=>Qr,axSpanAttributes:()=>me,axSpanEvents:()=>mt,axUpdateMetricsConfig:()=>el,axUpdateOptimizerMetricsConfig:()=>ru,axValidateChatRequestMessage:()=>Kt,axValidateChatResponseResult:()=>qr,axValidateProviderCapabilities:()=>Fs,axWorkerRuntime:()=>ss,f:()=>ge,flow:()=>ns,s:()=>su});module.exports=Wu(ud);function rt({model:i,modelInfo:e,models:t}){let n=t?.find(u=>u.key===i),r=n&&"model"in n?n.model:i,o=e.find(u=>u.name===i);if(o)return o;let s=e.find(u=>u.name===r);if(s)return s;let a=r.replace(/^(anthropic\.|openai\.)/,"").replace(/-latest$/,"").replace(/-\d{8}$/,"").replace(/-v\d+:\d+$/,"").replace(/@\d{8}$/,"").replace(/-\d{2,}(-[a-zA-Z0-9-]+)?$/,"").replace(/-v\d+@\d{8}$/,"").replace(/-v\d+$/,""),l=e.find(u=>u.name===a);return l||null}var fs=(()=>{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 ze(){return fs.randomUUID()}async function Vu(i){let e=new TextEncoder,t=typeof i=="string"?e.encode(i):i,n=await fs.subtle.digest("SHA-256",t);return Array.from(new Uint8Array(n)).map(s=>s.toString(16).padStart(2,"0")).join("")}var hs=class{data="";update(e){return this.data+=e,this}digest(e){if(e!=="hex")throw new Error("Only hex encoding is supported");let n=new TextEncoder().encode(this.data),r=0;for(let o=0;o<n.length;o++){let s=n[o];r=(r<<5)-r+s,r=r&r}return Math.abs(r).toString(16).padStart(8,"0")}async digestAsync(){return Vu(this.data)}};function tt(i){if(i!=="sha256")throw new Error("Only SHA-256 algorithm is supported");return new hs}function Bi(){return fs}var $r=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 qu=Object.create;var Lr=Object.defineProperty;var zu=Object.getOwnPropertyDescriptor;var Hu=Object.getOwnPropertyNames;var Ku=Object.getPrototypeOf,Wu=Object.prototype.hasOwnProperty;var Vu=(i,e)=>{for(var t in e)Lr(i,t,{get:e[t],enumerable:!0})},Ui=(i,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Hu(e))!Wu.call(i,r)&&r!==t&&Lr(i,r,{get:()=>e[r],enumerable:!(n=zu(e,r))||n.enumerable});return i};var Hn=(i,e,t)=>(t=i!=null?qu(Ku(i)):{},Ui(e||!i||!i.__esModule?Lr(t,"default",{value:i,enumerable:!0}):t,i)),Ju=i=>Ui(Lr({},"__esModule",{value:!0}),i);var ud={};Vu(ud,{AxACE:()=>Yo,AxACEOptimizedProgram:()=>kr,AxAI:()=>pr,AxAIAnthropic:()=>Wt,AxAIAnthropicModel:()=>Vn,AxAIAnthropicVertexModel:()=>Jn,AxAIAzureOpenAI:()=>Xt,AxAICohere:()=>Zt,AxAICohereEmbedModel:()=>Zn,AxAICohereModel:()=>Xn,AxAIDeepSeek:()=>en,AxAIDeepSeekModel:()=>er,AxAIGoogleGemini:()=>tn,AxAIGoogleGeminiEmbedModel:()=>Zr,AxAIGoogleGeminiEmbedTypes:()=>$s,AxAIGoogleGeminiModel:()=>tr,AxAIGoogleGeminiSafetyCategory:()=>eo,AxAIGoogleGeminiSafetyThreshold:()=>to,AxAIGrok:()=>gn,AxAIGrokEmbedModels:()=>Qs,AxAIGrokModel:()=>cr,AxAIGroq:()=>rn,AxAIGroqModel:()=>nr,AxAIHuggingFace:()=>on,AxAIHuggingFaceModel:()=>io,AxAIMistral:()=>sn,AxAIMistralEmbedModels:()=>js,AxAIMistralModel:()=>rr,AxAIOllama:()=>an,AxAIOpenAI:()=>Yt,AxAIOpenAIBase:()=>ke,AxAIOpenAIEmbedModel:()=>Vt,AxAIOpenAIModel:()=>Yn,AxAIOpenAIResponses:()=>un,AxAIOpenAIResponsesBase:()=>or,AxAIOpenAIResponsesImpl:()=>ln,AxAIOpenAIResponsesModel:()=>Jt,AxAIOpenRouter:()=>cn,AxAIRefusalError:()=>Re,AxAIReka:()=>pn,AxAIRekaModel:()=>ir,AxAIServiceAbortedError:()=>Fe,AxAIServiceAuthenticationError:()=>St,AxAIServiceError:()=>je,AxAIServiceNetworkError:()=>De,AxAIServiceResponseError:()=>Rt,AxAIServiceStatusError:()=>Ve,AxAIServiceStreamTerminatedError:()=>rt,AxAIServiceTimeoutError:()=>tt,AxAITogether:()=>dn,AxAITogetherModel:()=>lr,AxAIWebLLM:()=>mn,AxAIWebLLMModel:()=>ur,AxAgent:()=>Fr,AxApacheTika:()=>Bo,AxAssertionError:()=>yt,AxBalancer:()=>Vr,AxBaseAI:()=>Me,AxBaseOptimizer:()=>Ye,AxBootstrapFewShot:()=>Nn,AxContentProcessingError:()=>dt,AxDB:()=>Io,AxDBBase:()=>st,AxDBCloudflare:()=>hn,AxDBManager:()=>Co,AxDBMemory:()=>Ot,AxDBPinecone:()=>fn,AxDBWeaviate:()=>xn,AxDefaultCostTracker:()=>wr,AxDefaultResultReranker:()=>Uo,AxDockerSession:()=>rs,AxEmbeddingAdapter:()=>os,AxEvalUtil:()=>Zl,AxFlow:()=>Mr,AxFlowDependencyAnalyzer:()=>Ln,AxFlowExecutionPlanner:()=>Gn,AxFlowSubContextImpl:()=>Bn,AxFlowTypedSubContextImpl:()=>ts,AxFluentFieldType:()=>Le,AxFunctionError:()=>xr,AxFunctionProcessor:()=>yr,AxGEPA:()=>Fn,AxGEPAFlow:()=>Xo,AxGen:()=>Se,AxGenerateError:()=>Cr,AxHFDataLoader:()=>Qo,AxInstanceRegistry:()=>wn,AxJSRuntime:()=>zt,AxJSRuntimePermission:()=>$i,AxJudge:()=>En,AxLLMRequestTypeValues:()=>bs,AxLearn:()=>Jo,AxMCPClient:()=>as,AxMCPHTTPSSETransport:()=>cs,AxMCPStreambleHTTPTransport:()=>us,AxMediaNotSupportedError:()=>ot,AxMemory:()=>An,AxMiPRO:()=>es,AxMockAIService:()=>uo,AxMultiServiceRouter:()=>co,AxOptimizedProgramImpl:()=>Ct,AxProgram:()=>Pt,AxPromptTemplate:()=>Ut,AxProviderRouter:()=>ho,AxRateLimiterTokenUsage:()=>nn,AxSignature:()=>Oe,AxSignatureBuilder:()=>br,AxSimpleClassifier:()=>qo,AxSimpleClassifierClass:()=>jo,AxSpanKindValues:()=>Is,AxStepContextImpl:()=>kn,AxStopFunctionCallException:()=>$t,AxStringUtil:()=>Do,AxSynth:()=>_n,AxTestPrompt:()=>zo,AxTokenLimitError:()=>Kn,AxTraceLogger:()=>Pn,agent:()=>Lu,ai:()=>Xa,ax:()=>ze,axAIAnthropicDefaultConfig:()=>Ms,axAIAnthropicVertexDefaultConfig:()=>Ra,axAIAzureOpenAIBestConfig:()=>Oa,axAIAzureOpenAICreativeConfig:()=>va,axAIAzureOpenAIDefaultConfig:()=>Ps,axAIAzureOpenAIFastConfig:()=>ka,axAICohereCreativeConfig:()=>_a,axAICohereDefaultConfig:()=>Ns,axAIDeepSeekCodeConfig:()=>Na,axAIDeepSeekDefaultConfig:()=>Ls,axAIGoogleGeminiDefaultConfig:()=>Ds,axAIGoogleGeminiDefaultCreativeConfig:()=>Da,axAIGrokBestConfig:()=>Ya,axAIGrokDefaultConfig:()=>yo,axAIHuggingFaceCreativeConfig:()=>Ua,axAIHuggingFaceDefaultConfig:()=>Bs,axAIMistralBestConfig:()=>Ba,axAIMistralDefaultConfig:()=>lo,axAIOllamaDefaultConfig:()=>qs,axAIOllamaDefaultCreativeConfig:()=>ja,axAIOpenAIBestConfig:()=>Hr,axAIOpenAICreativeConfig:()=>Kr,axAIOpenAIDefaultConfig:()=>_t,axAIOpenAIFastConfig:()=>Wr,axAIOpenAIResponsesBestConfig:()=>qa,axAIOpenAIResponsesCreativeConfig:()=>za,axAIOpenAIResponsesDefaultConfig:()=>sr,axAIOpenRouterDefaultConfig:()=>Hs,axAIRekaBestConfig:()=>Wa,axAIRekaCreativeConfig:()=>Va,axAIRekaDefaultConfig:()=>ar,axAIRekaFastConfig:()=>Ja,axAITogetherDefaultConfig:()=>Ws,axAIWebLLMCreativeConfig:()=>Qa,axAIWebLLMDefaultConfig:()=>Js,axAnalyzeChatPromptRequirements:()=>Ha,axAnalyzeRequestRequirements:()=>Nt,axBaseAIDefaultConfig:()=>be,axBaseAIDefaultCreativeConfig:()=>Ne,axBuildActorDefinition:()=>ps,axBuildResponderDefinition:()=>ds,axCheckMetricsHealth:()=>Za,axCreateDefaultColorLogger:()=>ys,axCreateDefaultOptimizerColorLogger:()=>wi,axCreateDefaultOptimizerTextLogger:()=>nu,axCreateDefaultTextLogger:()=>Vi,axCreateFlowColorLogger:()=>Or,axCreateFlowTextLogger:()=>cu,axCreateJSRuntime:()=>Iu,axDefaultFlowLogger:()=>pu,axDefaultMetricsConfig:()=>ei,axDefaultOptimizerLogger:()=>Tr,axDefaultOptimizerMetricsConfig:()=>Ri,axGetCompatibilityReport:()=>Ma,axGetFormatCompatibility:()=>Pa,axGetMetricsConfig:()=>tl,axGetOptimizerMetricsConfig:()=>ou,axGetProvidersWithMediaSupport:()=>Ea,axGlobals:()=>de,axModelInfoAnthropic:()=>Qn,axModelInfoCohere:()=>Yr,axModelInfoDeepSeek:()=>Xr,axModelInfoGoogleGemini:()=>no,axModelInfoGrok:()=>Ao,axModelInfoGroq:()=>oo,axModelInfoHuggingFace:()=>so,axModelInfoMistral:()=>ao,axModelInfoOpenAI:()=>Qt,axModelInfoOpenAIResponses:()=>zr,axModelInfoReka:()=>go,axModelInfoTogether:()=>fo,axModelInfoWebLLM:()=>xo,axProcessContentForProvider:()=>mo,axRAG:()=>$u,axScoreProvidersForRequest:()=>Jr,axSelectOptimalProvider:()=>Qr,axSpanAttributes:()=>ge,axSpanEvents:()=>mt,axUpdateMetricsConfig:()=>el,axUpdateOptimizerMetricsConfig:()=>ru,axValidateChatRequestMessage:()=>Ht,axValidateChatResponseResult:()=>qr,axValidateProviderCapabilities:()=>Fs,axWorkerRuntime:()=>ss,f:()=>fe,flow:()=>ns,s:()=>su});module.exports=Ju(ud);function nt({model:i,modelInfo:e,models:t}){let n=t?.find(u=>u.key===i),r=n&&"model"in n?n.model:i,o=e.find(u=>u.name===i);if(o)return o;let s=e.find(u=>u.name===r);if(s)return s;let a=r.replace(/^(anthropic\.|openai\.)/,"").replace(/-latest$/,"").replace(/-\d{8}$/,"").replace(/-v\d+:\d+$/,"").replace(/@\d{8}$/,"").replace(/-\d{2,}(-[a-zA-Z0-9-]+)?$/,"").replace(/-v\d+@\d{8}$/,"").replace(/-v\d+$/,""),l=e.find(u=>u.name===a);return l||null}var fs=(()=>{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 Be(){return fs.randomUUID()}async function Qu(i){let e=new TextEncoder,t=typeof i=="string"?e.encode(i):i,n=await fs.subtle.digest("SHA-256",t);return Array.from(new Uint8Array(n)).map(s=>s.toString(16).padStart(2,"0")).join("")}var hs=class{data="";update(e){return this.data+=e,this}digest(e){if(e!=="hex")throw new Error("Only hex encoding is supported");let n=new TextEncoder().encode(this.data),r=0;for(let o=0;o<n.length;o++){let s=n[o];r=(r<<5)-r+s,r=r&r}return Math.abs(r).toString(16).padStart(8,"0")}async digestAsync(){return Qu(this.data)}};function et(i){if(i!=="sha256")throw new Error("Only SHA-256 algorithm is supported");return new hs}function Bi(){return fs}var $r=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 xs=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)}},Gr=class extends TransformStream{constructor(){super(new xs)}};var Ju={maxRetries:3,initialDelayMs:1e3,maxDelayMs:6e4,backoffFactor:2,retryableStatusCodes:[500,408,429,502,503,504]},Qu=globalThis.TextDecoderStream??Gr,He=class extends Error{constructor(t,n,r,o,s={},a=!0){super(t);this.url=n;this.requestBody=r;this.responseBody=o;this.name="AxAIServiceError",this.timestamp=new Date().toISOString(),this.errorId=ze(),this.context=s,this.includeRequestBodyInErrors=a,this.stack=this.toString()}timestamp;errorId;context;includeRequestBodyInErrors;toString(){let t=[`${this.name}: ${this.message}`,`URL: ${this.url}`];return this.includeRequestBodyInErrors&&t.push(`Request Body: ${JSON.stringify(this.requestBody,null,2)}`),t.push(`Response Body: ${JSON.stringify(this.responseBody,null,2)}`,`Context: ${JSON.stringify(this.context,null,2)}`,`Timestamp: ${this.timestamp}`,`Error ID: ${this.errorId}`),t.join(`
8
- `)}[Symbol.for("nodejs.util.inspect.custom")](t,n){return this.toString()}},Qe=class extends He{constructor(t,n,r,o,s,a,l,u=!0){let c=l?` (after ${l} retries)`:"";super(`HTTP ${t} - ${n}${c}`,r,o,{httpStatus:t,httpStatusText:n,responseBody:s,...a},{},u);this.status=t;this.statusText=n;this.name="AxAIServiceStatusError"}},De=class extends He{constructor(t,n,r,o,s,a=!0){super(`Network Error: ${t.message}`,n,r,o,{originalErrorName:t.name,originalErrorStack:t.stack,...s},a);this.originalError=t;this.name="AxAIServiceNetworkError",this.stack=t.stack}},Rt=class extends He{constructor(e,t,n,r,o=!0){super(e,t,n,void 0,r,o),this.name="AxAIServiceResponseError"}},ot=class extends He{constructor(t,n,r,o,s=!0){super("Stream terminated unexpectedly by remote host",t,n,void 0,{lastChunk:r,...o},s);this.lastChunk=r;this.name="AxAIServiceStreamTerminatedError"}},nt=class extends He{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"}},Wn=class extends Qe{constructor(e,t,n,r,o,s,a=!0){super(e,t,n,r,o,s,void 0,a),this.name="AxTokenLimitError"}},Fe=class extends He{constructor(e,t,n,r,o=!0){super(`Request aborted${t?`: ${t}`:""}`,e,n,void 0,{abortReason:t,...r},o),this.name="AxAIServiceAbortedError"}},St=class extends He{constructor(e,t,n,r,o=!0){super("Authentication failed",e,t,n,r,o),this.name="AxAIServiceAuthenticationError"}},Se=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=ze()}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()}},st=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=ze()}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()}},dt=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=ze()}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 As(i){try{return i.headers.get("content-type")?.includes("application/json")?await i.json():await i.clone().text()}catch(e){return`[ReadableStream - read failed: ${e.message}]`}}function ji(i,e){return Math.min(e.maxDelayMs,e.initialDelayMs*e.backoffFactor**i)*(.75+Math.random()*.5)}function Yu(i){if(!i)return;let e=Number(i);if(!Number.isNaN(e))return e*1e3;let t=Date.parse(i);if(!Number.isNaN(t)){let n=t-Date.now();return Math.max(0,n)}}function Xu(){return{startTime:Date.now(),retryCount:0}}function qi(i){i.retryCount++,i.lastRetryTime=Date.now()}function zi(i,e,t,n){return t>=n.maxRetries?!1:e&&n.retryableStatusCodes.includes(e)?!0:i instanceof De&&!(i instanceof St)}var Ke=async(i,e)=>{if(i.localCall)return await i.localCall(e,i.stream);if(!i.url)throw new Error("API URL is required when localCall is not provided");let t={...Ju,...i.retry},n=i.timeout,r=Xu(),o=i.verbose??!1,s=i.includeRequestBodyInErrors??!0,a,l=new URL(i.url),u=`${[l.pathname,i.name].filter(Boolean).join("/").replace(/\/+/g,"/")}${l.search}`,c=new URL(u,l);if(i.corsProxy){let m=c.href;c=new URL(`${i.corsProxy}?url=${encodeURIComponent(m)}`)}let p=ze();if(i.validateRequest&&!await i.validateRequest(e))throw new Rt("Invalid request data",c.href,e,{validation:"request"},s);i.span?.setAttributes({"http.request.method":i.put?"PUT":"POST","url.full":c.href,"request.id":p,"request.startTime":r.startTime});let d=0;for(;;){let m=new AbortController;if(i.abortSignal){if(i.abortSignal.aborted)throw new Fe(c.href,i.abortSignal.reason,e,{metrics:r},s);let g=()=>{m.abort(i.abortSignal.reason||"User aborted request")};i.abortSignal.addEventListener("abort",g,{once:!0});let h=m.abort.bind(m);m.abort=f=>{i.abortSignal.removeEventListener("abort",g),h(f)}}n&&(a=setTimeout(()=>{m.abort("Request timeout")},n));try{o&&console.log(`
7
+ `:"")+r;break;case"id":this.currentEvent.id=r;break;case"retry":{let o=Number.parseInt(r,10);Number.isNaN(o)||(this.currentEvent.retry=o);break}}}processEvent(e){if(this.currentEvent.rawData){if(this.currentEvent.event||(this.currentEvent.event="message"),this.currentEvent.rawData.trim()==="[DONE]"){this.currentEvent={rawData:""};return}try{let t=this.dataParser(this.currentEvent.rawData);e.enqueue(t)}catch(t){this.onError(t,this.currentEvent.rawData)}this.currentEvent={rawData:""}}}};var xs=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)}},Gr=class extends TransformStream{constructor(){super(new xs)}};var Yu={maxRetries:3,initialDelayMs:1e3,maxDelayMs:6e4,backoffFactor:2,retryableStatusCodes:[500,408,429,502,503,504]},Xu=globalThis.TextDecoderStream??Gr,je=class extends Error{constructor(t,n,r,o,s={},a=!0){super(t);this.url=n;this.requestBody=r;this.responseBody=o;this.name="AxAIServiceError",this.timestamp=new Date().toISOString(),this.errorId=Be(),this.context=s,this.includeRequestBodyInErrors=a,this.stack=this.toString()}timestamp;errorId;context;includeRequestBodyInErrors;toString(){let t=[`${this.name}: ${this.message}`,`URL: ${this.url}`];return this.includeRequestBodyInErrors&&t.push(`Request Body: ${JSON.stringify(this.requestBody,null,2)}`),t.push(`Response Body: ${JSON.stringify(this.responseBody,null,2)}`,`Context: ${JSON.stringify(this.context,null,2)}`,`Timestamp: ${this.timestamp}`,`Error ID: ${this.errorId}`),t.join(`
8
+ `)}[Symbol.for("nodejs.util.inspect.custom")](t,n){return this.toString()}},Ve=class extends je{constructor(t,n,r,o,s,a,l,u=!0){let c=l?` (after ${l} retries)`:"";super(`HTTP ${t} - ${n}${c}`,r,o,{httpStatus:t,httpStatusText:n,responseBody:s,...a},{},u);this.status=t;this.statusText=n;this.name="AxAIServiceStatusError"}},De=class extends je{constructor(t,n,r,o,s,a=!0){super(`Network Error: ${t.message}`,n,r,o,{originalErrorName:t.name,originalErrorStack:t.stack,...s},a);this.originalError=t;this.name="AxAIServiceNetworkError",this.stack=t.stack}},Rt=class extends je{constructor(e,t,n,r,o=!0){super(e,t,n,void 0,r,o),this.name="AxAIServiceResponseError"}},rt=class extends je{constructor(t,n,r,o,s=!0){super("Stream terminated unexpectedly by remote host",t,n,void 0,{lastChunk:r,...o},s);this.lastChunk=r;this.name="AxAIServiceStreamTerminatedError"}},tt=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"}},Kn=class extends Ve{constructor(e,t,n,r,o,s,a=!0){super(e,t,n,r,o,s,void 0,a),this.name="AxTokenLimitError"}},Fe=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"}},St=class extends je{constructor(e,t,n,r,o=!0){super("Authentication failed",e,t,n,r,o),this.name="AxAIServiceAuthenticationError"}},Re=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=Be()}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()}},ot=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=Be()}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()}},dt=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=Be()}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 As(i){try{return i.headers.get("content-type")?.includes("application/json")?await i.json():await i.clone().text()}catch(e){return`[ReadableStream - read failed: ${e.message}]`}}function ji(i,e){return Math.min(e.maxDelayMs,e.initialDelayMs*e.backoffFactor**i)*(.75+Math.random()*.5)}function Zu(i){if(!i)return;let e=Number(i);if(!Number.isNaN(e))return e*1e3;let t=Date.parse(i);if(!Number.isNaN(t)){let n=t-Date.now();return Math.max(0,n)}}function ec(){return{startTime:Date.now(),retryCount:0}}function qi(i){i.retryCount++,i.lastRetryTime=Date.now()}function zi(i,e,t,n){return t>=n.maxRetries?!1:e&&n.retryableStatusCodes.includes(e)?!0:i instanceof De&&!(i instanceof St)}var qe=async(i,e)=>{if(i.localCall)return await i.localCall(e,i.stream);if(!i.url)throw new Error("API URL is required when localCall is not provided");let t={...Yu,...i.retry},n=i.timeout,r=ec(),o=i.verbose??!1,s=i.includeRequestBodyInErrors??!0,a,l=new URL(i.url),u=`${[l.pathname,i.name].filter(Boolean).join("/").replace(/\/+/g,"/")}${l.search}`,c=new URL(u,l);if(i.corsProxy){let m=c.href;c=new URL(`${i.corsProxy}?url=${encodeURIComponent(m)}`)}let p=Be();if(i.validateRequest&&!await i.validateRequest(e))throw new Rt("Invalid request data",c.href,e,{validation:"request"},s);i.span?.setAttributes({"http.request.method":i.put?"PUT":"POST","url.full":c.href,"request.id":p,"request.startTime":r.startTime});let d=0;for(;;){let m=new AbortController;if(i.abortSignal){if(i.abortSignal.aborted)throw new Fe(c.href,i.abortSignal.reason,e,{metrics:r},s);let g=()=>{m.abort(i.abortSignal.reason||"User aborted request")};i.abortSignal.addEventListener("abort",g,{once:!0});let h=m.abort.bind(m);m.abort=f=>{i.abortSignal.removeEventListener("abort",g),h(f)}}n&&(a=setTimeout(()=>{m.abort("Request timeout")},n));try{o&&console.log(`
12
12
  --- [AxAI API Request] ---
13
13
  `,`URL: ${c.href}
14
14
  `,`Method: ${i.put?"PUT":"POST"}
15
15
  `,"Headers:",JSON.stringify({"Content-Type":"application/json","X-Request-ID":p,"X-Retry-Count":d.toString(),...i.headers},null,2),`
16
16
  Body:`,JSON.stringify(e,null,2),`
17
17
  ------------------------
18
- `);let g=await(i.fetch??fetch)(c,{method:i.put?"PUT":"POST",headers:{"Content-Type":"application/json","X-Request-ID":p,"X-Retry-Count":d.toString(),...i.headers},body:JSON.stringify(e),signal:m.signal});if(a&&clearTimeout(a),g.status===401||g.status===403){let w=await As(g);throw new St(c.href,e,w,{metrics:r},s)}if(g.status===400){let w=await As(g),I=w,T=!1;if(I?.error?.code==="context_length_exceeded")T=!0;else if(I?.type==="invalid_request_error"&&(I?.error?.message?.includes("prompt is too long")||I?.error?.message?.includes("max_tokens")||I?.error?.message?.includes("token limit")))T=!0;else if(I?.error?.code===400&&I?.error?.status==="INVALID_ARGUMENT"&&(I?.error?.message?.includes("token")||I?.error?.message?.includes("limit")))T=!0;else{let v=JSON.stringify(w).toLowerCase();(v.includes("token")&&v.includes("limit")||v.includes("context length")||v.includes("prompt is too long"))&&(T=!0)}if(T)throw new Wn(g.status,g.statusText,c.href,e,w,{metrics:r},s)}if(g.status>=400&&zi(new Error,g.status,d,t)){let w=ji(d,t),I=Yu(g.headers.get("Retry-After"));I!==void 0&&I<=t.maxDelayMs&&(w=I,o&&console.log(`[AxAI] Respecting Retry-After header: ${w}ms`)),d++,qi(r),i.span?.addEvent("retry",{attempt:d,delay:w,status:g.status,"metrics.startTime":r.startTime,"metrics.retryCount":r.retryCount,"metrics.lastRetryTime":r.lastRetryTime}),await new Promise(T=>setTimeout(T,w));continue}if(g.status>=400){let w=await As(g);throw new Qe(g.status,g.statusText,c.href,e,w,{metrics:r},d>0?d:void 0,s)}if(!i.stream){let w=await g.json();if(o&&console.log(`
18
+ `);let g=await(i.fetch??fetch)(c,{method:i.put?"PUT":"POST",headers:{"Content-Type":"application/json","X-Request-ID":p,"X-Retry-Count":d.toString(),...i.headers},body:JSON.stringify(e),signal:m.signal});if(a&&clearTimeout(a),g.status===401||g.status===403){let w=await As(g);throw new St(c.href,e,w,{metrics:r},s)}if(g.status===400){let w=await As(g),b=w,T=!1;if(b?.error?.code==="context_length_exceeded")T=!0;else if(b?.type==="invalid_request_error"&&(b?.error?.message?.includes("prompt is too long")||b?.error?.message?.includes("max_tokens")||b?.error?.message?.includes("token limit")))T=!0;else if(b?.error?.code===400&&b?.error?.status==="INVALID_ARGUMENT"&&(b?.error?.message?.includes("token")||b?.error?.message?.includes("limit")))T=!0;else{let v=JSON.stringify(w).toLowerCase();(v.includes("token")&&v.includes("limit")||v.includes("context length")||v.includes("prompt is too long"))&&(T=!0)}if(T)throw new Kn(g.status,g.statusText,c.href,e,w,{metrics:r},s)}if(g.status>=400&&zi(new Error,g.status,d,t)){let w=ji(d,t),b=Zu(g.headers.get("Retry-After"));b!==void 0&&b<=t.maxDelayMs&&(w=b,o&&console.log(`[AxAI] Respecting Retry-After header: ${w}ms`)),d++,qi(r),i.span?.addEvent("retry",{attempt:d,delay:w,status:g.status,"metrics.startTime":r.startTime,"metrics.retryCount":r.retryCount,"metrics.lastRetryTime":r.lastRetryTime}),await new Promise(T=>setTimeout(T,w));continue}if(g.status>=400){let w=await As(g);throw new Ve(g.status,g.statusText,c.href,e,w,{metrics:r},d>0?d:void 0,s)}if(!i.stream){let w=await g.json();if(o&&console.log(`
19
19
  --- [AxAI API Response] ---
20
20
  `,`Status: ${g.status} ${g.statusText}
21
21
  `,"Body:",JSON.stringify(w,null,2),`
@@ -25,10 +25,10 @@ Body:`,JSON.stringify(e,null,2),`
25
25
  `,`Status: ${g.status} ${g.statusText}
26
26
  `,`
27
27
  -------------------------------------------
28
- `),!g.body)throw new Rt("Response body is null",c.href,e,{metrics:r},s);let h,f=0;if(typeof window<"u"&&typeof EventSource<"u")return new ReadableStream({start(w){let I=g.body.getReader(),T=new TextDecoder,v="";async function F(){try{for(;;){let{done:E,value:D}=await I.read();if(E){A=!0,w.close();break}v+=T.decode(D,{stream:!0});let U=v.split(`
28
+ `),!g.body)throw new Rt("Response body is null",c.href,e,{metrics:r},s);let h,f=0;if(typeof window<"u"&&typeof EventSource<"u")return new ReadableStream({start(w){let b=g.body.getReader(),T=new TextDecoder,v="";async function F(){try{for(;;){let{done:O,value:D}=await b.read();if(O){A=!0,w.close();break}v+=T.decode(D,{stream:!0});let B=v.split(`
29
29
 
30
- `);v=U.pop()||"";for(let V of U){if(!V.trim())continue;let k=V.split(`
31
- `),$="",N="message";for(let Q of k)Q.startsWith("data: ")?$=Q.slice(6):Q.startsWith("event: ")&&(N=Q.slice(7));if($){if($==="[DONE]"){w.close();return}try{let Q=JSON.parse($);h=Q,f++,r.streamChunks=f,r.lastChunkTime=Date.now(),w.enqueue(Q),i.span?.addEvent("stream.chunk",{"stream.chunks":f,"stream.duration":Date.now()-r.startTime,"response.retries":r.retryCount,"sse.event.type":N})}catch(Q){o&&console.warn("Skipping non-JSON SSE data:",$,Q)}}}}}catch(E){let D=E,U={...r,streamDuration:Date.now()-r.startTime};D.name==="AbortError"||D.message?.includes("aborted")?w.error(new ot(c.href,e,h,{streamMetrics:U},s)):w.error(new De(D,c.href,e,"[ReadableStream - consumed during streaming]",{streamMetrics:U},s))}finally{I.releaseLock()}}F()}});let x=new TransformStream({transform(w,I){h=w,f++,r.streamChunks=f,r.lastChunkTime=Date.now(),I.enqueue(w),i.span?.addEvent("stream.chunk",{"stream.chunks":f,"stream.duration":Date.now()-r.startTime,"response.retries":r.retryCount})}}),A=!1;return new ReadableStream({start(w){let I=g.body.pipeThrough(new Qu).pipeThrough(new $r).pipeThrough(x).getReader();async function T(){try{for(;;){let{done:v,value:F}=await I.read();if(v){A||(A=!0,w.close());break}if(A)break;w.enqueue(F)}}catch(v){let F=v,E={...r,streamDuration:Date.now()-r.startTime};throw F.name==="AbortError"||F.message?.includes("aborted")?w.error(new ot(c.href,e,h,{streamMetrics:E},s)):F instanceof TypeError&&F.message.includes("cancelled")?w.error(new ot(c.href,e,h,{streamMetrics:E,cancelReason:"Stream cancelled by client"},s)):w.error(new De(F,c.href,e,"[ReadableStream - consumed during streaming]",{streamMetrics:E},s)),F}finally{a&&clearTimeout(a),I.releaseLock()}}T()},cancel(){A=!0}})}catch(g){if(g instanceof Error&&g.name==="AbortError")throw i.abortSignal?.aborted?new Fe(c.href,i.abortSignal.reason,e,{metrics:r},s):new nt(c.href,n||0,e,{metrics:r},s);let h=g;if(!(g instanceof He)&&g instanceof Error&&(h=new De(g,c.href,e,void 0,{metrics:r},s)),i.span?.isRecording()&&(i.span.recordException(h),i.span.setAttributes({"error.time":Date.now()-r.startTime,"error.retries":r.retryCount})),h instanceof De&&zi(h,void 0,d,t)){let f=ji(d,t);d++,qi(r),i.span?.addEvent("retry",{attempt:d,delay:f,error:h.message,"metrics.startTime":r.startTime,"metrics.retryCount":r.retryCount,"metrics.lastRetryTime":r.lastRetryTime}),await new Promise(b=>setTimeout(b,f));continue}throw h instanceof He&&(h.context.metrics=r),h}finally{a!==void 0&&clearTimeout(a)}}};var Wt=require("@opentelemetry/api");var ce={signatureStrict:!0,useStructuredPrompt:!0,tracer:void 0,meter:void 0,logger:void 0,optimizerLogger:void 0,debug:void 0,abortSignal:void 0,customLabels:void 0,cachingFunction:void 0,functionResultFormatter:i=>typeof i=="string"?i:i==null?"":JSON.stringify(i,null,2)};var Ye=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 Td=new Ye,Hi=i=>{console.log(i)},Ki=(i,e,t)=>{let n=(r,o)=>t&&o&&o in t?t[o](r):r;switch(i.role){case"system":return`${n("[ SYSTEM ]","magentaBright")}
30
+ `);v=B.pop()||"";for(let Q of B){if(!Q.trim())continue;let S=Q.split(`
31
+ `),G="",N="message";for(let Y of S)Y.startsWith("data: ")?G=Y.slice(6):Y.startsWith("event: ")&&(N=Y.slice(7));if(G){if(G==="[DONE]"){w.close();return}try{let Y=JSON.parse(G);h=Y,f++,r.streamChunks=f,r.lastChunkTime=Date.now(),w.enqueue(Y),i.span?.addEvent("stream.chunk",{"stream.chunks":f,"stream.duration":Date.now()-r.startTime,"response.retries":r.retryCount,"sse.event.type":N})}catch(Y){o&&console.warn("Skipping non-JSON SSE data:",G,Y)}}}}}catch(O){let D=O,B={...r,streamDuration:Date.now()-r.startTime};D.name==="AbortError"||D.message?.includes("aborted")?w.error(new rt(c.href,e,h,{streamMetrics:B},s)):w.error(new De(D,c.href,e,"[ReadableStream - consumed during streaming]",{streamMetrics:B},s))}finally{b.releaseLock()}}F()}});let x=new TransformStream({transform(w,b){h=w,f++,r.streamChunks=f,r.lastChunkTime=Date.now(),b.enqueue(w),i.span?.addEvent("stream.chunk",{"stream.chunks":f,"stream.duration":Date.now()-r.startTime,"response.retries":r.retryCount})}}),A=!1;return new ReadableStream({start(w){let b=g.body.pipeThrough(new Xu).pipeThrough(new $r).pipeThrough(x).getReader();async function T(){try{for(;;){let{done:v,value:F}=await b.read();if(v){A||(A=!0,w.close());break}if(A)break;w.enqueue(F)}}catch(v){let F=v,O={...r,streamDuration:Date.now()-r.startTime};throw F.name==="AbortError"||F.message?.includes("aborted")?w.error(new rt(c.href,e,h,{streamMetrics:O},s)):F instanceof TypeError&&F.message.includes("cancelled")?w.error(new rt(c.href,e,h,{streamMetrics:O,cancelReason:"Stream cancelled by client"},s)):w.error(new De(F,c.href,e,"[ReadableStream - consumed during streaming]",{streamMetrics:O},s)),F}finally{a&&clearTimeout(a),b.releaseLock()}}T()},cancel(){A=!0}})}catch(g){if(g instanceof Error&&g.name==="AbortError")throw i.abortSignal?.aborted?new Fe(c.href,i.abortSignal.reason,e,{metrics:r},s):new tt(c.href,n||0,e,{metrics:r},s);let h=g;if(!(g instanceof je)&&g instanceof Error&&(h=new De(g,c.href,e,void 0,{metrics:r},s)),i.span?.isRecording()&&(i.span.recordException(h),i.span.setAttributes({"error.time":Date.now()-r.startTime,"error.retries":r.retryCount})),h instanceof De&&zi(h,void 0,d,t)){let f=ji(d,t);d++,qi(r),i.span?.addEvent("retry",{attempt:d,delay:f,error:h.message,"metrics.startTime":r.startTime,"metrics.retryCount":r.retryCount,"metrics.lastRetryTime":r.lastRetryTime}),await new Promise(I=>setTimeout(I,f));continue}throw h instanceof je&&(h.context.metrics=r),h}finally{a!==void 0&&clearTimeout(a)}}};var Kt=require("@opentelemetry/api");var de={signatureStrict:!0,useStructuredPrompt:!0,tracer:void 0,meter:void 0,logger:void 0,optimizerLogger:void 0,debug:void 0,abortSignal:void 0,customLabels:void 0,cachingFunction:void 0,functionResultFormatter:i=>typeof i=="string"?i:i==null?"":JSON.stringify(i,null,2)};var Je=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 Td=new Je,Hi=i=>{console.log(i)},Ki=(i,e,t)=>{let n=(r,o)=>t&&o&&o in t?t[o](r):r;switch(i.role){case"system":return`${n("[ SYSTEM ]","magentaBright")}
32
32
  ${n(i.content,"magenta")}`;case"function":return`${n("[ FUNCTION RESULT ]","yellow")}
33
33
  ${n(i.result??"[No result]","yellowDim")}`;case"user":{let r=`${n("[ USER ]","greenBright")}
34
34
  `;if(typeof i.content=="string")return r+n(i.content,"green");let o=i.content.map(s=>{if(s.type==="text")return n(s.text,"green");if(s.type==="image"){let a=e?"[Image]":`[Image: ${s.image}]`;return n(a,"green")}if(s.type==="audio"){let a=e?"[Audio]":`[Audio: ${s.data}]`;return n(a,"green")}return n("[Unknown content type]","gray")});return r+o.join(`
@@ -38,7 +38,7 @@ ${n(i.result??"[No result]","yellowDim")}`;case"user":{let r=`${n("[ USER ]","gr
38
38
  `,i.functionCalls.forEach((s,a)=>{let l=typeof s.function.params=="string"?s.function.params:JSON.stringify(s.function.params,null,2);o+=n(`${a+1}. ${s.function.name}(${l}) [id: ${s.id}]`,"yellowDim"),a<(i.functionCalls?.length??0)-1&&(o+=`
39
39
  `)}),o+=`
40
40
  `),!i.content&&(!i.functionCalls||i.functionCalls.length===0)&&(o+=n("[No content]","gray")),o}default:return`${n("[ UNKNOWN ]","redBright")}
41
- ${n(JSON.stringify(i),"gray")}`}},ys=(i=Hi)=>{let e=new Ye,t=e.gray(`${"\u2500".repeat(60)}
41
+ ${n(JSON.stringify(i),"gray")}`}},ys=(i=Hi)=>{let e=new Je,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}
@@ -164,23 +164,23 @@ ${e}
164
164
  `,n.value.forEach(o=>{r+=`- ${o.title||o.url}
165
165
  `,o.description&&(r+=` ${o.description}
166
166
  `)}),r+=`${e}
167
- `;break}default:r=JSON.stringify(n,null,2)}i(r)}};var me={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"},mt={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"},bs=(r=>(r.COMPLETION="completion",r.CHAT="chat",r.RERANK="rerank",r.UNKNOWN="unknown",r))(bs||{}),Is=(o=>(o.WORKFLOW="workflow",o.TASK="task",o.AGENT="agent",o.TOOL="tool",o.UNKNOWN="unknown",o))(Is||{});var Cs=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()}},Dr=class extends TransformStream{constructor(e,t){super(new Cs(e,t))}};function Ur(i,e){for(let t of e){let n=i.find(r=>r.id===t.id);n?(typeof t.function.name=="string"&&t.function.name.length>0&&(n.function.name+=t.function.name),typeof t.function.params=="string"&&t.function.params.length>0&&(n.function.params+=t.function.params),typeof t.function.params=="object"&&(n.function.params=t.function.params)):i.push(t)}}var Ji=(i,e,t,n)=>{let r=n?i.filter(s=>s.role!=="system"):[...i];t({name:"ChatRequestChatPrompt",step:e,value:r})};var Qi=(i,e)=>{if(!i.results)return;let t={name:"ChatResponseResults",value:i.results};e(t)};function Yi(i,e){let t=new Map;for(let n of i)for(let r of n.results){if(!r)continue;let o=t.get(r.index);o?(r.content&&(o.content=(o.content??"")+r.content),r.thought&&(o.thought=(o.thought??"")+r.thought),r.finishReason&&(o.finishReason=r.finishReason),r.functionCalls&&(o.functionCalls?Ur(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 Xi=(i,e)=>{e({name:"FunctionResults",value:i})},Ts=(i,e,t,n)=>{n({name:"FunctionError",index:e,fixingInstructions:t,error:i})},Zi=(i,e,t,n)=>{n({name:"ValidationError",index:e,fixingInstructions:t,error:i})},ea=(i,e,t,n)=>{n({name:"AssertionError",index:e,fixingInstructions:t,error:i})},ta=(i,e,t)=>{t({name:"RefusalError",index:e,error:i})};var na=(i,e,t)=>{t({name:"EmbedRequest",embedModel:e,value:i})},ra=(i,e)=>{let t=i.slice(0,3).map(r=>({length:r.length,sample:r.slice(0,5),truncated:r.length>5})),n={name:"EmbedResponse",totalEmbeddings:i.length,value:t};e(n)},oa=(i,e,t,n)=>{n({name:"ResultPickerUsed",sampleCount:i,selectedIndex:e,latency:t})};var jr=i=>{let e={};for(let[t,n]of Object.entries(i))if(n!=null){let r=String(n);e[t]=r.length>100?r.substring(0,100):r}return e},Ft=(...i)=>{let e={};for(let t of i)t&&Object.assign(e,t);return e},Br,sa=i=>{if(Br)return Br;if(i)return Br=Zu(i),Br};var Zu=i=>({latencyHistogram:i.createHistogram("ax_llm_request_duration_ms",{description:"Duration of LLM requests in milliseconds",unit:"ms"}),errorCounter:i.createCounter("ax_llm_errors_total",{description:"Total number of LLM request errors"}),requestCounter:i.createCounter("ax_llm_requests_total",{description:"Total number of LLM requests"}),tokenCounter:i.createCounter("ax_llm_tokens_total",{description:"Total number of LLM tokens consumed"}),inputTokenCounter:i.createCounter("ax_llm_input_tokens_total",{description:"Total number of input/prompt tokens consumed"}),outputTokenCounter:i.createCounter("ax_llm_output_tokens_total",{description:"Total number of output/completion tokens generated"}),errorRateGauge:i.createGauge("ax_llm_error_rate",{description:"Current error rate as a percentage (0-100)"}),meanLatencyGauge:i.createGauge("ax_llm_mean_latency_ms",{description:"Mean latency of LLM requests in milliseconds",unit:"ms"}),p95LatencyGauge:i.createGauge("ax_llm_p95_latency_ms",{description:"95th percentile latency of LLM requests in milliseconds",unit:"ms"}),p99LatencyGauge:i.createGauge("ax_llm_p99_latency_ms",{description:"99th percentile latency of LLM requests in milliseconds",unit:"ms"}),streamingRequestsCounter:i.createCounter("ax_llm_streaming_requests_total",{description:"Total number of streaming LLM requests"}),functionCallsCounter:i.createCounter("ax_llm_function_calls_total",{description:"Total number of function/tool calls made"}),functionCallLatencyHistogram:i.createHistogram("ax_llm_function_call_latency_ms",{description:"Latency of function calls in milliseconds",unit:"ms"}),requestSizeHistogram:i.createHistogram("ax_llm_request_size_bytes",{description:"Size of LLM request payloads in bytes",unit:"By"}),responseSizeHistogram:i.createHistogram("ax_llm_response_size_bytes",{description:"Size of LLM response payloads in bytes",unit:"By"}),temperatureGauge:i.createGauge("ax_llm_temperature_gauge",{description:"Temperature setting used for LLM requests"}),maxTokensGauge:i.createGauge("ax_llm_max_tokens_gauge",{description:"Maximum tokens setting used for LLM requests"}),estimatedCostCounter:i.createCounter("ax_llm_estimated_cost_total",{description:"Estimated cost of LLM requests in USD",unit:"$"}),promptLengthHistogram:i.createHistogram("ax_llm_prompt_length_chars",{description:"Length of prompts in characters"}),contextWindowUsageGauge:i.createGauge("ax_llm_context_window_usage_ratio",{description:"Context window utilization ratio (0-1)"}),timeoutsCounter:i.createCounter("ax_llm_timeouts_total",{description:"Total number of timed out LLM requests"}),abortsCounter:i.createCounter("ax_llm_aborts_total",{description:"Total number of aborted LLM requests"}),thinkingBudgetUsageCounter:i.createCounter("ax_llm_thinking_budget_usage_total",{description:"Total thinking budget tokens used"}),multimodalRequestsCounter:i.createCounter("ax_llm_multimodal_requests_total",{description:"Total number of multimodal requests (with images/audio)"}),cacheReadTokensCounter:i.createCounter("ax_llm_cache_read_tokens_total",{description:"Total number of tokens read from cache (prompt caching)"}),cacheWriteTokensCounter:i.createCounter("ax_llm_cache_write_tokens_total",{description:"Total number of tokens written to cache (prompt caching)"})}),ia=(i,e,t,n,r,o)=>{try{if(i.latencyHistogram){let s=jr({operation:e,ai_service:n,...r?{model:r}:{},...o});i.latencyHistogram.record(t,s)}}catch(s){console.warn("Failed to record latency metric:",s)}},aa=(i,e,t,n,r,o,s,a)=>{let l={operation:e,ai_service:o,...s?{model:s}:{},...a};i.meanLatencyGauge&&i.meanLatencyGauge.record(t,l),i.p95LatencyGauge&&i.p95LatencyGauge.record(n,l),i.p99LatencyGauge&&i.p99LatencyGauge.record(r,l)},la=(i,e,t,n,r)=>{try{if(i.errorCounter){let o=jr({operation:e,ai_service:t,...n?{model:n}:{},...r});i.errorCounter.add(1,o)}}catch(o){console.warn("Failed to record error metric:",o)}},ua=(i,e,t,n,r,o)=>{i.errorRateGauge&&i.errorRateGauge.record(t*100,{operation:e,ai_service:n,...r?{model:r}:{},...o})},ca=(i,e,t,n,r)=>{i.requestCounter&&i.requestCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},Vn=(i,e,t,n,r,o)=>{try{let s=jr({ai_service:n,...r?{model:r}:{},...o});i.tokenCounter&&i.tokenCounter.add(t,{token_type:e,...s}),e==="input"&&i.inputTokenCounter&&i.inputTokenCounter.add(t,s),e==="output"&&i.outputTokenCounter&&i.outputTokenCounter.add(t,s)}catch(s){console.warn("Failed to record token metric:",s)}},pa=(i,e,t,n,r,o)=>{t&&i.streamingRequestsCounter&&i.streamingRequestsCounter.add(1,{operation:e,ai_service:n,...r?{model:r}:{},...o})},da=(i,e,t,n,r,o)=>{let s={function_name:e,...n?{ai_service:n}:{},...r?{model:r}:{},...o};i.functionCallsCounter&&i.functionCallsCounter.add(1,s),t&&i.functionCallLatencyHistogram&&i.functionCallLatencyHistogram.record(t,s)},ws=(i,e,t,n,r,o)=>{i.requestSizeHistogram&&i.requestSizeHistogram.record(t,{operation:e,ai_service:n,...r?{model:r}:{},...o})},Rs=(i,e,t,n,r,o)=>{i.responseSizeHistogram&&i.responseSizeHistogram.record(t,{operation:e,ai_service:n,...r?{model:r}:{},...o})},ma=(i,e,t,n,r,o)=>{let s={...n?{ai_service:n}:{},...r?{model:r}:{},...o};e!==void 0&&i.temperatureGauge&&i.temperatureGauge.record(e,s),t!==void 0&&i.maxTokensGauge&&i.maxTokensGauge.record(t,s)},Ss=(i,e,t,n,r,o)=>{i.estimatedCostCounter&&i.estimatedCostCounter.add(t,{operation:e,ai_service:n,...r?{model:r}:{},...o})},ga=(i,e,t,n,r)=>{i.promptLengthHistogram&&i.promptLengthHistogram.record(e,{ai_service:t,...n?{model:n}:{},...r})},ha=(i,e,t,n,r)=>{i.contextWindowUsageGauge&&i.contextWindowUsageGauge.record(e,{ai_service:t,...n?{model:n}:{},...r})},fa=(i,e,t,n,r)=>{i.timeoutsCounter&&i.timeoutsCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},xa=(i,e,t,n,r)=>{i.abortsCounter&&i.abortsCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},Aa=(i,e,t,n,r)=>{i.thinkingBudgetUsageCounter&&i.thinkingBudgetUsageCounter.add(e,{ai_service:t,...n?{model:n}:{},...r})},ya=(i,e,t,n,r,o)=>{(e||t)&&i.multimodalRequestsCounter&&i.multimodalRequestsCounter.add(1,{ai_service:n,has_images:e.toString(),has_audio:t.toString(),...r?{model:r}:{},...o})},vs=(i,e,t,n,r,o)=>{try{if(t<=0)return;let s=jr({ai_service:n,...r?{model:r}:{},...o});e==="read"&&i.cacheReadTokensCounter&&i.cacheReadTokensCounter.add(t,s),e==="write"&&i.cacheWriteTokensCounter&&i.cacheWriteTokensCounter.add(t,s)}catch(s){console.warn("Failed to record cache token metric:",s)}};function ba(i){try{return JSON.stringify(i,null,2)}catch{return String(i)}}function gt(i,e={}){let t=[i];throw e.fieldPath!==void 0&&t.push(`Field: ${e.fieldPath}`),e.value!==void 0&&t.push(`Value: ${ba(e.value)}`),e.note&&t.push(`Note: ${e.note}`),e.item!==void 0&&t.push(`Chat item: ${ba(e.item)}`),new Error(t.join(`
168
- `))}function Kt(i){let e=n=>JSON.stringify(n,null,2);if(!i)throw new Error(`Chat request message item cannot be null or undefined, received: ${e(i)}`);let t=typeof i=="object"&&i!==null&&"role"in i&&typeof i.role=="string"?i.role:void 0;if(!t)throw new Error(`Chat request message must have a role, received: ${e(t)}`);switch(t){case"system":{let n=typeof i=="object"&&i!==null&&"content"in i&&typeof i.content=="string"?i.content:void 0;if(!n||n.trim()==="")throw new Error(`System message content cannot be empty or whitespace-only, received: ${e(n)}`);break}case"user":{let n=typeof i=="object"&&i!==null&&"content"in i?i.content:void 0;if(n===void 0)throw new Error(`User message content cannot be undefined, received: ${e(n)}`);if(typeof n=="string"){if(n.trim()==="")throw new Error(`User message content cannot be empty or whitespace-only, received: ${e(n)}`)}else if(Array.isArray(n)){if(n.length===0)throw new Error(`User message content array cannot be empty, received: ${e(n)}`);for(let r=0;r<n.length;r++){let o=n[r];if(!o||typeof o!="object")throw new Error(`User message content item at index ${r} must be an object, received: ${e(o)}`);let s=typeof o=="object"&&o!==null&&"type"in o&&typeof o.type=="string"?o.type:void 0;if(!s)throw new Error(`User message content item at index ${r} must have a type, received: ${e(s)}`);switch(s){case"text":{let a="text"in o&&typeof o.text=="string"?o.text:void 0;if(!a||a.trim()==="")throw new Error(`User message text content at index ${r} cannot be empty or whitespace-only, received: ${e(a)}`);break}case"image":{let a="image"in o&&typeof o.image=="string"?o.image:void 0,l="mimeType"in o&&typeof o.mimeType=="string"?o.mimeType:void 0;if(!a||a.trim()==="")throw new Error(`User message image content at index ${r} cannot be empty, received: ${e(a)}`);if(!l||l.trim()==="")throw new Error(`User message image content at index ${r} must have a mimeType, received: ${e(l)}`);break}case"audio":{let a="data"in o&&typeof o.data=="string"?o.data:void 0;if(!a||a.trim()==="")throw new Error(`User message audio content at index ${r} cannot be empty, received: ${e(a)}`);break}case"file":{let a="fileUri"in o&&typeof o.fileUri=="string",l="data"in o&&typeof o.data=="string";if(!a&&!l)throw new Error(`User message file content at index ${r} must have either 'data' or 'fileUri', received: ${e(o)}`);if(a&&l)throw new Error(`User message file content at index ${r} cannot have both 'data' and 'fileUri', received: ${e(o)}`);if(a){let c=o.fileUri;if(!c||c.trim()==="")throw new Error(`User message file content at index ${r} fileUri cannot be empty, received: ${e(c)}`)}if(l){let c=o.data;if(!c||c.trim()==="")throw new Error(`User message file content at index ${r} data cannot be empty, received: ${e(c)}`)}let u="mimeType"in o&&typeof o.mimeType=="string"?o.mimeType:null;if(!u||u.trim()==="")throw new Error(`User message file content at index ${r} must have a mimeType, received: ${e(u)}`);break}case"url":{let a="url"in o&&typeof o.url=="string"?o.url:void 0;if(!a||a.trim()==="")throw new Error(`User message url content at index ${r} cannot be empty, received: ${e(a)}`);break}default:throw new Error(`User message content item at index ${r} has unsupported type: ${e(s)}`)}}}else throw new Error(`User message content must be a string or array of content objects, received: ${e(n)}`);break}case"assistant":{let n=typeof i=="object"&&i!==null&&"content"in i?i.content:void 0,r=typeof i=="object"&&i!==null&&"functionCalls"in i?i.functionCalls:void 0,o=typeof i=="object"&&i!==null&&"thoughtBlocks"in i?i.thoughtBlocks:void 0,s=typeof n=="string"&&n.trim()!=="",a=Array.isArray(r)&&r.length>0,l=Array.isArray(o)&&o.length>0;if(!s&&!a&&!l&&gt("Assistant message must include non-empty content, at least one function call, or thought blocks",{fieldPath:"content | functionCalls | thoughtBlocks",value:{content:n,functionCalls:r,thoughtBlocks:o},item:i}),n!==void 0&&typeof n!="string"&&gt("Assistant message content must be a string",{fieldPath:"content",value:n,item:i}),r!==void 0&&!Array.isArray(r)&&gt("Assistant message functionCalls must be an array when provided",{fieldPath:"functionCalls",value:r,item:i}),Array.isArray(r))for(let u=0;u<r.length;u++){let c=r[u];if((!c||typeof c!="object")&&gt("functionCalls entry must be an object",{fieldPath:`functionCalls[${u}]`,value:c,item:i}),(!("id"in c)||typeof c.id!="string"||c.id.trim()==="")&&gt("functionCalls entry must include a non-empty string id",{fieldPath:`functionCalls[${u}].id`,value:c.id,item:i}),(!("type"in c)||c.type!=="function")&&gt("functionCalls entry must have type 'function'",{fieldPath:`functionCalls[${u}].type`,value:c.type,item:i}),!("function"in c)||!c.function)gt("functionCalls entry must include a function object",{fieldPath:`functionCalls[${u}].function`,value:c.function,item:i});else{let p=c.function;(!("name"in p)||typeof p.name!="string"||p.name.trim()==="")&&gt("functionCalls entry must include a non-empty function name",{fieldPath:`functionCalls[${u}].function.name`,value:p?.name,item:i}),p.params!==void 0&&typeof p.params!="string"&&typeof p.params!="object"&&gt("functionCalls entry params must be a string or object when provided",{fieldPath:`functionCalls[${u}].function.params`,value:p.params,item:i})}}if(i.name!==void 0){let u=i.name;(typeof u!="string"||u.trim()==="")&&gt("Assistant message name must be a non-empty string when provided",{fieldPath:"name",value:u,item:i})}break}case"function":{let n=typeof i=="object"&&i!==null&&"functionId"in i&&typeof i.functionId=="string"?i.functionId:void 0,r=typeof i=="object"&&i!==null&&"result"in i?i.result:void 0;if(!n||n.trim()==="")throw new Error(`Function message must have a non-empty functionId, received: ${e(n)}`);if(r==null)throw new Error(`Function message must have a result, received: ${e(r)}`);if(typeof r!="string")throw new Error(`Function message result must be a string, received: ${e(r)}`);i.isError!==void 0&&typeof i.isError!="boolean"&&gt("Function message isError must be a boolean when provided",{fieldPath:"isError",value:i.isError,item:i});break}default:throw new Error(`Unsupported message role: ${e(t)}`)}}function qr(i){let e=n=>JSON.stringify(n,null,2),t=Array.isArray(i)?i:[i];if(t.length===0)throw new Error(`Chat response results cannot be empty, received: ${e(t)}`);for(let n=0;n<t.length;n++){let r=t[n];if(!r)throw new Error(`Chat response result at index ${n} cannot be null or undefined, received: ${e(r)}`);if(typeof r.index!="number")throw new Error(`Chat response result at index ${n} must have a numeric index, received: ${e(r.index)}`);if(r.index<0)throw new Error(`Chat response result at index ${n} must have a non-negative index, received: ${e(r.index)}`);if(!r.content&&!r.thought&&(!r.thoughtBlocks||r.thoughtBlocks.length===0)&&!r.functionCalls&&!r.finishReason)throw new Error(`Chat response result at index ${n} must have at least one of: content, thought, thoughtBlocks, functionCalls, or finishReason, received: ${e({content:r.content,thought:r.thought,thoughtBlocks:r.thoughtBlocks,functionCalls:r.functionCalls,finishReason:r.finishReason})}`);if(r.content!==void 0&&typeof r.content!="string")throw new Error(`Chat response result content at index ${n} must be a string, received: ${e(r.content)}`);if(r.thought!==void 0&&typeof r.thought!="string")throw new Error(`Chat response result thought at index ${n} must be a string, received: ${e(r.thought)}`);if(r.thoughtBlocks!==void 0){if(!Array.isArray(r.thoughtBlocks))throw new Error(`Chat response result thoughtBlocks at index ${n} must be an array, received: ${e(r.thoughtBlocks)}`);for(let o=0;o<r.thoughtBlocks.length;o++){let s=r.thoughtBlocks[o];if(typeof s!="object"||s===null)throw new Error(`Chat response result thoughtBlocks[${o}] at index ${n} must be an object, received: ${e(s)}`);if(typeof s.data!="string")throw new Error(`Chat response result thoughtBlocks[${o}].data at index ${n} must be a string, received: ${e(s.data)}`);if(typeof s.encrypted!="boolean")throw new Error(`Chat response result thoughtBlocks[${o}].encrypted at index ${n} must be a boolean, received: ${e(s.encrypted)}`);if(s.signature!==void 0&&typeof s.signature!="string")throw new Error(`Chat response result thoughtBlocks[${o}].signature at index ${n} must be a string when provided, received: ${e(s.signature)}`)}}if(r.name!==void 0){if(typeof r.name!="string")throw new Error(`Chat response result name at index ${n} must be a string, received: ${e(r.name)}`);if(r.name.trim()==="")throw new Error(`Chat response result name at index ${n} cannot be empty or whitespace-only, received: ${e(r.name)}`)}if(r.annotations!==void 0){if(!Array.isArray(r.annotations))throw new Error(`Chat response result annotations at index ${n} must be an array, received: ${e(r.annotations)}`);for(let o=0;o<r.annotations.length;o++){let s=r.annotations[o];if(!s||typeof s!="object")throw new Error(`Chat response result annotation at index ${n}[${o}] must be an object, received: ${e(s)}`);if(s.type!=="url_citation")throw new Error(`Chat response result annotation at index ${n}[${o}] must have type 'url_citation', received: ${e(s.type)}`);if(!s.url_citation||typeof s.url_citation!="object")throw new Error(`Chat response result annotation at index ${n}[${o}] must have a valid url_citation object, received: ${e(s.url_citation)}`);if(typeof s.url_citation.url!="string")throw new Error(`Chat response result annotation at index ${n}[${o}] url_citation.url must be a string, received: ${e(s.url_citation.url)}`)}}if(r.id!==void 0){if(typeof r.id!="string")throw new Error(`Chat response result id at index ${n} must be a string, received: ${e(r.id)}`);if(r.id.trim()==="")throw new Error(`Chat response result id at index ${n} cannot be empty or whitespace-only, received: ${e(r.id)}`)}if(r.functionCalls!==void 0){if(!Array.isArray(r.functionCalls))throw new Error(`Chat response result functionCalls at index ${n} must be an array, received: ${e(r.functionCalls)}`);for(let o=0;o<r.functionCalls.length;o++){let s=r.functionCalls[o];if(!s)throw new Error(`Function call at index ${o} in result ${n} cannot be null or undefined, received: ${e(s)}`);if(!s.id||typeof s.id!="string"||s.id.trim()==="")throw new Error(`Function call at index ${o} in result ${n} must have a non-empty string id, received: ${e(s.id)}`);if(s.type!=="function")throw new Error(`Function call at index ${o} in result ${n} must have type 'function', received: ${e(s.type)}`);if(!s.function)throw new Error(`Function call at index ${o} in result ${n} must have a function object, received: ${e(s.function)}`);if(!s.function.name||typeof s.function.name!="string"||s.function.name.trim()==="")throw new Error(`Function call at index ${o} in result ${n} must have a non-empty function name, received: ${e(s.function.name)}`);if(s.function.params!==void 0&&typeof s.function.params!="string"&&typeof s.function.params!="object")throw new Error(`Function call params at index ${o} in result ${n} must be a string or object, received: ${e(s.function.params)}`)}}if(r.finishReason!==void 0){let o=["stop","length","function_call","content_filter","error"];if(!o.includes(r.finishReason))throw new Error(`Chat response result finishReason at index ${n} must be one of: ${o.join(", ")}, received: ${e(r.finishReason)}`)}}}var ks=new Map;function ec(i){return`${i.providerName}:${i.contentHash}`}function tc(i,e){e.type==="text"?i.update(`text:${e.text}`):e.type==="image"?i.update(`image:${e.mimeType}:${e.image.slice(0,100)}`):e.type==="audio"?i.update(`audio:${e.format}:${e.data.slice(0,100)}`):e.type==="file"&&("fileUri"in e?i.update(`file:${e.mimeType}:${e.fileUri}`):i.update(`file:${e.mimeType}:${e.data.slice(0,100)}`))}function nc(i){let e=tt("sha256"),t=-1;for(let n=i.length-1;n>=0;n--){let r=i[n];if("cache"in r&&r.cache){t=n;break}}for(let n=0;n<i.length;n++){let r=i[n];if(r.role==="system"){e.update(`system:${r.content}`);continue}if(t>=0&&n<=t)if(r.role==="user"){if(typeof r.content=="string")e.update(`user:${r.content}`);else if(Array.isArray(r.content))for(let o of r.content)tc(e,o)}else r.role==="assistant"&&r.content&&e.update(`assistant:${r.content}`)}return e.digest("hex")}var be=()=>structuredClone({temperature:0}),Le=()=>structuredClone({temperature:.4,frequencyPenalty:.2}),Ee=class{constructor(e,{name:t,apiURL:n,headers:r,modelInfo:o,defaults:s,options:a={},supportFor:l,models:u}){this.aiImpl=e;this.name=t,this.apiURL=n||"",this.headers=r,this.supportFor=l,this.tracer=a.tracer??ce.tracer,this.meter=a.meter??ce.meter,this.modelInfo=o,this.models=u,this.id=ze();let c=this.getModel(s.model)??s.model,p=this.getEmbedModel(s.embedModel)??s.embedModel;if(this.defaults={model:c,embedModel:p},!s.model||typeof s.model!="string"||s.model==="")throw new Error("No model defined");this.setOptions(a),u&&rc(u)}#e=!1;#t=!1;rt;fetch;tracer;meter;timeout;excludeContentFromTrace;models;abortSignal;logger=ce.logger??Wi;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 sa(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??ce.debug??!1,this.#t=e.verbose??!1,this.rt=e.rateLimiter,this.fetch=e.fetch,this.timeout=e.timeout,this.tracer=e.tracer??ce.tracer,this.meter=e.meter??ce.meter,this.excludeContentFromTrace=e.excludeContentFromTrace,this.abortSignal=e.abortSignal,this.logger=e.logger??ce.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 Ft(ce.customLabels,this.customLabels,e)}getModelList(){let e=[];for(let t of this.models??[])t.isInternal||("model"in t&&t.model&&e.push({key:t.key,description:t.description,model:t.model}),"embedModel"in t&&t.embedModel&&e.push({key:t.key,description:t.description,embedModel:t.embedModel}));return e}getName(){return this.name}getFeatures(e){return typeof this.supportFor=="function"?this.supportFor(e??this.defaults.model):this.supportFor}getLastUsedChatModel(){return this.lastUsedChatModel}getLastUsedEmbedModel(){return this.lastUsedEmbedModel}getLastUsedModelConfig(){return this.lastUsedModelConfig}calculatePercentile(e,t){if(e.length===0)return 0;let n=[...e].sort((o,s)=>o-s),r=Math.ceil(t/100*n.length)-1;return n[r]??0}updateLatencyMetrics(e,t,n){let r=this.metrics.latency[e];r.samples.push(t),r.samples.length>1e3&&r.samples.shift(),r.mean=r.samples.reduce((s,a)=>s+a,0)/r.samples.length,r.p95=this.calculatePercentile(r.samples,95),r.p99=this.calculatePercentile(r.samples,99);let o=this.getMetricsInstruments();if(o){let s=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel,a=this.getMergedCustomLabels(n);ia(o,e,t,this.name,s,a),aa(o,e,r.mean,r.p95,r.p99,this.name,s,a)}}updateErrorMetrics(e,t,n){let r=this.metrics.errors[e];r.total++,t&&r.count++,r.rate=r.count/r.total;let o=this.getMetricsInstruments();if(o){let s=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel,a=this.getMergedCustomLabels(n);ca(o,e,this.name,s,a),t&&la(o,e,this.name,s,a),ua(o,e,r.rate,this.name,s,a)}}recordTokenUsage(e,t){let n=this.getMetricsInstruments();if(n&&e?.tokens){let{promptTokens:r,completionTokens:o,totalTokens:s,thoughtsTokens:a,cacheReadTokens:l,cacheCreationTokens:u}=e.tokens,c=this.getMergedCustomLabels(t);r&&Vn(n,"input",r,this.name,e.model,c),o&&Vn(n,"output",o,this.name,e.model,c),s&&Vn(n,"total",s,this.name,e.model,c),a&&Vn(n,"thoughts",a,this.name,e.model,c),l&&vs(n,"read",l,this.name,e.model,c),u&&vs(n,"write",u,this.name,e.model,c)}}calculateRequestSize(e){try{return new TextEncoder().encode(JSON.stringify(e)).length}catch{return 0}}calculateResponseSize(e){try{return new TextEncoder().encode(JSON.stringify(e)).length}catch{return 0}}detectMultimodalContent(e){let t=!1,n=!1;if(e.chatPrompt&&Array.isArray(e.chatPrompt)){for(let r of e.chatPrompt)if(r.role==="user"&&Array.isArray(r.content))for(let o of r.content)o.type==="image"?t=!0:o.type==="audio"&&(n=!0)}return{hasImages:t,hasAudio:n}}calculatePromptLength(e){let t=0;if(e.chatPrompt&&Array.isArray(e.chatPrompt))for(let n of e.chatPrompt)if(n.role==="system"||n.role==="assistant")n.content&&(t+=n.content.length);else if(n.role==="user"){if(typeof n.content=="string")t+=n.content.length;else if(Array.isArray(n.content))for(let r of n.content)r.type==="text"&&(t+=r.text.length)}else n.role==="function"&&n.result&&(t+=n.result.length);return t}calculateContextWindowUsage(e,t){if(!t?.tokens?.promptTokens)return 0;let n=this.modelInfo.find(r=>r.name===e);return n?.contextWindow?t.tokens.promptTokens/n.contextWindow:0}estimateCost(e,t){if(!t?.tokens)return 0;let n=this.modelInfo.find(l=>l.name===e);if(!n||!n.promptTokenCostPer1M&&!n.completionTokenCostPer1M)return 0;let{promptTokens:r=0,completionTokens:o=0}=t.tokens,s=n.promptTokenCostPer1M||0,a=n.completionTokenCostPer1M||0;return r*s/1e6+o*a/1e6}estimateCostByName(e,t){if(!t?.tokens)return 0;let n=this.modelInfo.find(l=>l.name===e);if(!n||!n.promptTokenCostPer1M&&!n.completionTokenCostPer1M)return 0;let{promptTokens:r=0,completionTokens:o=0}=t.tokens,s=n.promptTokenCostPer1M||0,a=n.completionTokenCostPer1M||0;return r*s/1e6+o*a/1e6}recordFunctionCallMetrics(e,t,n){let r=this.getMetricsInstruments();if(!(!r||!e))for(let o of e)o&&typeof o=="object"&&"function"in o&&o.function&&typeof o.function=="object"&&"name"in o.function&&da(r,o.function.name,void 0,this.name,t,this.getMergedCustomLabels(n))}recordTimeoutMetric(e,t){let n=this.getMetricsInstruments();if(n){let r=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel;fa(n,e,this.name,r,this.getMergedCustomLabels(t))}}recordAbortMetric(e,t){let n=this.getMetricsInstruments();if(n){let r=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel;xa(n,e,this.name,r,this.getMergedCustomLabels(t))}}recordChatMetrics(e,t,n){let r=this.getMetricsInstruments();if(!r)return;let o=this.lastUsedChatModel,s=this.lastUsedModelConfig,a=this.getMergedCustomLabels(t?.customLabels),l=s?.stream??!1;pa(r,"chat",l,this.name,o,a);let{hasImages:u,hasAudio:c}=this.detectMultimodalContent(e);ya(r,u,c,this.name,o,a);let p=this.calculatePromptLength(e);ga(r,p,this.name,o,a),ma(r,s?.temperature,s?.maxTokens,this.name,o,a),t?.thinkingTokenBudget&&this.modelUsage?.tokens?.thoughtsTokens&&Aa(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(Rs(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 h=this.calculateContextWindowUsage(this.lastUsedChatModel,m.modelUsage);h>0&&ha(r,h,this.name,o,a);let f=this.estimateCost(this.lastUsedChatModel,m.modelUsage);f>0&&Ss(r,"chat",f,this.name,o,a)}}recordEmbedMetrics(e,t,n){let r=this.getMetricsInstruments();if(!r)return;let o=this.lastUsedEmbedModel,s=this.getMergedCustomLabels(n?.customLabels),a=this.calculateRequestSize(e);ws(r,"embed",a,this.name,o,s);let l=this.calculateResponseSize(t);Rs(r,"embed",l,this.name,o,s);let u=this.estimateCostByName(o,t.modelUsage);u>0&&Ss(r,"embed",u,this.name,o,s)}getMetrics(){return structuredClone(this.metrics)}async chat(e,t){let n=performance.now(),r=!1,o,s=this.getModelByKey(e.model),a=s?s.thinkingTokenBudget:void 0,l={...s?{thinkingTokenBudget:a,showThoughts:s.showThoughts,stream:s.stream,debug:s.debug,useExpensiveModel:s.useExpensiveModel}:void 0,...Object.fromEntries(Object.entries(t??{}).filter(([,u])=>u!==void 0))};try{return o=await this._chat1(e,l),o}catch(u){throw r=!0,u instanceof Error&&(u.message.includes("timeout")||u.name==="TimeoutError"?this.recordTimeoutMetric("chat",l?.customLabels):(u.message.includes("abort")||u.name==="AbortError")&&this.recordAbortMetric("chat",l?.customLabels)),u}finally{let u=performance.now()-n;this.updateLatencyMetrics("chat",u,l?.customLabels),this.updateErrorMetrics("chat",r,l?.customLabels),r||this.recordChatMetrics(e,l,o)}}async _chat1(e,t){let n=this.getModel(e.model)??e.model??this.defaults.model;if(Array.isArray(e.chatPrompt))for(let u of e.chatPrompt)Kt(u);let r=this.getModelByKey(e.model),o={...this.aiImpl.getModelConfig(),...r?r.modelConfig:void 0,...e.modelConfig},s=this.modelInfo.find(u=>u.name===n);if(s?.notSupported?.temperature&&"temperature"in o&&delete o.temperature,s?.notSupported?.topP&&"topP"in o&&delete o.topP,t?.thinkingTokenBudget&&!this.getFeatures(n).hasThinkingBudget)throw new Error(`Model ${n} does not support thinkingTokenBudget.`);if(t?.showThoughts&&!this.getFeatures(n).hasShowThoughts)throw new Error(`Model ${n} does not support showThoughts.`);if(this.modelInfo.find(u=>u.name===n)?.isExpensive&&t?.useExpensiveModel!=="yes")throw new Error(`Model ${n} is marked as expensive and requires explicit confirmation. Set useExpensiveModel: "yes" to proceed.`);return o.stream=(t?.stream!==void 0?t.stream:o.stream)??!0,this.getFeatures(n).streaming||(o.stream=!1),this.tracer?await this.tracer.startActiveSpan("AI Chat Request",{kind:Wt.SpanKind.SERVER,attributes:{[me.LLM_SYSTEM]:this.name,[me.LLM_OPERATION_NAME]:"chat",[me.LLM_REQUEST_MODEL]:n,[me.LLM_REQUEST_MAX_TOKENS]:o.maxTokens??"Not set",[me.LLM_REQUEST_TEMPERATURE]:o.temperature,[me.LLM_REQUEST_TOP_P]:o.topP??"Not set",[me.LLM_REQUEST_TOP_K]:o.topK??"Not set",[me.LLM_REQUEST_FREQUENCY_PENALTY]:o.frequencyPenalty??"Not set",[me.LLM_REQUEST_PRESENCE_PENALTY]:o.presencePenalty??"Not set",[me.LLM_REQUEST_STOP_SEQUENCES]:o.stopSequences?.join(", ")??"Not set",[me.LLM_REQUEST_LLM_IS_STREAMING]:o.stream??"Not set"}},t?.traceContext??Wt.context.active(),async u=>await this._chat2(n,o,e,t,u)):await this._chat2(n,o,e,t)}cleanupFunctionSchema(e){let t={...e};if(t.parameters){let n={...t.parameters};Array.isArray(n.required)&&n.required.length===0&&delete n.required,n.properties&&Object.keys(n.properties).length===0&&delete n.properties,Object.keys(n).length===0||Object.keys(n).length===1&&n.type==="object"?delete t.parameters:t.parameters=n}return t}async _chat2(e,t,n,r,o){if(!this.aiImpl.createChatReq)throw new Error("createChatReq not implemented");let s=r?.debug??this.#e,a=r?.verbose??this.#t,l;n.functions&&n.functions.length>0&&(l=n.functions.map(A=>this.cleanupFunctionSchema(A)));let u={...n,model:e,functions:l,modelConfig:t};this.lastUsedChatModel=e,this.lastUsedModelConfig=t,s&&Ji(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(A=>{if(A.role==="assistant"){let{content:w,name:I,cache:T}=A;return{role:"assistant",content:w,name:I,cache:T}}return A.role==="function"?{role:"user",content:A.result}:A}),functions:[]}:u,g=await this.handleContextCaching(e,m,r,o),h=async()=>{if(g?.preparedRequest){let{apiConfig:T,request:v}=g.preparedRequest;return o?.isRecording()&&Ia(n,o,this.excludeContentFromTrace),await Ke({name:T.name,url:this.apiURL,localCall:T.localCall,headers:await this.buildHeaders(T.headers),stream:t.stream,timeout:this.timeout,verbose:a,fetch:this.fetch,span:o,abortSignal:r?.abortSignal??this.abortSignal,corsProxy:this.corsProxy,retry:r?.retry??this.retry},v)}let[A,w]=await this.aiImpl.createChatReq(m,r);return o?.isRecording()&&Ia(n,o,this.excludeContentFromTrace),await Ke({name:A.name,url:this.apiURL,localCall:A.localCall,headers:await this.buildHeaders(A.headers),stream:t.stream,timeout:this.timeout,verbose:a,fetch:this.fetch,span:o,abortSignal:r?.abortSignal??this.abortSignal,corsProxy:this.corsProxy,retry:r?.retry??this.retry},w)},f=r?.rateLimiter??this.rt,b=f?await f(h,{modelUsage:this.modelUsage}):await h();if(t.stream){if(!this.aiImpl.createChatStreamResp)throw new Error("createChatStreamResp not implemented");let A=this.aiImpl.createChatStreamResp.bind(this),w=F=>E=>{let D=A(E,F);if(D.sessionId=r?.sessionId,!D.modelUsage){let U=this.aiImpl.getTokenUsage();U&&(D.modelUsage={ai:this.name,model:e,tokens:U})}return this.modelUsage=D.modelUsage,this.recordTokenUsage(D.modelUsage,r?.customLabels),o?.isRecording()&&Ca(D,o,this.excludeContentFromTrace),D},I=async F=>{o?.isRecording()&&o.end(),s&&Yi(F,r?.logger??this.logger)};if(typeof window<"u"){let F=b,E={},D=[],U=r?.abortSignal??this.abortSignal;return new ReadableStream({start:V=>{let k=F.getReader(),$=()=>{try{k.cancel().catch(()=>{})}catch{}try{this.recordAbortMetric("chat",r?.customLabels)}catch{}try{o?.isRecording()&&o.end()}catch{}try{V.error(new DOMException("Aborted","AbortError"))}catch{V.error(new Error("Aborted"))}};if(U){if(U.aborted){$();return}U.addEventListener("abort",$,{once:!0})}async function N(){try{for(;;){let{done:Q,value:M}=await k.read();if(Q){I&&await I(D),V.close();break}let L=w(E)(M);L&&(D.push(L),V.enqueue(L))}}catch(Q){if(V.error(Q),o?.isRecording())try{o.end()}catch{}}finally{if(k.releaseLock(),U)try{U.removeEventListener("abort",$)}catch{}}}N()}})}return b.pipeThrough(new Dr(w({}),I))}if(!this.aiImpl.createChatResp)throw new Error("createChatResp not implemented");let x=this.aiImpl.createChatResp(b);if(x.sessionId=r?.sessionId,!x.modelUsage){let A=this.aiImpl.getTokenUsage();A&&(x.modelUsage={ai:this.name,model:e,tokens:A})}return x.modelUsage&&(this.modelUsage=x.modelUsage,this.recordTokenUsage(x.modelUsage,r?.customLabels)),o?.isRecording()&&(Ca(x,o,this.excludeContentFromTrace),o.end()),s&&Qi(x,r?.logger??this.logger),x}async embed(e,t){let n=performance.now(),r=!1,o,s=this.getModelByKey(e.embedModel),a={...s?{thinkingTokenBudget:s.thinkingTokenBudget,showThoughts:s.showThoughts,stream:s.stream,debug:s.debug,useExpensiveModel:s.useExpensiveModel}:void 0,...t};try{return o=await this._embed1(e,a),o}catch(l){throw r=!0,l instanceof Error&&(l.message.includes("timeout")||l.name==="TimeoutError"?this.recordTimeoutMetric("embed",a?.customLabels):(l.message.includes("abort")||l.name==="AbortError")&&this.recordAbortMetric("embed",a?.customLabels)),l}finally{let l=performance.now()-n;this.updateLatencyMetrics("embed",l,a?.customLabels),this.updateErrorMetrics("embed",r,a?.customLabels),!r&&o&&this.recordEmbedMetrics(e,o,a)}}async _embed1(e,t){let n=this.getEmbedModel(e.embedModel)??e.embedModel??this.defaults.embedModel;if(!n)throw new Error("No embed model defined");return this.tracer?await this.tracer.startActiveSpan("AI Embed Request",{kind:Wt.SpanKind.SERVER,attributes:{[me.LLM_SYSTEM]:this.name,[me.LLM_OPERATION_NAME]:"embeddings",[me.LLM_REQUEST_MODEL]:n}},t?.traceContext??Wt.context.active(),async r=>await this._embed2(n,e,t,r)):await this._embed2(n,e,t)}async _embed2(e,t,n,r){if(!this.aiImpl.createEmbedReq)throw new Error("createEmbedReq not implemented");if(!this.aiImpl.createEmbedResp)throw new Error("createEmbedResp not implemented");let o=this.aiImpl.createEmbedReq.bind(this.aiImpl),s=n?.debug??this.#e,a=n?.verbose??this.#t,l={...t,embedModel:e};this.lastUsedEmbedModel=e,s&&na(l.texts??[],e,n?.logger??this.logger);let u=async()=>{let[m,g]=await o(l);return await Ke({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(mt.GEN_AI_USAGE,{[me.LLM_USAGE_INPUT_TOKENS]:d.modelUsage.tokens.promptTokens,[me.LLM_USAGE_OUTPUT_TOKENS]:d.modelUsage.tokens.completionTokens??0,[me.LLM_USAGE_TOTAL_TOKENS]:d.modelUsage.tokens.totalTokens}),s&&ra(d.embeddings,n?.logger??this.logger),r?.end(),d}async buildHeaders(e={}){return{...e,...await this.headers()}}getModelByKey(e){return e?this.models?.find(n=>n.key===e):void 0}getModel(e){let t=this.getModelByKey(e);return t&&"model"in t?t.model:void 0}getEmbedModel(e){let t=this.getModelByKey(e);return t&&"embedModel"in t?t.embedModel:void 0}async handleContextCaching(e,t,n,r){let o=n?.contextCache;if(!o)return null;let s=this.aiImpl.supportsContextCache?.(e)??!1,a=this.aiImpl.supportsImplicitCaching?.(e)??!1;if(!s&&!a)throw new Error(`Context caching is not supported by this provider/model (${this.getName()}/${e}). Remove the contextCache option or use a provider that supports caching.`);if(!s)return null;let l=o.ttlSeconds??3600,u=o.refreshWindowSeconds??300,c=o.minTokens??2048;if(o.name)return this.useCacheByName(e,t,o.name,n,r);let p=nc(t.chatPrompt);if(!p||p===tt("sha256").digest("hex"))return null;let d={providerName:this.getName(),contentHash:p},m=ec(d),g=Date.now(),h=o.registry,f=h?await h.get(m):ks.get(m);if(f&&f.expiresAt>g){if(f.expiresAt-g<u*1e3&&this.aiImpl.buildCacheUpdateTTLOp){await this.executeCacheOperation(this.aiImpl.buildCacheUpdateTTLOp(f.cacheName,l),n,r);let w={cacheName:f.cacheName,expiresAt:g+l*1e3,tokenCount:f.tokenCount};h?await h.set(m,w):ks.set(m,{...w,contentHash:p,lastTouchedAt:g})}return this.useCacheByName(e,t,f.cacheName,n,r)}if(this.estimateCacheableTokens(t.chatPrompt)<c)return null;let x=this.aiImpl.buildCacheCreateOp?.(t,n);if(x){let A=await this.executeCacheOperation(x,n,r);if(A){let w={cacheName:A.name,expiresAt:new Date(A.expiresAt).getTime(),tokenCount:A.tokenCount};return h?await h.set(m,w):ks.set(m,{...w,contentHash:p,lastTouchedAt:g}),this.useCacheByName(e,t,A.name,n,r)}}return null}async useCacheByName(e,t,n,r,o){if(this.aiImpl.prepareCachedChatReq){let s=await this.aiImpl.prepareCachedChatReq(t,r??{},n);return{preparedRequest:{apiConfig:s.apiConfig,request:s.request}}}return null}async executeCacheOperation(e,t,n){let r=t?.verbose??this.#t;try{n?.addEvent("context_cache.operation",{type:e.type,endpoint:e.apiConfig.name});let o=await Ke({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 Ia(i,e,t){let n=[];if(i.chatPrompt&&Array.isArray(i.chatPrompt)&&i.chatPrompt.length>0)for(let o of i.chatPrompt)switch(o.role){case"system":if(o.content){let s={};t||(s.content=o.content),e.addEvent(mt.GEN_AI_SYSTEM_MESSAGE,s)}break;case"user":if(typeof o.content=="string")n.push(o.content);else if(Array.isArray(o.content))for(let s of o.content)s.type==="text"&&n.push(s.text);break;case"assistant":{let s=o.functionCalls?.map(a=>({id:a.id,type:a.type,function:a.function.name,arguments:a.function.params}));if(s&&s.length>0){let a={function_calls:JSON.stringify(s,null,2)};!t&&o.content&&(a.content=o.content),e.addEvent(mt.GEN_AI_ASSISTANT_MESSAGE,a)}else if(o.content){let a={};t||(a.content=o.content),e.addEvent(mt.GEN_AI_ASSISTANT_MESSAGE,a)}break}case"function":{let s={id:o.functionId};t||(s.content=o.result),e.addEvent(mt.GEN_AI_TOOL_MESSAGE,s);break}}let r={};t||(r.content=n.join(`
169
- `)),e.addEvent(mt.GEN_AI_USER_MESSAGE,r)}function Ca(i,e,t){if(i.modelUsage?.tokens){let n=i.modelUsage.tokens.thoughtsTokens?{[me.LLM_USAGE_THOUGHTS_TOKENS]:i.modelUsage.tokens.thoughtsTokens}:{};e.addEvent(mt.GEN_AI_USAGE,{[me.LLM_USAGE_INPUT_TOKENS]:i.modelUsage.tokens.promptTokens,[me.LLM_USAGE_OUTPUT_TOKENS]:i.modelUsage.tokens.completionTokens??0,[me.LLM_USAGE_TOTAL_TOKENS]:i.modelUsage.tokens.totalTokens,...n})}if(i.results)for(let n=0;n<i.results.length;n++){let r=i.results[n];if(!r||!r.content&&!r.thought&&!r.functionCalls?.length&&!r.finishReason)continue;let o=r.functionCalls?.map(a=>({id:a.id,type:a.type,function:a.function.name,arguments:a.function.params})),s={};o&&o.length>0?(t||(s.content=r.content),s.tool_calls=o):t||(s.content=r.content??""),e.addEvent(mt.GEN_AI_CHOICE,{finish_reason:r.finishReason,index:n,message:JSON.stringify(s,null,2)})}}function Ta(i){let e=0;for(let t of i){if(!t||typeof t!="object")throw new Error(`AxMessage array validation failed: Item at index ${e} is not a valid message object`);if(t.role!=="user"&&t.role!=="assistant")throw new Error(`AxMessage array validation failed: Item at index ${e} has invalid role: ${t.role}`);e++}}function rc(i){let e=new Set;for(let t of i){if(e.has(t.key))throw new Error(`Duplicate model key detected: "${t.key}". Each model key must be unique.`);e.add(t.key)}}var Jn=(f=>(f.Claude46Opus="claude-opus-4-6",f.Claude46Sonnet="claude-sonnet-4-6",f.Claude45Opus="claude-opus-4-5-20251101",f.Claude41Opus="claude-opus-4-1-20250805",f.Claude4Opus="claude-opus-4-20250514",f.Claude4Sonnet="claude-sonnet-4-20250514",f.Claude45Sonnet="claude-sonnet-4-5-20250929",f.Claude45Haiku="claude-haiku-4-5",f.Claude37Sonnet="claude-3-7-sonnet-latest",f.Claude35Sonnet="claude-3-5-sonnet-latest",f.Claude35Haiku="claude-3-5-haiku-latest",f.Claude3Opus="claude-3-opus-latest",f.Claude3Sonnet="claude-3-sonnet-20240229",f.Claude3Haiku="claude-3-haiku-20240307",f.Claude21="claude-2.1",f.ClaudeInstant12="claude-instant-1.2",f))(Jn||{}),Qn=(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))(Qn||{});var Yn=[{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 kt=i=>{if(!i||typeof i!="object")return i;let e={...i};if(e.type==="object"||Array.isArray(e.type)&&e.type.includes("object")){if(!e.properties||Object.keys(e.properties).length===0)throw new Error("Anthropic models do not support arbitrary JSON objects (e.g. f.json() or f.object() with no properties) in structured outputs. Please use f.string() and instruct the model to return a JSON string, or define the expected structure with f.object({ ... })");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,kt(r)]))),e.items&&(e.items=kt(e.items)),Array.isArray(e.anyOf)&&(e.anyOf=e.anyOf.map(n=>kt(n))),Array.isArray(e.allOf)&&(e.allOf=e.allOf.map(n=>kt(n))),Array.isArray(e.oneOf)&&(e.oneOf=e.oneOf.map(n=>kt(n))),e},Ms=()=>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()}),Ra=()=>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()}),Os=class{constructor(e,t){this.config=e;this.isVertex=t}tokensUsed;currentPromptConfig;usedStructuredOutput=!1;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens??4096,temperature:e.temperature,topP:e.topP,topK:e.topK,stream:e.stream,stopSequences:e.stopSequences,endSequences:e.endSequences,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,n:e.n}}createChatReq=async(e,t)=>{this.currentPromptConfig=t;let n=e.model,r=e.modelConfig?.stream??this.config.stream,o;this.isVertex?o={name:r?`/models/${n}:streamRawPredict?alt=sse`:`/models/${n}:rawPredict`}:o={name:"/messages"};let s;if(e.functionCall&&e.functions&&e.functions.length>0)if(typeof e.functionCall=="string")switch(e.functionCall){case"auto":s={tool_choice:{type:"auto"}};break;case"required":s={tool_choice:{type:"any"}};break;case"none":throw new Error("functionCall none not supported")}else if("function"in e.functionCall)s={tool_choice:{type:"tool",name:e.functionCall.function.name}};else throw new Error("Invalid function call type, must be string or object");let a=e.chatPrompt.some(N=>"cache"in N&&N.cache)||e.functions?.some(N=>N.cache),l=e.chatPrompt.filter(N=>N.role==="system"),u=l.map((N,Q)=>({type:"text",text:N.content,...N.cache||a&&Q===l.length-1?{cache_control:{type:"ephemeral"}}:{}})),c=e.chatPrompt.filter(N=>N.role!=="system"),p=e.functions?.map((N,Q,M)=>{let L={type:"object",properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]},Y=N.parameters?kt(N.parameters):void 0;return Y===void 0||Y&&typeof Y=="object"&&Object.keys(Y).length===0?Y={...L}: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:N.name,description:N.description,input_schema:Y,...N.cache||a&&Q===M.length-1?{cache_control:{type:"ephemeral"}}:{}}}),m=(this.config.tools??[]).map(N=>N&&typeof N=="object"&&"type"in N?N:{name:N.name,description:N.description,input_schema:N.input_schema?kt(N.input_schema):void 0,...N.cache_control?{cache_control:N.cache_control}:{}}),g=[...p??[],...m];g.length===0&&(g=void 0);let h=e.modelConfig?.maxTokens??this.config.maxTokens,f=e.modelConfig?.stopSequences??this.config.stopSequences,b=e.modelConfig?.temperature,x=e.modelConfig?.topP,A=e.modelConfig?.topK??this.config.topK,w=e.modelConfig?.n??this.config.n;if(w&&w>1)throw new Error("Anthropic does not support sampling (n > 1)");let I=N=>N.includes("claude-opus-4-6"),T=N=>N.includes("claude-opus-4-5"),v,F,E=n;if(t?.thinkingTokenBudget){let N=this.config.thinkingTokenBudgetLevels,Q=this.config.effortLevelMapping;if(t.thinkingTokenBudget==="none")v=void 0,F=void 0;else{let M=t.thinkingTokenBudget;if(I(E))v={type:"adaptive"},F={effort:Q?.[M]??"medium"};else if(T(E)){v={type:"enabled",budget_tokens:N?.[M]??1e4};let Y=Q?.[M]??"medium";Y==="max"&&(Y="high"),F={effort:Y}}else v={type:"enabled",budget_tokens:N?.[M]??1e4}}}let U=oc(c,!!v);U.some(N=>N.role==="assistant"&&Array.isArray(N.content)&&N.content.length>0&&N.content[0]?.type==="tool_use")&&(v=void 0,F=void 0);let k;if(this.usedStructuredOutput=!1,e.responseFormat&&e.responseFormat.type==="json_schema"&&e.responseFormat.schema){let N=e.responseFormat.schema.schema||e.responseFormat.schema;k={type:"json_schema",schema:kt(N)},this.usedStructuredOutput=!0}let $={...this.isVertex?{anthropic_version:"vertex-2023-10-16"}:{model:n},...h?{max_tokens:h}:{},...f&&f.length>0?{stop_sequences:f}:{},...b!==void 0&&!v?{temperature:b}:{},...x!==void 0&&(!v||x>=.95)?{top_p:x}:{},...A&&!v?{top_k:A}:{},...s,...g?{tools:g}:{},...r?{stream:!0}:{},...u?{system:u}:{},...v?{thinking:v}:{},...F?{output_config:F}:{},...k?{output_format:k}:{},messages:U};return[o,$]};createChatResp=e=>{if(e.type==="error")throw new Se(e.error.message,void 0,void 0);let t=wa(e.stop_reason),n=this.currentPromptConfig?.thinkingTokenBudget!=="none"&&this.currentPromptConfig?.showThoughts!==!1,r="",o=[],s=[],a=[];for(let c of e.content)switch(c.type){case"text":if(r+=c.text??"",Array.isArray(c.citations))for(let p of c.citations)p?.url&&a.push({url:String(p.url),title:typeof p.title=="string"?p.title:void 0,snippet:typeof p.cited_text=="string"?p.cited_text:void 0});break;case"thinking":if(n){let p=c.thinking??"",d=c.signature;s.push({data:p,encrypted:!1,...typeof d=="string"?{signature:d}:{}})}break;case"redacted_thinking":if(n){let p=c.data??"",d=c.signature;s.push({data:p,encrypted:!0,...typeof d=="string"?{signature:d}:{}})}break;case"tool_use":o.push({id:c.id,type:"function",function:{name:c.name,params:c.input}});break}let l={index:0,id:e.id,finishReason:t};r&&(l.content=r),s.length>0&&(l.thoughtBlocks=s,l.thought=s.map(c=>c.data).join("")),o.length>0&&(l.functionCalls=o),a.length>0&&(l.citations=a);let u=[l];return this.tokensUsed={promptTokens:e.usage.input_tokens,completionTokens:e.usage.output_tokens,totalTokens:e.usage.input_tokens+e.usage.output_tokens+(e.usage.cache_creation_input_tokens||0)+(e.usage.cache_read_input_tokens||0),cacheCreationTokens:e.usage.cache_creation_input_tokens,cacheReadTokens:e.usage.cache_read_input_tokens},{results:u,remoteId:e.id}};createChatStreamResp=(e,t)=>{if(!("type"in e))throw new Error("Invalid Anthropic streaming event");let n=t;if(n.indexIdMap||(n.indexIdMap={}),e.type==="error"){let{error:o}=e;throw new Se(o.message,void 0,void 0)}let r=0;if(e.type==="message_start"){let{message:o}=e,s=[{index:r,content:"",id:o.id}];return this.tokensUsed={promptTokens:o.usage?.input_tokens??0,completionTokens:o.usage?.output_tokens??0,totalTokens:(o.usage?.input_tokens??0)+(o.usage?.output_tokens??0)+(o.usage?.cache_creation_input_tokens??0)+(o.usage?.cache_read_input_tokens??0),cacheCreationTokens:o.usage?.cache_creation_input_tokens,cacheReadTokens:o.usage?.cache_read_input_tokens},{results:s}}if(e.type==="content_block_start"){let{content_block:o}=e;if(o.type==="text"){let s=[];if(Array.isArray(o.citations))for(let a of o.citations)a?.url&&s.push({url:String(a.url),title:typeof a.title=="string"?a.title:void 0,snippet:typeof a.cited_text=="string"?a.cited_text:void 0});return{results:[{index:r,content:o.text,...s.length?{citations:s}:{}}]}}if(o.type==="thinking")return this.currentPromptConfig?.thinkingTokenBudget!=="none"&&this.currentPromptConfig?.showThoughts!==!1?{results:[{index:r,thought:o.thinking,thoughtBlocks:[{data:o.thinking,encrypted:!1}]}]}:{results:[{index:r,content:""}]};if(o.type==="tool_use"&&typeof o.id=="string"&&typeof e.index=="number"&&!n.indexIdMap[e.index]){n.indexIdMap[e.index]=o.id;let s=[{id:o.id,type:"function",function:{name:o.name,params:""}}];return{results:[{index:r,functionCalls:s}]}}if(o.type==="web_search_tool_result"||o.type==="server_tool_use")return{results:[{index:r,content:""}]}}if(e.type==="content_block_delta"){let{delta:o}=e;if(o.type==="citations_delta"){let s=o.citation;if(s&&typeof s.url=="string"&&s.url.length>0){let a=[{url:String(s.url),title:typeof s.title=="string"?s.title:void 0,snippet:typeof s.cited_text=="string"?s.cited_text:void 0}];return{results:[{index:r,content:"",citations:a}]}}return{results:[{index:r,content:""}]}}if(o.type==="text_delta"){let s=[];if(Array.isArray(o.citations))for(let a of o.citations)a?.url&&s.push({url:String(a.url),title:typeof a.title=="string"?a.title:void 0,snippet:typeof a.cited_text=="string"?a.cited_text:void 0});return{results:[{index:r,content:o.text,...s.length?{citations:s}:{}}]}}if(o.type==="thinking_delta")return this.currentPromptConfig?.thinkingTokenBudget!=="none"&&this.currentPromptConfig?.showThoughts!==!1?{results:[{index:r,thought:o.thinking,thoughtBlocks:[{data:o.thinking,encrypted:!1}]}]}:{results:[{index:r,content:""}]};if(o.type==="signature_delta")return{results:[{index:r,thoughtBlocks:[{data:"",encrypted:!1,signature:o.signature}]}]};if(o.type==="input_json_delta"){let s=n.indexIdMap[e.index];if(!s)return{results:[{index:r,content:""}]};let a=[{id:s,type:"function",function:{name:"",params:o.partial_json}}];return{results:[{index:r,functionCalls:a}]}}}if(e.type==="message_delta"){let{delta:o,usage:s}=e;return this.tokensUsed={promptTokens:this.tokensUsed?.promptTokens??0,completionTokens:s.output_tokens,totalTokens:(this.tokensUsed?.promptTokens??0)+s.output_tokens+(this.tokensUsed?.cacheCreationTokens??0)+(this.tokensUsed?.cacheReadTokens??0),cacheCreationTokens:this.tokensUsed?.cacheCreationTokens,cacheReadTokens:this.tokensUsed?.cacheReadTokens},{results:[{index:r,content:"",finishReason:wa(o.stop_reason)}]}}return{results:[{index:r,content:""}]}};supportsImplicitCaching=()=>!0},Vt=class i extends Ee{static create(e){return new i(e)}constructor({apiKey:e,projectId:t,region:n,config:r,options:o,models:s}){let a=t!==void 0&&n!==void 0,l,u;if(a){if(!e)throw new Error("Anthropic Vertex API key not set");if(typeof e!="function")throw new Error("Anthropic Vertex API key must be a function for token-based authentication");l=`https://${n==="global"?"aiplatform":`${n}-aiplatform`}.googleapis.com/v1/projects/${t}/locations/${n}/publishers/anthropic/`,u=async()=>({Authorization:`Bearer ${await e()}`,"anthropic-beta":"web-search-2025-03-05"})}else{if(!e)throw new Error("Anthropic API key not set");l="https://api.anthropic.com/v1",u=async()=>({"anthropic-version":"2023-06-01","anthropic-beta":"structured-outputs-2025-11-13, web-search-2025-03-05","x-api-key":typeof e=="function"?await e():e})}let c={...Ms(),...r},p=new Os(c,a),d=g=>{let h=rt({model:g,modelInfo:Yn,models:s});return{functions:!0,streaming:!0,hasThinkingBudget:h?.supported?.thinkingBudget??!1,hasShowThoughts:h?.supported?.showThoughts??!1,structuredOutputs:h?.supported?.structuredOutputs??!1,functionCot:!0,media:{images:{supported:!0,formats:["image/jpeg","image/png","image/gif","image/webp"],maxSize:5*1024*1024,detailLevels:["high","low","auto"]},audio:{supported:!1,formats:[],maxDuration:0},files:{supported:!1,formats:[],maxSize:0,uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!0,types:["ephemeral"],cacheBreakpoints:!1},thinking:h?.supported?.thinkingBudget??!1,multiTurn:!0}},m=s?.map(g=>{let h=g,f=h?.config;if(!f)return g;let b={};f.maxTokens!==void 0&&(b.maxTokens=f.maxTokens),f.temperature!==void 0&&(b.temperature=f.temperature),f.topP!==void 0&&(b.topP=f.topP),f.topK!==void 0&&(b.topK=f.topK),f.presencePenalty!==void 0&&(b.presencePenalty=f.presencePenalty),f.frequencyPenalty!==void 0&&(b.frequencyPenalty=f.frequencyPenalty),f.stopSequences!==void 0&&(b.stopSequences=f.stopSequences),f.endSequences!==void 0&&(b.endSequences=f.endSequences),f.stream!==void 0&&(b.stream=f.stream),f.n!==void 0&&(b.n=f.n);let x={...h};Object.keys(b).length>0&&(x.modelConfig={...h.modelConfig??{},...b});let A=f.thinking?.thinkingTokenBudget;if(typeof A=="number"){let w=c.thinkingTokenBudgetLevels,I=[["minimal",w?.minimal??1024],["low",w?.low??5e3],["medium",w?.medium??1e4],["high",w?.high??2e4],["highest",w?.highest??32e3]],T="minimal",v=Number.POSITIVE_INFINITY;for(let[F,E]of I){let D=Math.abs(A-E);D<v&&(v=D,T=F)}x.thinkingTokenBudget=T}return f.thinking?.includeThoughts!==void 0&&(x.showThoughts=!!f.thinking.includeThoughts),x});super(p,{name:"Anthropic",apiURL:l,headers:u,modelInfo:Yn,defaults:{model:c.model},options:o,supportFor:d,models:m??s})}};function oc(i,e){let t=i.map(r=>{switch(r.role){case"function":return{role:"user",content:[{type:"tool_result",content:r.result,tool_use_id:r.functionId,...r.isError?{is_error:!0}:{},...r.cache?{cache:{type:"ephemeral"}}:{}}]};case"user":return typeof r.content=="string"?{role:"user",content:r.content,...r.cache?{cache_control:{type:"ephemeral"}}:{}}:{role:"user",content:r.content.map(s=>{switch(s.type){case"text":return{type:"text",text:s.text,...s.cache?{cache_control:{type:"ephemeral"}}:{}};case"image":return{type:"image",source:{type:"base64",media_type:s.mimeType,data:s.image},...s.cache?{cache_control:{type:"ephemeral"}}:{}};default:throw new Error("Invalid content type")}})};case"assistant":{let o="",s=[],a=r.thoughtBlocks;if(Array.isArray(a)&&a.length>0)for(let l of a)l.encrypted?s.push(l.signature?{type:"redacted_thinking",data:l.data,signature:l.signature}:{type:"redacted_thinking",data:l.data}):s.push(l.signature?{type:"thinking",thinking:l.data,signature:l.signature}:{type:"thinking",thinking:l.data});if(typeof r.content=="string"&&(s.length>0?o=[...s,{type:"text",text:r.content}]:o=r.content),typeof r.functionCalls<"u"&&(o=r.functionCalls.map(l=>{let u={};if(typeof l.function.params=="string"){let c=l.function.params;if(c.trim().length===0)u={};else try{u=JSON.parse(c)}catch{throw new Error(`Failed to parse function params JSON: ${c}`)}}else typeof l.function.params=="object"&&(u=l.function.params);return{type:"tool_use",id:l.id,name:l.function.name,input:u,...r.cache?{cache_control:{type:"ephemeral"}}:{}}}),Array.isArray(o)&&s.length>0&&(o=[...s,...o])),r.cache){if(typeof o=="string")o=[{type:"text",text:o,cache_control:{type:"ephemeral"}}];else if(Array.isArray(o)&&o.length>0){let l=o.length-1,u=o[l];u&&u.type==="text"&&(o[l]={...u,cache_control:{type:"ephemeral"}})}}return{role:"assistant",content:o}}default:throw new Error("Invalid role")}}),n=sc(t);return ic(n)}function sc(i){let e=[];for(let[t,n]of i.entries()){if(n.role!=="assistant"){e.push(n);continue}if(t>0&&i.at(t-1)?.role==="assistant"){let r=e.pop();e.push({...r||{},...n})}else e.push(n)}return e}function ic(i){return i.map(e=>e.role==="assistant"&&typeof e.content=="string"?{...e,content:e.content.replace(/\s+$/,"")}:e)}function wa(i){if(i)switch(i){case"stop_sequence":return"stop";case"max_tokens":return"length";case"tool_use":return"function_call";case"end_turn":return"stop";default:return"stop"}}var Xn=(L=>(L.GPT4="gpt-4",L.GPT41="gpt-4.1",L.GPT41Mini="gpt-4.1-mini",L.GPT41Nano="gpt-4.1-nano",L.GPT4O="gpt-4o",L.GPT4OMini="gpt-4o-mini",L.GPT4ChatGPT4O="chatgpt-4o-latest",L.GPT4Turbo="gpt-4-turbo",L.GPT35Turbo="gpt-3.5-turbo",L.GPT35TurboInstruct="gpt-3.5-turbo-instruct",L.GPT35TextDavinci002="text-davinci-002",L.GPT3TextBabbage002="text-babbage-002",L.GPT3TextAda001="text-ada-001",L.GPT5="gpt-5",L.GPT5Nano="gpt-5-nano",L.GPT5Mini="gpt-5-mini",L.GPT5Chat="gpt-5-chat",L.GPT5ChatLatest="gpt-5-chat-latest",L.GPT5Codex="gpt-5-codex",L.GPT5Pro="gpt-5-pro",L.GPT51="gpt-5.1",L.GPT51ChatLatest="gpt-5.1-chat-latest",L.GPT51Codex="gpt-5.1-codex",L.GPT51CodexMini="gpt-5.1-codex-mini",L.GPT51CodexMax="gpt-5.1-codex-max",L.GPT52="gpt-5.2",L.GPT52ChatLatest="gpt-5.2-chat-latest",L.GPT52Codex="gpt-5.2-codex",L.GPT52Pro="gpt-5.2-pro",L.O1="o1",L.O1Mini="o1-mini",L.O3="o3",L.O3Mini="o3-mini",L.O4Mini="o4-mini",L))(Xn||{}),Jt=(n=>(n.TextEmbeddingAda002="text-embedding-ada-002",n.TextEmbedding3Small="text-embedding-3-small",n.TextEmbedding3Large="text-embedding-3-large",n))(Jt||{});var Qt=(_=>(_.GPT4="gpt-4",_.GPT41="gpt-4.1",_.GPT41Mini="gpt-4.1-mini",_.GPT41Nano="gpt-4.1-nano",_.GPT4O="gpt-4o",_.GPT4OMini="gpt-4o-mini",_.GPT4ChatGPT4O="chatgpt-4o-latest",_.GPT4Turbo="gpt-4-turbo",_.GPT35Turbo="gpt-3.5-turbo",_.GPT35TurboInstruct="gpt-3.5-turbo-instruct",_.GPT35TextDavinci002="text-davinci-002",_.GPT3TextBabbage002="text-babbage-002",_.GPT3TextAda001="text-ada-001",_.GPT5="gpt-5",_.GPT5Nano="gpt-5-nano",_.GPT5Mini="gpt-5-mini",_.GPT5Chat="gpt-5-chat",_.GPT5ChatLatest="gpt-5-chat-latest",_.GPT5Codex="gpt-5-codex",_.GPT5Pro="gpt-5-pro",_.GPT51="gpt-5.1",_.GPT51ChatLatest="gpt-5.1-chat-latest",_.GPT51Codex="gpt-5.1-codex",_.GPT51CodexMini="gpt-5.1-codex-mini",_.GPT51CodexMax="gpt-5.1-codex-max",_.GPT52="gpt-5.2",_.GPT52ChatLatest="gpt-5.2-chat-latest",_.GPT52Codex="gpt-5.2-codex",_.GPT52Pro="gpt-5.2-pro",_.O1Pro="o1-pro",_.O1="o1",_.O1Mini="o1-mini",_.O3Pro="o3-pro",_.O3="o3",_.O3Mini="o3-mini",_.O4Mini="o4-mini",_))(Qt||{});var Yt=[{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}],zr=[{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 ac=i=>{let e=["o1","o1-mini","o3","o3-mini","o4-mini","o1-pro","o3-pro"];return e.includes(i)||e.includes(i)},_t=()=>structuredClone({model:"gpt-5-mini",embedModel:"text-embedding-3-small",...be()}),Hr=()=>structuredClone({..._t(),model:"gpt-5"}),Kr=()=>structuredClone({model:"gpt-5-mini",embedModel:"text-embedding-3-small",...Le()}),Wr=()=>({..._t(),model:"gpt-5-nano"}),Es=class{constructor(e,t,n){this.config=e;this.streamingUsage=t;this.chatReqUpdater=n}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,endSequences:e.endSequences,topP:e.topP,n:e.n,stream:e.stream}}createChatReq=(e,t)=>{let n=e.model;if(!e.chatPrompt||e.chatPrompt.length===0)throw new Error("Chat prompt is empty");let r={name:"/chat/completions"},o=e.functions?.map(m=>({type:"function",function:{name:m.name,description:m.description,parameters:m.parameters}})),s=!e.functionCall&&e.functions&&e.functions.length>0?"auto":e.functionCall,a=lc(e),l=e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,u=e.modelConfig?.stream??this.config.stream,c=this.config.store,p=ac(n),d={model:n,messages:a,...e.responseFormat?{response_format:e.responseFormat.type==="json_schema"?{type:"json_schema",json_schema:e.responseFormat.schema}:e.responseFormat}:this.config?.responseFormat?{response_format:{type:this.config.responseFormat}}:{},...o?{tools:o}:{},...s?{tool_choice:s}:{},...p?{}:{...(e.modelConfig?.maxTokens??this.config.maxTokens)!==void 0?{max_completion_tokens:e.modelConfig?.maxTokens??this.config.maxTokens}:{},...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},...(e.modelConfig?.n??this.config.n)!==void 0?{n:e.modelConfig?.n??this.config.n}:{},...(e.modelConfig?.presencePenalty??this.config.presencePenalty)!==void 0?{presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty}:{},...l!==void 0?{frequency_penalty:l}:{}},...(e.modelConfig?.stopSequences??this.config.stop)&&(e.modelConfig?.stopSequences??this.config.stop).length>0?{stop:e.modelConfig?.stopSequences??this.config.stop}:{},...this.config.logitBias!==void 0?{logit_bias:this.config.logitBias}:{},...u&&this.streamingUsage?{stream:!0,stream_options:{include_usage:!0}}:{},...c?{store:c}:{},...this.config.serviceTier?{service_tier:this.config.serviceTier}:{},...this.config.user?{user:this.config.user}:{}};if(this.config.reasoningEffort&&(d.reasoning_effort=this.config.reasoningEffort),this.config.webSearchOptions&&(d.web_search_options={...this.config.webSearchOptions.searchContextSize&&{search_context_size:this.config.webSearchOptions.searchContextSize},...this.config.webSearchOptions.userLocation&&{user_location:{approximate:{type:"approximate",...this.config.webSearchOptions.userLocation.approximate.city&&{city:this.config.webSearchOptions.userLocation.approximate.city},...this.config.webSearchOptions.userLocation.approximate.country&&{country:this.config.webSearchOptions.userLocation.approximate.country},...this.config.webSearchOptions.userLocation.approximate.region&&{region:this.config.webSearchOptions.userLocation.approximate.region},...this.config.webSearchOptions.userLocation.approximate.timezone&&{timezone:this.config.webSearchOptions.userLocation.approximate.timezone}}}}}),t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"none":d.reasoning_effort=void 0;break;case"minimal":d.reasoning_effort="minimal";break;case"low":d.reasoning_effort="medium";break;case"medium":d.reasoning_effort="high";break;case"high":d.reasoning_effort="high";break;case"highest":d.reasoning_effort="high";break}if(!d.reasoning_effort&&t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"minimal":d.reasoning_effort="minimal";break;case"low":d.reasoning_effort="medium";break;case"medium":case"high":case"highest":d.reasoning_effort="high";break}return this.chatReqUpdater&&(d=this.chatReqUpdater(d)),[r,d]};createEmbedReq=e=>{let t=e.embedModel;if(!t)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let n={name:"/embeddings"},r={model:t,input:e.texts,dimensions:this.config.dimensions};return[n,r]};createChatResp(e){let{id:t,usage:n,choices:r,error:o}=e;if(o)throw o;return this.tokensUsed=n?{promptTokens:n.prompt_tokens,completionTokens:n.completion_tokens,totalTokens:n.total_tokens}:void 0,{results:r.map(a=>{if(a.message.refusal)throw new Se(a.message.refusal,e.model,e.id);let l=Sa(a.finish_reason),u=a.message.tool_calls?.map(({id:c,function:{arguments:p,name:d}})=>({id:c,type:"function",function:{name:d,params:p}}));return{index:a.index,id:`${a.index}`,content:a.message.content??void 0,thought:a.message.reasoning_content,citations:a.message.annotations?.filter(c=>c?.type==="url_citation"&&c.url_citation).map(c=>({url:c.url_citation?.url,title:c.url_citation?.title,description:c.url_citation?.description})),functionCalls:u,finishReason:l}}),remoteId:t}}createChatStreamResp(e,t){let{id:n,usage:r,choices:o}=e;this.tokensUsed=r?{promptTokens:r.prompt_tokens,completionTokens:r.completion_tokens,totalTokens:r.total_tokens}:void 0;let s=t;return s.indexIdMap||(s.indexIdMap={}),{results:o.map(({index:l,delta:{content:u,role:c,refusal:p,tool_calls:d,reasoning_content:m,annotations:g},finish_reason:h})=>{if(p)throw new Se(p,void 0,n);let f=Sa(h),b=d?.map(({id:x,index:A,function:{name:w,arguments:I}})=>{typeof x=="string"&&typeof A=="number"&&!s.indexIdMap[A]&&(s.indexIdMap[A]=x);let T=s.indexIdMap[A];return T?{id:T,type:"function",function:{name:w,params:I}}:null}).filter(x=>x!==null);return{index:l,content:u??void 0,role:c,thought:m,citations:g?.filter(x=>x?.type==="url_citation"&&x.url_citation).map(x=>({url:x.url_citation?.url,title:x.url_citation?.title,description:x.url_citation?.description})),functionCalls:b,finishReason:f,id:n}})}}createEmbedResp(e){let{data:t,usage:n}=e;return this.tokensUsed=n?{promptTokens:n.prompt_tokens,completionTokens:n.completion_tokens,totalTokens:n.total_tokens}:void 0,{embeddings:t.map(r=>r.embedding)}}},Sa=i=>{switch(i){case"stop":return"stop";case"length":return"length";case"content_filter":return"error";case"tool_calls":return"function_call"}};function lc(i){return i.chatPrompt.map(t=>{switch(t.role){case"system":return{role:"system",content:t.content};case"user":{let n=Array.isArray(t.content)?t.content.map(r=>{switch(r.type){case"text":return{type:"text",text:r.text};case"image":return{type:"image_url",image_url:{url:`data:${r.mimeType};base64,${r.image}`,details:r.details??"auto"}};case"audio":return{type:"input_audio",input_audio:{data:r.data,format:r.format==="wav"?"wav":void 0}};default:throw new Error("Invalid content type")}}):t.content;return{role:"user",...t.name?{name:t.name}:{},content:n}}case"assistant":{let n=t.functionCalls?.map(r=>({id:r.id,type:"function",function:{name:r.function.name,arguments:typeof r.function.params=="object"?JSON.stringify(r.function.params):r.function.params}}));if(n&&n.length>0)return{role:"assistant",...t.content?{content:t.content}:{},name:t.name,tool_calls:n};if(t.content===void 0)throw new Error("Assistant content is required when no tool calls are provided");return{role:"assistant",content:t.content,...t.name?{name:t.name}:{}}}case"function":return{role:"tool",content:t.result,tool_call_id:t.functionId};default:throw new Error("Invalid role")}})}var ke=class extends Ee{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o,models:s,chatReqUpdater:a,supportFor:l}){if(!e||e==="")throw new Error("OpenAI API key not set");let u=new Es(t,n?.streamingUsage??!0,a);super(u,{name:"OpenAI",apiURL:r||"https://api.openai.com/v1",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:o,defaults:{model:t.model,embedModel:t.embedModel},options:n,supportFor:l,models:s})}},Xt=class extends ke{constructor({apiKey:e,apiURL:t,config:n,options:r,models:o,modelInfo:s}){if(!e||e==="")throw new Error("OpenAI API key not set");s=[...Yt,...s??[]];let a=u=>{let c=rt({model:u,modelInfo:s,models:o});return{functions:!0,streaming:!0,hasThinkingBudget:c?.supported?.thinkingBudget??!1,hasShowThoughts:c?.supported?.showThoughts??!1,structuredOutputs:c?.supported?.structuredOutputs??!1,media:{images:{supported:!0,formats:["image/jpeg","image/png","image/gif","image/webp"],maxSize:20*1024*1024,detailLevels:["high","low","auto"]},audio:{supported:!0,formats:["wav","mp3","ogg"],maxDuration:25*60},files:{supported:!0,formats:["text/plain","application/pdf","image/jpeg","image/png"],maxSize:512*1024*1024,uploadMethod:"upload"},urls:{supported:!1,webSearch:!0,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:c?.supported?.thinkingBudget??!1,multiTurn:!0}},l=o?.map(u=>{let c=u,p=c?.config;if(!p)return u;let d={};p.maxTokens!==void 0&&(d.maxTokens=p.maxTokens),p.temperature!==void 0&&(d.temperature=p.temperature),p.topP!==void 0&&(d.topP=p.topP),p.presencePenalty!==void 0&&(d.presencePenalty=p.presencePenalty),p.frequencyPenalty!==void 0&&(d.frequencyPenalty=p.frequencyPenalty);let m=p.stopSequences??p.stop;m!==void 0&&(d.stopSequences=m),p.n!==void 0&&(d.n=p.n),p.stream!==void 0&&(d.stream=p.stream);let g={...c};Object.keys(d).length>0&&(g.modelConfig={...c.modelConfig??{},...d});let h=p?.thinking?.thinkingTokenBudget;if(typeof h=="number"){let f=[["minimal",200],["low",800],["medium",5e3],["high",1e4],["highest",24500]],b="minimal",x=Number.POSITIVE_INFINITY;for(let[A,w]of f){let I=Math.abs(h-w);I<x&&(x=I,b=A)}g.thinkingTokenBudget=b}return p?.thinking?.includeThoughts!==void 0&&(g.showThoughts=!!p.thinking.includeThoughts),g});super({apiKey:e,apiURL:t,config:{..._t(),...n},options:r,modelInfo:s,models:l??o,supportFor:a}),super.setName("OpenAI")}};var uc=i=>{let e=i.trim();if(!e)return e;if(e.includes("api-version=")){let t=e.indexOf("api-version="),n=e.slice(t);return new URLSearchParams(n).get("api-version")??e}return e},cc=i=>{let e=i.match(/^(\d{4}-\d{2}-\d{2})/);return e?e[1]>="2024-08-01":!1},Ps=_t,va=Kr,ka=Wr,Oa=Hr,Zt=class extends ke{constructor({apiKey:e,resourceName:t,deploymentName:n,version:r="api-version=2024-02-15-preview",config:o,options:s,models:a,modelInfo:l}){if(!e||e==="")throw new Error("Azure OpenAPI API key not set");if(!t||t==="")throw new Error("Azure OpenAPI resource name not set");if(!n||n==="")throw new Error("Azure OpenAPI deployment id not set");let u={...Ps(),...o},c=uc(r),p=cc(c);l=[...Yt,...l??[]];let d=g=>{let h=rt({model:g,modelInfo:l,models:a});return{functions:!0,streaming:!0,hasThinkingBudget:h?.supported?.thinkingBudget??!1,hasShowThoughts:h?.supported?.showThoughts??!1,structuredOutputs:p&&(h?.supported?.structuredOutputs??!1),functionCot:!1,media:{images:{supported:!0,formats:["image/jpeg","image/png","image/gif","image/webp"],maxSize:20*1024*1024,detailLevels:["high","low","auto"]},audio:{supported:!1,formats:[],maxDuration:0},files:{supported:!1,formats:[],maxSize:0,uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:h?.supported?.thinkingBudget??!1,multiTurn:!0}};super({apiKey:e,config:u,options:s,models:a,modelInfo:l,supportFor:d});let m=t.includes("://")?t:`https://${t}.openai.azure.com/`;super.setName("Azure OpenAI"),super.setAPIURL(new URL(`/openai/deployments/${n}?api-version=${c}`,m).href),super.setHeaders(async()=>({"api-key":e}))}};var Vr=class i{services;currentServiceIndex=0;currentService;debug;initialBackoffMs;maxBackoffMs;maxRetries;serviceFailures=new Map;constructor(e,t){if(e.length===0)throw new Error("No AI services provided.");pc(e),this.services=[...e].sort(t?.comparator??i.metricComparator);let n=this.services[this.currentServiceIndex];if(n===void 0)throw new Error("Error initializing the AI services.");this.currentService=n,this.debug=t?.debug??!0,this.initialBackoffMs=t?.initialBackoffMs??1e3,this.maxBackoffMs=t?.maxBackoffMs??32e3,this.maxRetries=t?.maxRetries??3}static create(e,t){return new i(e,t)}getLastUsedChatModel(){return this.currentService.getLastUsedChatModel()}getLastUsedEmbedModel(){return this.currentService.getLastUsedEmbedModel()}getLastUsedModelConfig(){return this.currentService.getLastUsedModelConfig()}static inputOrderComparator=()=>0;static metricComparator=(e,t)=>{let n=e.getMetrics(),r=t.getMetrics();return n.latency.chat.mean-r.latency.chat.mean};getModelList(){for(let e of this.services){let t=e.getModelList();if(t)return t}}getNextService(e,t){let n=t+1,r=e[n];return r===void 0?{service:void 0,index:n}:{service:r,index:n}}reset(){this.currentServiceIndex=0;let e=this.services[this.currentServiceIndex];if(e===void 0)throw new Error("No AI services provided.");this.currentService=e}getName(){return this.currentService.getName()}getId(){return this.currentService.getId()}getFeatures(e){let t={functions:!1,streaming:!1,thinking:!1,multiTurn:!1,structuredOutputs:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]}};for(let n of this.services){let r=n.getFeatures(e);r.functions&&(t.functions=!0),r.streaming&&(t.streaming=!0),r.thinking&&(t.thinking=!0),r.multiTurn&&(t.multiTurn=!0),r.structuredOutputs&&(t.structuredOutputs=!0),r.functionCot&&(t.functionCot=!0),r.hasThinkingBudget&&(t.hasThinkingBudget=!0),r.hasShowThoughts&&(t.hasShowThoughts=!0),r.media.images.supported&&(t.media.images.supported=!0),t.media.images.formats=Array.from(new Set([...t.media.images.formats,...r.media.images.formats])),r.media.audio.supported&&(t.media.audio.supported=!0),t.media.audio.formats=Array.from(new Set([...t.media.audio.formats,...r.media.audio.formats])),r.media.files.supported&&(t.media.files.supported=!0),t.media.files.formats=Array.from(new Set([...t.media.files.formats,...r.media.files.formats])),r.media.files.uploadMethod!=="none"&&(t.media.files.uploadMethod=r.media.files.uploadMethod),r.media.urls.supported&&(t.media.urls.supported=!0),r.media.urls.webSearch&&(t.media.urls.webSearch=!0),r.media.urls.contextFetching&&(t.media.urls.contextFetching=!0),r.caching.supported&&(t.caching.supported=!0),t.caching.types=Array.from(new Set([...t.caching.types,...r.caching.types]))}return t}getMetrics(){let e={latency:{chat:{mean:0,p95:0,p99:0,samples:[]},embed:{mean:0,p95:0,p99:0,samples:[]}},errors:{chat:{count:0,rate:0,total:0},embed:{count:0,rate:0,total:0}}},t=0,n=0,r=0,o=0;for(let s of this.services){let a=s.getMetrics();e.errors.chat.count+=a.errors.chat.count,e.errors.chat.total+=a.errors.chat.total,e.errors.embed.count+=a.errors.embed.count,e.errors.embed.total+=a.errors.embed.total;let l=a.latency.chat.samples.length;l>0&&(t+=a.latency.chat.mean*l,n+=l);let u=a.latency.embed.samples.length;u>0&&(r+=a.latency.embed.mean*u,o+=u)}e.errors.chat.total>0&&(e.errors.chat.rate=e.errors.chat.count/e.errors.chat.total),e.errors.embed.total>0&&(e.errors.embed.rate=e.errors.embed.count/e.errors.embed.total),n>0&&(e.latency.chat.mean=t/n),o>0&&(e.latency.embed.mean=r/o);for(let s of this.services){let a=s.getMetrics();e.latency.chat.p95=Math.max(e.latency.chat.p95,a.latency.chat.p95),e.latency.chat.p99=Math.max(e.latency.chat.p99,a.latency.chat.p99),e.latency.embed.p95=Math.max(e.latency.embed.p95,a.latency.embed.p95),e.latency.embed.p99=Math.max(e.latency.embed.p99,a.latency.embed.p99)}return e}canRetryService(e){let t=this.serviceFailures.get(e.getId());if(!t)return!0;let{retries:n,lastFailureTime:r}=t,o=Date.now()-r,s=Math.min(this.initialBackoffMs*2**n,this.maxBackoffMs);return o>=s}handleFailure(e,t){let r=(this.serviceFailures.get(e.getId())?.retries??0)+1;this.serviceFailures.set(e.getId(),{retries:r,lastFailureTime:Date.now()}),this.debug&&console.warn(`AxBalancer: Service ${e.getName()} failed (retry ${r}/${this.maxRetries})`,t)}handleSuccess(e){this.serviceFailures.delete(e.getId())}async chat(e,t){let n=e.responseFormat?.type==="json_schema",r=e.capabilities,o=r?.requiresImages,s=r?.requiresAudio,a=this.services,l=e.model;if((n||o||s)&&(a=this.services.filter(p=>{let d=p.getFeatures(l);return!(n&&!d.structuredOutputs||o&&!d.media.images.supported||s&&!d.media.audio.supported)}),a.length===0)){let p=[];throw n&&p.push("structured outputs"),o&&p.push("images"),s&&p.push("audio"),new Error(`No services available that support required capabilities: ${p.join(", ")}.`)}let u=0,c=a[u];if(!c)throw new Error("No matching AI services available for request.");for(this.currentService=c;;){if(!this.canRetryService(c)){let p=this.getNextService(a,u);if(!p.service)throw new Error(`All candidate services exhausted (tried ${a.length} service(s))`);c=p.service,u=p.index,this.currentService=c;continue}try{let p=await c.chat(e,t);return this.handleSuccess(c),p}catch(p){if(!(p instanceof He))throw p;switch(p.constructor){case St:throw p;case Qe:{if(![408,429,500,502,503,504].includes(p.status))throw p;break}case De:break;case Rt:break;case ot: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 He)||r instanceof Qe&&![408,429,500,502,503,504].includes(r.status)||r instanceof St)throw r;if(this.handleFailure(this.currentService,r),(this.serviceFailures.get(this.currentService.getId())?.retries??0)>=this.maxRetries){let s=this.getNextService(this.services,n);if(!s.service)throw r;this.currentService=s.service,n=s.index,this.currentServiceIndex=n}}}}setOptions(e){for(let t of this.services)t.setOptions(e);this.currentService.setOptions(e),this.debug=e.debug??this.debug}getOptions(){return this.currentService.getOptions()}getLogger(){return this.currentService.getLogger()}};function pc(i){let e=i.find(r=>r.getModelList()!==void 0);if(!e)return;let t=e.getModelList();if(!t)throw new Error("No model list found in any service.");let n=new Set(t.map(r=>r.key));for(let r=0;r<i.length;r++){let o=i[r];if(!o)throw new Error(`Service at index ${r} is undefined`);let s=o.getModelList();if(!s)throw new Error(`Service at index ${r} (${o.getName()}) has no model list while another service does.`);let a=new Set(s.map(l=>l.key));for(let l of n)if(!a.has(l))throw new Error(`Service at index ${r} (${o.getName()}) is missing model "${l}"`);for(let l of a)if(!n.has(l))throw new Error(`Service at index ${r} (${o.getName()}) has extra model "${l}"`)}}function Nt(i){let e=!1,t=!1,n=!1,r=!1,o=!1,s=!1,a=!1,l=new Set,u=0;if(i.chatPrompt&&Array.isArray(i.chatPrompt))for(let c of i.chatPrompt){if(c.role==="user"&&Array.isArray(c.content))for(let p of c.content)switch(l.add(p.type),p.type){case"image":e=!0,p.cache&&(a=!0),u+=85;break;case"audio":t=!0,p.cache&&(a=!0),u+=p.duration||60;break;case"file":n=!0,p.cache&&(a=!0),u+=Math.ceil((p.extractedText?.length||1e3)/4);break;case"url":r=!0,p.cache&&(a=!0),u+=Math.ceil((p.cachedContent?.length||2e3)/4);break;case"text":p.cache&&(a=!0),u+=Math.ceil(p.text.length/4);break}else"content"in c&&typeof c.content=="string"&&(u+=Math.ceil(c.content.length/4));"cache"in c&&c.cache&&(a=!0)}return i.functions&&i.functions.length>0&&(o=!0),i.modelConfig?.stream===!0&&(s=!0),i.capabilities&&(i.capabilities.requiresImages&&(e=!0),i.capabilities.requiresAudio&&(t=!0),i.capabilities.requiresFiles&&(n=!0),i.capabilities.requiresWebSearch&&(r=!0)),{hasImages:e,hasAudio:t,hasFiles:n,hasUrls:r,requiresFunctions:o,requiresStreaming:s,requiresCaching:a,contentTypes:l,estimatedTokens:u}}function Fs(i,e){let t=i.getFeatures(),n=[],r=[],o=[];if(e.hasImages&&!t.media.images.supported&&(n.push("Image support"),o.push("Use altText for images or imageToText service")),e.hasAudio&&!t.media.audio.supported&&(n.push("Audio support"),o.push("Pre-transcribe audio or use transcription field")),e.hasFiles&&!t.media.files.supported&&(n.push("File support"),o.push("Pre-extract text content or use extractedText field")),e.hasUrls&&!t.media.urls.supported&&(n.push("URL/Web search support"),o.push("Pre-fetch content or use cachedContent field")),e.requiresFunctions&&!t.functions&&n.push("Function calling"),e.requiresStreaming&&!t.streaming&&(n.push("Streaming responses"),o.push("Use non-streaming mode")),e.requiresCaching&&!t.caching.supported&&(n.push("Content caching"),o.push("Repeated content will not be cached")),e.hasImages&&t.media.images.supported){let a=t.media.images.maxSize;a&&a<10*1024*1024&&r.push(`Image size limit is ${Math.round(a/(1024*1024))}MB`)}if(e.hasAudio&&t.media.audio.supported){let a=t.media.audio.maxDuration;a&&a<600&&r.push(`Audio duration limit is ${Math.round(a/60)} minutes`)}return{isSupported:n.length===0,missingCapabilities:n,warnings:r,alternatives:o}}function Jr(i,e){return i.map(t=>{let n=t.getFeatures(),r=Fs(t,e),o=0,s=[];return o+=10,e.hasImages&&n.media.images.supported&&(o+=25,s.push("Images"),n.media.images.detailLevels?.includes("high")&&(o+=5),n.media.images.maxSize&&n.media.images.maxSize>10*1024*1024&&(o+=3)),e.hasAudio&&n.media.audio.supported&&(o+=25,s.push("Audio"),n.media.audio.maxDuration&&n.media.audio.maxDuration>600&&(o+=5)),e.hasFiles&&n.media.files.supported&&(o+=25,s.push("Files"),n.media.files.uploadMethod==="cloud"&&(o+=3)),e.hasUrls&&n.media.urls.supported&&(o+=25,s.push("URLs"),n.media.urls.webSearch&&(o+=5)),e.requiresFunctions&&n.functions&&(o+=15,s.push("Functions"),n.functionCot&&(o+=3)),e.requiresStreaming&&n.streaming&&(o+=10,s.push("Streaming")),e.requiresCaching&&n.caching.supported&&(o+=8,s.push("Caching"),n.caching.types.includes("persistent")&&(o+=3)),n.thinking&&(o+=2),n.multiTurn&&(o+=2),n.hasThinkingBudget&&(o+=1),n.hasShowThoughts&&(o+=1),o-=r.missingCapabilities.length*10,{provider:t,score:o,missingCapabilities:r.missingCapabilities,supportedCapabilities:s}}).sort((t,n)=>n.score-t.score)}function Qr(i,e,t={}){if(e.length===0)throw new Error("No providers available");let n=Nt(i),r=Jr(e,n);if(t.requireExactMatch){let o=r.filter(s=>s.missingCapabilities.length===0);if(o.length===0)throw new Error(`No providers fully support the request requirements: ${r[0]?.missingCapabilities.join(", ")||"unknown requirements"}`);return o[0].provider}if(!t.allowDegradation){let o=r[0];if(o.missingCapabilities.length>0)throw new Error(`Best available provider (${o.provider.getName()}) is missing: ${o.missingCapabilities.join(", ")}`)}return r[0].provider}function Ma(i,e){let t=Nt(i),n=Jr(e,t),r=n[0]?.provider||null,o=[t.hasImages&&"images",t.hasAudio&&"audio",t.hasFiles&&"files",t.hasUrls&&"URLs",t.requiresFunctions&&"functions",t.requiresStreaming&&"streaming",t.requiresCaching&&"caching"].filter(Boolean).length,s=r?n[0].supportedCapabilities.length:0,a=r?`${r.getName()} supports ${s}/${o} requirements (${Math.round(s/Math.max(o,1)*100)}% compatibility)`:"No suitable providers found";return{requirements:t,providerScores:n,recommendedProvider:r,summary:a}}function Ea(i,e){return i.filter(t=>t.getFeatures().media[e].supported)}function Pa(i,e){let t={};for(let n of i){let o=n.getFeatures().media[e];if(o.supported)for(let s of o.formats)t[s]||(t[s]=[]),t[s].push(n)}return t}var Zn=(r=>(r.CommandRPlus="command-r-plus",r.CommandR="command-r",r.Command="command",r.CommandLight="command-light",r))(Zn||{}),er=(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))(er||{});var Yr=[{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 dc(i){if(Array.isArray(i)){let t=["string","number","integer","boolean","object","array"].find(n=>i.includes(n));return t||(i.find(n=>n!=="null")??i[0]??"string")}return typeof i=="string"?i:"string"}var Ns=()=>structuredClone({model:"command-r-plus",embedModel:"embed-english-v3.0",...be()}),_a=()=>structuredClone({model:"command-r",embedModel:"embed-english-v3.0",...Le()}),_s=class{constructor(e){this.config=e}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,frequencyPenalty:e.frequencyPenalty,presencePenalty:e.presencePenalty,endSequences:e.endSequences,stopSequences:e.stopSequences,stream:e.stream,n:e.n}}createChatReq(e){let t=e.model,n=e.chatPrompt.at(-1),r=e.chatPrompt.slice(0,-1),o;n&&n.role==="user"&&typeof n.content=="string"&&(o=n?.content);let s=mc(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:dc(g.type),required:p.parameters.required?.includes(m)??!1};return{name:p.name,description:p.description,parameter_definitions:d}}),l=e.chatPrompt.filter(p=>p.role==="function").map(p=>{let d=a?.find(m=>m.name===p.functionId);if(!d)throw new Error("Function not found");return{call:{name:d.name,parameters:d.parameter_definitions},outputs:[{result:p.result??""}]}}),u={name:"/chat"},c={message:o,model:t,tools:a,...l&&!o?{tool_results:l}:{},chat_history:s,max_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},k:e.modelConfig?.topK??this.config.topK,...e.modelConfig?.topP!==void 0?{p:e.modelConfig.topP}:{},frequency_penalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,end_sequences:this.config.endSequences,stop_sequences:e.modelConfig?.stopSequences??this.config.stopSequences};return[u,c]}createEmbedReq=e=>{let t=e.embedModel;if(!t)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let n={name:"/embed"},r={model:t,texts:e.texts??[],input_type:"classification",truncate:""};return[n,r]};createChatResp=e=>{this.tokensUsed=e.meta.billed_units?{promptTokens:e.meta.billed_units.input_tokens,completionTokens:e.meta.billed_units.output_tokens,totalTokens:e.meta.billed_units.input_tokens+e.meta.billed_units.output_tokens}:void 0;let t;if("finish_reason"in e)switch(e.finish_reason){case"COMPLETE":t="stop";break;case"MAX_TOKENS":t="length";break;case"ERROR":throw new Error("Finish reason: ERROR");case"ERROR_TOXIC":throw new Error("Finish reason: CONTENT_FILTER");default:t="stop";break}let n;return"tool_calls"in e&&(n=e.tool_calls?.map(o=>({id:o.name,type:"function",function:{name:o.name,params:o.parameters}}))),{results:[{index:0,id:e.generation_id,content:e.text,functionCalls:n,finishReason:t}],remoteId:e.response_id}};createChatStreamResp=(e,t)=>{let n=t;e.event_type==="stream-start"&&(n.generation_id=e.generation_id),this.tokensUsed={promptTokens:0,completionTokens:e.meta.billed_units?.output_tokens??0,totalTokens:e.meta.billed_units?.output_tokens??0};let{results:r}=this.createChatResp(e),o=r[0];if(!o)throw new Error("No result");return o.id=n.generation_id??"",{results:r}};createEmbedResp(e){return{remoteId:e.id,embeddings:e.embeddings}}},en=class extends Ee{constructor({apiKey:e,config:t,options:n,models:r}){if(!e||e==="")throw new Error("Cohere API key not set");let o={...Ns(),...t},s=new _s(o),a=r?.map(l=>{let u=l,c=u?.config;if(!c)return l;let p={};c.maxTokens!==void 0&&(p.maxTokens=c.maxTokens),c.temperature!==void 0&&(p.temperature=c.temperature),c.topP!==void 0&&(p.topP=c.topP),c.topK!==void 0&&(p.topK=c.topK),c.presencePenalty!==void 0&&(p.presencePenalty=c.presencePenalty),c.frequencyPenalty!==void 0&&(p.frequencyPenalty=c.frequencyPenalty),c.stopSequences!==void 0&&(p.stopSequences=c.stopSequences),c.endSequences!==void 0&&(p.endSequences=c.endSequences),c.stream!==void 0&&(p.stream=c.stream),c.n!==void 0&&(p.n=c.n);let d={...u};return Object.keys(p).length>0&&(d.modelConfig={...u.modelConfig??{},...p}),d});super(s,{name:"Cohere",apiURL:"https://api.cohere.ai/v1",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:Yr,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 mc(i){return i.map(e=>{let t="";if(e.role==="system"||e.role==="assistant"||e.role==="user")if(typeof e.content=="string")t=e.content;else throw new Error("Multi-modal content not supported");switch(e.role){case"user":return{role:"USER",message:t};case"system":return{role:"SYSTEM",message:t};case"assistant":{let n=Fa(e.functionCalls);return{role:"CHATBOT",message:t,tool_calls:n}}case"function":{let n=i.map(s=>{if(s.role==="assistant")return s.functionCalls?.find(a=>a.id===e.functionId)}).filter(s=>s!==void 0),r=Fa(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 Fa(i){return i?.map(e=>{let t;if(typeof e.function.params=="string"){let n=e.function.params;if(n.trim().length===0)t={};else try{t=JSON.parse(n)}catch{throw new Error(`Failed to parse function params JSON: ${n}`)}}else t=e.function.params;return{name:e.function.name,parameters:t}})}var tr=(n=>(n.DeepSeekChat="deepseek-chat",n.DeepSeekCoder="deepseek-coder",n.DeepSeekReasoner="deepseek-reasoner",n))(tr||{});var Xr=[{name:"deepseek-chat",currency:"USD",promptTokenCostPer1M:.27,completionTokenCostPer1M:1.1},{name:"deepseek-reasoner",currency:"USD",promptTokenCostPer1M:.55,completionTokenCostPer1M:2.19}];var Ls=()=>structuredClone({model:"deepseek-chat",...be()}),Na=()=>structuredClone({model:"deepseek-coder",...Le()}),tn=class extends ke{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("DeepSeek API key not set");let s={...Ls(),...t};o=[...Xr,...o??[]],super({apiKey:e,config:s,options:n,apiURL:"https://api.deepseek.com",modelInfo:o,supportFor:{functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},models:r}),super.setName("DeepSeek")}};var nr=(A=>(A.Gemini31Pro="gemini-3.1-pro-preview",A.Gemini3Flash="gemini-3-flash-preview",A.Gemini3Pro="gemini-3-pro-preview",A.Gemini3ProImage="gemini-3-pro-image-preview",A.Gemini25Pro="gemini-2.5-pro",A.Gemini25Flash="gemini-2.5-flash",A.Gemini25FlashLite="gemini-2.5-flash-lite",A.Gemini20Flash="gemini-2.0-flash",A.Gemini20FlashLite="gemini-2.0-flash-lite",A.Gemini20ProExp="gemini-2.0-pro-exp-02-05",A.Gemini20FlashThinkingExp="gemini-2.0-flash-thinking-exp-01-21",A.Gemini1Pro="gemini-1.0-pro",A.Gemini15Flash="gemini-1.5-flash",A.Gemini15Flash002="gemini-1.5-flash-002",A.Gemini15Flash8B="gemini-1.5-flash-8b",A.Gemini15Pro="gemini-1.5-pro",A.GeminiFlashLatest="gemini-flash-latest",A.GeminiFlashLiteLatest="gemini-flash-lite-latest",A.GeminiProLatest="gemini-pro-latest",A))(nr||{}),Zr=(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))(Zr||{}),eo=(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))(eo||{}),to=(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))(to||{}),$s=(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))($s||{}),La=["gemini-3.1-pro-preview","gemini-3.1-pro-preview-customtools","gemini-3-flash-preview","gemini-3-pro-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 no=[{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-pro-preview",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:2,completionTokenCostPer1M:12,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 Lt=i=>i.includes("gemini-3"),$a=i=>i.includes("gemini-3")&&i.includes("pro"),ro=i=>{if(!i||typeof i!="object")return i;let e={...i};return delete e.additionalProperties,delete e.default,delete e.optional,delete e.maximum,delete e.oneOf,delete e.anyOf,Array.isArray(e.type)&&(e.type=e.type.includes("object")?"object":e.type[0]??"string"),e.properties&&typeof e.properties=="object"&&(e.properties=Object.fromEntries(Object.entries(e.properties).map(([t,n])=>[t,ro(n)]))),e.items&&(e.items=ro(e.items)),e},Ga=[{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"}],Ds=()=>structuredClone({model:"gemini-2.5-flash",embedModel:"text-embedding-005",safetySettings:Ga,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...be()}),Da=()=>structuredClone({model:"gemini-2.0-flash",embedModel:"text-embedding-005",safetySettings:Ga,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...Le()}),Gs=class{constructor(e,t,n,r,o){this.config=e;this.isVertex=t;this.endpointId=n;this.apiKey=r;this.options=o;if(!this.isVertex&&this.config.autoTruncate)throw new Error("Auto truncate is not supported for GoogleGemini");let s=this.config.model;if(Lt(s)){if(this.config.thinking?.thinkingTokenBudget!==void 0&&typeof this.config.thinking.thinkingTokenBudget=="number")throw new Error(`Gemini 3 models (${s}) do not support numeric thinkingTokenBudget. Use thinkingLevel ('low', 'medium', 'high') instead, or pass thinkingTokenBudget as a string level via options.`);if($a(s)&&this.config.thinking?.thinkingLevel){let a=this.config.thinking.thinkingLevel;if(a!=="low"&&a!=="high")throw new Error(`Gemini 3 Pro (${s}) only supports thinkingLevel 'low' or 'high', got '${a}'. Use 'low' for less thinking or 'high' for more thinking.`)}}}tokensUsed;models;setModels(e){this.models=e}getEffectiveMappings(e){let t=this.models?.find(n=>n.model===e);return{thinkingLevelMapping:{...this.config.thinkingLevelMapping,...t?.thinkingLevelMapping??{}},thinkingTokenBudgetLevels:{...this.config.thinkingTokenBudgetLevels,...t?.thinkingTokenBudgetLevels??{}}}}getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,endSequences:e.endSequences,stream:e.stream,n:e.n}}createChatReq=async(e,t)=>{let n=e.model,r=e.modelConfig?.stream??this.config.stream;if(!e.chatPrompt||e.chatPrompt.length===0)throw new Error("Chat prompt is empty");let o;if(this.endpointId?o={name:r?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:o={name:r?`/models/${n}:streamGenerateContent?alt=sse`:`/models/${n}:generateContent`},!this.isVertex){let x=r?"&":"?",A=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;o.name+=`${x}key=${A}`}let s=e.chatPrompt.filter(x=>x.role==="system").map(x=>x.content),a=s.length>0?{role:"user",parts:[{text:s.join(" ")}]}:void 0,l=[],u=e.chatPrompt.filter(x=>x.role!=="system");for(let x=0;x<u.length;x++){let A=u[x];switch(A.role){case"user":{let w=Array.isArray(A.content)?A.content.map((I,T)=>{switch(I.type){case"text":return{text:I.text};case"image":return{inlineData:{mimeType:I.mimeType,data:I.image}};case"audio":return{inlineData:{mimeType:`audio/${I.format??"mp3"}`,data:I.data}};case"file":return"fileUri"in I?{fileData:{mimeType:I.mimeType,fileUri:I.fileUri}}:{inlineData:{mimeType:I.mimeType,data:I.data}};default:throw new Error(`Chat prompt content type not supported (index: ${T})`)}}):[{text:A.content}];l.push({role:"user",parts:w});break}case"assistant":{let w=[],I=A.thoughtBlocks,T=A.functionCalls&&A.functionCalls.length>0,v=I?.[0],F=I?.map(D=>D.data).join("")??"",E=v?.signature;if(F&&w.push({...T?{}:{thought:!0},text:F,...E&&!T?{thought_signature:E}:{}}),A.functionCalls){let D=A.functionCalls.map((U,V)=>{let k;if(typeof U.function.params=="string"){let N=U.function.params;if(N.trim().length===0)k={};else try{k=JSON.parse(N)}catch{throw new Error(`Failed to parse function params JSON: ${N}`)}}else k=U.function.params;let $={functionCall:{name:U.function.name,args:k}};return E&&V===0&&($.thought_signature=E),$});w.push(...D)}if(A.content&&w.push({text:A.content}),w.length===0)throw new Error("Assistant content is empty");l.push({role:"model",parts:w});break}case"function":{let w=[],I=A,T=x;for(;;){if(!("functionId"in I))throw new Error(`Chat prompt functionId is empty (index: ${T})`);if(w.push({functionResponse:{name:I.functionId,response:{result:I.result}}}),T+1<u.length&&u[T+1].role==="function")T++,I=u[T];else break}x=T,l.push({role:"user",parts:w});break}default:throw new Error(`Invalid role: ${JSON.stringify(A)} (index: ${x})`)}}let c=[];if(e.functions&&e.functions.length>0){let x=e.functions.map(A=>{let w={type:"object",properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]},I=A.parameters?ro(A.parameters):void 0;return I===void 0||I&&typeof I=="object"&&Object.keys(I).length===0?I={...w}:I&&typeof I=="object"&&I.type==="object"&&(!("properties"in I)||!I.properties||Object.keys(I.properties).length===0)&&(I={...I,properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]}),{name:A.name,description:A.description,parameters:I}});c.push({function_declarations:x})}if(this.options?.codeExecution&&c.push({code_execution:{}}),this.options?.googleSearchRetrieval&&c.push({google_search_retrieval:{dynamic_retrieval_config:this.options.googleSearchRetrieval}}),this.options?.googleSearch&&c.push({google_search:{}}),this.options?.googleMaps){let x=this.options.googleMaps,A=x?.enableWidget!==void 0?{enableWidget:x.enableWidget}:{};c.push({google_maps:A})}this.options?.urlContext&&c.push({url_context:{}}),c.length===0&&(c=void 0);let p,d=Array.isArray(c)?c.some(x=>x&&Array.isArray(x.function_declarations)&&x.function_declarations.length>0):!1;if(e.functionCall)if(e.functionCall==="none")p={function_calling_config:{mode:"NONE"}};else if(e.functionCall==="auto")p={function_calling_config:{mode:"AUTO"}};else if(e.functionCall==="required")p={function_calling_config:{mode:"ANY"}};else{let x=e.functionCall.function?.name?{allowedFunctionNames:[e.functionCall.function.name]}:{};p={function_calling_config:{mode:"ANY"},...x}}else d&&(p={function_calling_config:{mode:"AUTO"}});this.options?.retrievalConfig&&(p={...p??{},retrievalConfig:{...this.options.retrievalConfig.latLng?{latLng:this.options.retrievalConfig.latLng}:{}}});let m={};if(this.config.thinking?.includeThoughts&&(m.includeThoughts=!0),this.config.thinking?.thinkingTokenBudget&&(m.thinkingBudget=this.config.thinking.thinkingTokenBudget),this.config.thinking?.thinkingLevel&&Lt(n)&&(m.thinkingLevel=this.config.thinking.thinkingLevel),t?.thinkingTokenBudget){let x=this.getEffectiveMappings(n),A=x.thinkingTokenBudgetLevels;if(Lt(n)){let I=$a(n),T=x.thinkingLevelMapping;if(t.thinkingTokenBudget==="none")m.thinkingLevel=T?.minimal??"minimal";else{let v=t.thinkingTokenBudget,F=T?.[v];F||(F=v==="highest"?"high":v),m.thinkingLevel=F}if(I&&m.thinkingLevel){let v=m.thinkingLevel;v!=="low"&&v!=="high"&&(m.thinkingLevel=v==="minimal"?"low":"high")}}else switch(t.thinkingTokenBudget){case"none":m.thinkingBudget=0,m.includeThoughts=!1,delete m.thinkingLevel;break;case"minimal":m.thinkingBudget=A?.minimal??200;break;case"low":m.thinkingBudget=A?.low??800;break;case"medium":m.thinkingBudget=A?.medium??5e3;break;case"high":m.thinkingBudget=A?.high??1e4;break;case"highest":m.thinkingBudget=A?.highest??24500;break}}m.thinkingLevel&&delete m.thinkingBudget,Lt(n)||delete m.thinkingLevel,Lt(n)&&delete m.thinkingBudget;let g=e.modelConfig?.maxTokens??this.config.maxTokens;if(m.thinkingLevel&&g!==void 0)throw new Error("Cannot set maxTokens when using thinkingLevel with Gemini models. When thinking is enabled, the model manages output tokens automatically. Remove the maxTokens setting or disable thinking.");t?.showThoughts!==void 0&&t?.thinkingTokenBudget!=="none"&&(m.includeThoughts=t.showThoughts);let h={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,temperature:e.modelConfig?.temperature??this.config.temperature,...e.modelConfig?.topP!==void 0?{topP:e.modelConfig.topP}:{},topK:e.modelConfig?.topK??this.config.topK,frequencyPenalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,candidateCount:1,stopSequences:e.modelConfig?.stopSequences??this.config.stopSequences,responseMimeType:"text/plain",...Object.keys(m).length>0?{thinkingConfig:m}:{}};if(Lt(n)&&(h.temperature===void 0||h.temperature<1)&&(h.temperature=1),e.responseFormat){if(h.responseMimeType="application/json",e.responseFormat.type==="json_schema"&&e.responseFormat.schema){let x=e.responseFormat.schema.schema||e.responseFormat.schema;h.responseSchema=ro(x)}}else this.config.responseFormat&&this.config.responseFormat==="json_object"&&(h.responseMimeType="application/json");let f=this.config.safetySettings;return[o,{contents:l,tools:c,toolConfig:p,systemInstruction:a,generationConfig:h,safetySettings:f}]};createEmbedReq=async e=>{let t=e.embedModel;if(!t)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let n,r;if(this.isVertex)this.endpointId?n={name:`/${this.endpointId}:predict`}:n={name:`/models/${t}:predict`},r={instances:e.texts.map(o=>({content:o,...this.config.embedType&&{taskType:this.config.embedType}})),parameters:{autoTruncate:this.config.autoTruncate,outputDimensionality:this.config.dimensions}};else{let o=typeof this.apiKey=="function"?this.apiKey():this.apiKey;n={name:`/models/${t}:batchEmbedContents?key=${o}`},r={requests:e.texts.map(s=>({model:`models/${t}`,content:{parts:[{text:s}]},outputDimensionality:this.config.dimensions,...this.config.embedType&&{taskType:this.config.embedType}}))}}return[n,r]};createChatResp=e=>{let t,n=e.candidates?.map(o=>{let s={index:0};switch(o.finishReason){case"MAX_TOKENS":s.finishReason="length";break;case"STOP":s.finishReason="stop";break;case"SAFETY":throw new Se("Content was blocked due to safety settings",void 0,void 0);case"RECITATION":throw new Se("Content was blocked due to recitation policy",void 0,void 0);case"MALFORMED_FUNCTION_CALL":throw new Se("Function call was malformed and blocked",void 0,void 0);case"UNEXPECTED_TOOL_CALL":throw new Se("Unexpected tool call",void 0,void 0);case"FINISH_REASON_UNSPECIFIED":throw new Se("Finish reason unspecified",void 0,void 0);case"BLOCKLIST":throw new Se("Content was blocked due to blocklist",void 0,void 0);case"PROHIBITED_CONTENT":throw new Se("Content was blocked due to prohibited content",void 0,void 0);case"SPII":throw new Se("Content was blocked due to SPII",void 0,void 0);case"OTHER":throw new Se("Other finish reason",void 0,void 0)}if(!o.content||!o.content.parts)return s;for(let u of o.content.parts){if("text"in u){if("thought"in u&&u.thought||u.thought===!0){s.thought=u.text;let c=u.thoughtSignature||u.thought_signature;s.thoughtBlocks||(s.thoughtBlocks=[]),s.thoughtBlocks.push({data:u.text,encrypted:!1,...c?{signature:c}:{}})}else s.content=u.text;continue}if("functionCall"in u){let c=u.thoughtSignature||u.thought_signature;if(c)if(!s.thoughtBlocks||s.thoughtBlocks.length===0)s.thoughtBlocks=[{data:"",encrypted:!1,signature:c}];else{let p=s.thoughtBlocks[s.thoughtBlocks.length-1];p&&!p.signature&&(p.signature=c)}s.functionCalls=[...s.functionCalls??[],{id:ze(),type:"function",function:{name:u.functionCall.name,params:u.functionCall.args}}]}}let a=o.citationMetadata?.citations;if(Array.isArray(a)&&a.length){let u=c=>c?`${c.year}-${String(c.month).padStart(2,"0")}-${String(c.day).padStart(2,"0")}`:void 0;s.citations=a.filter(c=>typeof c?.uri=="string").map(c=>({url:c.uri,title:c.title,license:c.license,publicationDate:u(c.publicationDate)}))}let l=o.groundingMetadata;if(l){if(Array.isArray(l.groundingChunks)){let u=l.groundingChunks.map(c=>c?.maps).filter(c=>c&&typeof c.uri=="string").map(c=>({url:c.uri,title:c.title}));u.length&&(s.citations=[...s.citations??[],...u])}typeof l.googleMapsWidgetContextToken=="string"&&(t=l.googleMapsWidgetContextToken)}return s});e.usageMetadata&&(this.tokensUsed={totalTokens:e.usageMetadata.totalTokenCount,promptTokens:e.usageMetadata.promptTokenCount,completionTokens:e.usageMetadata.candidatesTokenCount,thoughtsTokens:e.usageMetadata.thoughtsTokenCount,...e.usageMetadata.cachedContentTokenCount!==void 0?{cacheReadTokens:e.usageMetadata.cachedContentTokenCount}:{}});let r={results:n};return t&&(r.providerMetadata={...r.providerMetadata,google:{...r.providerMetadata?.google??{},mapsWidgetContextToken:t}}),r};createChatStreamResp=e=>this.createChatResp(e);createEmbedResp=e=>{let t;return this.isVertex?t=e.predictions.map(n=>n.embeddings.values):t=e.embeddings.map(n=>n.values),{embeddings:t}};supportsContextCache=e=>{let t=e;return La.some(n=>t.includes(n)||n.includes(t))};buildCacheCreateOp=(e,t)=>{let n=e.model,r=t.contextCache?.ttlSeconds??3600,{systemInstruction:o,contents:s}=this.extractCacheableContent(e.chatPrompt);if(!o&&(!s||s.length===0))return;let a={model:this.isVertex?n:`models/${n}`,ttl:`${r}s`,displayName:`ax-cache-${Date.now()}`};o&&(a.systemInstruction=o),s&&s.length>0&&(a.contents=s);let l;if(this.isVertex)l="/cachedContents";else{l="/cachedContents";let u=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;l+=`?key=${u}`}return{type:"create",apiConfig:{name:l},request:a,parseResponse:u=>{let c=u;if(c?.name)return{name:c.name,expiresAt:c.expireTime,tokenCount:c.usageMetadata?.totalTokenCount}}}};buildCacheUpdateTTLOp=(e,t)=>{let n={ttl:`${t}s`},r=`/${e}`;if(!this.isVertex&&this.apiKey){let o=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;r+=`?key=${o}`}return{type:"update",apiConfig:{name:r,headers:{"Content-Type":"application/json"}},request:n,parseResponse:o=>{let s=o;if(s?.name)return{name:s.name,expiresAt:s.expireTime,tokenCount:s.usageMetadata?.totalTokenCount}}}};buildCacheDeleteOp=e=>{let t=`/${e}`;if(!this.isVertex&&this.apiKey){let n=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;t+=`?key=${n}`}return{type:"delete",apiConfig:{name:t,headers:{"Content-Type":"application/json"}},request:{},parseResponse:()=>{}}};prepareCachedChatReq=async(e,t,n)=>{let r=e.model,o=e.modelConfig?.stream??this.config.stream,{dynamicContents:s,dynamicSystemInstruction:a}=this.extractDynamicContent(e.chatPrompt),l;if(this.endpointId?l={name:o?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:l={name:o?`/models/${r}:streamGenerateContent?alt=sse`:`/models/${r}:generateContent`},!this.isVertex){let d=o?"&":"?",m=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;l.name+=`${d}key=${m}`}let u={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,temperature:e.modelConfig?.temperature??this.config.temperature,...e.modelConfig?.topP!==void 0?{topP:e.modelConfig.topP}:{},topK:e.modelConfig?.topK??this.config.topK,frequencyPenalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,candidateCount:1,stopSequences:e.modelConfig?.stopSequences??this.config.stopSequences,responseMimeType:"text/plain"};Lt(r)&&(u.temperature===void 0||u.temperature<1)&&(u.temperature=1);let c=this.config.safetySettings,p={contents:s,cachedContent:n,generationConfig:u,safetySettings:c};return a&&(p.systemInstruction=a),{apiConfig:l,request:p}};extractCacheableContent(e){let t,n=[],r=-1;for(let o=e.length-1;o>=0;o--){let s=e[o];if("cache"in s&&s.cache){r=o;break}}for(let o=0;o<e.length;o++){let s=e[o];if(s.role==="system"){t={role:"user",parts:[{text:s.content}]};continue}if(r>=0&&o<=r)if(s.role==="user"){let a=[];if(typeof s.content=="string")a.push({text:s.content});else if(Array.isArray(s.content))for(let l of s.content)switch(l.type){case"text":a.push({text:l.text});break;case"image":a.push({inlineData:{mimeType:l.mimeType,data:l.image}});break;case"audio":a.push({inlineData:{mimeType:`audio/${l.format??"mp3"}`,data:l.data}});break;case"file":"fileUri"in l?a.push({fileData:{mimeType:l.mimeType,fileUri:l.fileUri}}):a.push({inlineData:{mimeType:l.mimeType,data:l.data}});break}a.length>0&&n.push({role:"user",parts:a})}else s.role==="assistant"&&s.content&&n.push({role:"model",parts:[{text:s.content}]})}return{systemInstruction:t,contents:n}}extractDynamicContent(e){let n=[];for(let r of e)if(r.role!=="system"&&!("cache"in r&&r.cache))if(r.role==="user"){let o=[];if(typeof r.content=="string")o.push({text:r.content});else if(Array.isArray(r.content)){for(let s of r.content)if(!("cache"in s&&s.cache))switch(s.type){case"text":o.push({text:s.text});break;case"image":o.push({inlineData:{mimeType:s.mimeType,data:s.image}});break;case"audio":o.push({inlineData:{mimeType:`audio/${s.format??"mp3"}`,data:s.data}});break;case"file":"fileUri"in s?o.push({fileData:{mimeType:s.mimeType,fileUri:s.fileUri}}):o.push({inlineData:{mimeType:s.mimeType,data:s.data}});break}}o.length>0&&n.push({role:"user",parts:o})}else if(r.role==="assistant"){let o=[];if(r.content&&o.push({text:r.content}),r.functionCalls)for(let s of r.functionCalls){let a;if(typeof s.function.params=="string")try{a=JSON.parse(s.function.params)}catch{a={}}else a=s.function.params??{};o.push({functionCall:{name:s.function.name,args:a}})}o.length>0&&n.push({role:"model",parts:o})}else r.role==="function"&&n.push({role:"user",parts:[{functionResponse:{name:r.functionId,response:{result:r.result}}}]});return{dynamicContents:n,dynamicSystemInstruction:void 0}}},nn=class i extends Ee{static create(e){return new i(e)}constructor({apiKey:e,projectId:t,region:n,endpointId:r,config:o,options:s,models:a,modelInfo:l}){let u=t!==void 0&&n!==void 0,c,p;if(u){if(!e)throw new Error("GoogleGemini Vertex API key not set");if(typeof e!="function")throw new Error("GoogleGemini Vertex API key must be a function for token-based authentication");let f;r?f="endpoints":f="publishers/google",c=`https://${n==="global"?"aiplatform":`${n}-aiplatform`}.googleapis.com/v1/projects/${t}/locations/${n}/${f}`,p=async()=>({Authorization:`Bearer ${typeof e=="function"?await e():e}`})}else{if(!e)throw new Error("GoogleGemini AI API key not set");c="https://generativelanguage.googleapis.com/v1beta",p=async()=>({})}let d={...Ds(),...o},m=new Gs(d,u,r,e,s);l=[...no,...l??[]];let g=f=>{let b=rt({model:f,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(f),types:["persistent"]},thinking:b?.supported?.thinkingBudget??!1,multiTurn:!0}},h=a?.map(f=>{let b=f,x=b?.config;if(!x)return f;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 w={...b};Object.keys(A).length>0&&(w.modelConfig={...b.modelConfig??{},...A});let I=x.thinking?.thinkingTokenBudget;if(typeof I=="number"){let T=d.thinkingTokenBudgetLevels,v=[["minimal",T?.minimal??200],["low",T?.low??800],["medium",T?.medium??5e3],["high",T?.high??1e4],["highest",T?.highest??24500]],F="minimal",E=Number.POSITIVE_INFINITY;for(let[D,U]of v){let V=Math.abs(I-U);V<E&&(E=V,F=D)}w.thinkingTokenBudget=F}return x.thinking?.includeThoughts!==void 0&&(w.showThoughts=!!x.thinking.includeThoughts),x.thinkingLevelMapping&&(w.thinkingLevelMapping=x.thinkingLevelMapping),x.thinkingTokenBudgetLevels&&(w.thinkingTokenBudgetLevels=x.thinkingTokenBudgetLevels),w});h?m.setModels(h):a&&m.setModels(a),super(m,{name:"GoogleGeminiAI",apiURL:c,headers:p,modelInfo:l,defaults:{model:d.model,embedModel:d.embedModel},options:s,supportFor:g,models:h??a})}};var gc=new Ye,rn=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(gc.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 rr=(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))(rr||{});var oo=[{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 hc=()=>structuredClone({model:"llama-3.3-70b-versatile",...be()}),on=class extends ke{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Groq API key not set");let s={...hc(),...t},a={...n,streamingUsage:!1};o=[...oo,...o??[]];let l={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0};super({apiKey:e,config:s,options:a,modelInfo:o,apiURL:"https://api.groq.com/openai/v1",models:r,supportFor:l}),super.setName("Groq"),this.setOptions(a)}setOptions=e=>{let t=this.newRateLimiter(e);super.setOptions({...e,rateLimiter:t})};newRateLimiter=e=>{if(e?.rateLimiter)return e.rateLimiter;let t=e?.tokensPerMinute??4800,n=new rn(t,t/60,{debug:e?.debug});return async(o,s)=>{let a=s.modelUsage?.tokens?.totalTokens||0;return await n.acquire(a),await o()}}};var so=[];var io=(e=>(e.MetaLlama270BChatHF="meta-llama/Llama-2-70b-chat-hf",e))(io||{});var Bs=()=>structuredClone({model:"meta-llama/Llama-2-70b-chat-hf",...be()}),Ua=()=>structuredClone({model:"meta-llama/Llama-2-70b-chat-hf",...Le()}),Us=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)}i(r)}};var ge={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"},mt={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"},bs=(r=>(r.COMPLETION="completion",r.CHAT="chat",r.RERANK="rerank",r.UNKNOWN="unknown",r))(bs||{}),Is=(o=>(o.WORKFLOW="workflow",o.TASK="task",o.AGENT="agent",o.TOOL="tool",o.UNKNOWN="unknown",o))(Is||{});var Cs=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()}},Dr=class extends TransformStream{constructor(e,t){super(new Cs(e,t))}};function Ur(i,e){for(let t of e){let n=i.find(r=>r.id===t.id);n?(typeof t.function.name=="string"&&t.function.name.length>0&&(n.function.name+=t.function.name),typeof t.function.params=="string"&&t.function.params.length>0&&(n.function.params+=t.function.params),typeof t.function.params=="object"&&(n.function.params=t.function.params)):i.push(t)}}var Ji=(i,e,t,n)=>{let r=n?i.filter(s=>s.role!=="system"):[...i];t({name:"ChatRequestChatPrompt",step:e,value:r})};var Qi=(i,e)=>{if(!i.results)return;let t={name:"ChatResponseResults",value:i.results};e(t)};function Yi(i,e){let t=new Map;for(let n of i)for(let r of n.results){if(!r)continue;let o=t.get(r.index);o?(r.content&&(o.content=(o.content??"")+r.content),r.thought&&(o.thought=(o.thought??"")+r.thought),r.finishReason&&(o.finishReason=r.finishReason),r.functionCalls&&(o.functionCalls?Ur(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 Xi=(i,e)=>{e({name:"FunctionResults",value:i})},Ts=(i,e,t,n)=>{n({name:"FunctionError",index:e,fixingInstructions:t,error:i})},Zi=(i,e,t,n)=>{n({name:"ValidationError",index:e,fixingInstructions:t,error:i})},ea=(i,e,t,n)=>{n({name:"AssertionError",index:e,fixingInstructions:t,error:i})},ta=(i,e,t)=>{t({name:"RefusalError",index:e,error:i})};var na=(i,e,t)=>{t({name:"EmbedRequest",embedModel:e,value:i})},ra=(i,e)=>{let t=i.slice(0,3).map(r=>({length:r.length,sample:r.slice(0,5),truncated:r.length>5})),n={name:"EmbedResponse",totalEmbeddings:i.length,value:t};e(n)},oa=(i,e,t,n)=>{n({name:"ResultPickerUsed",sampleCount:i,selectedIndex:e,latency:t})};var jr=i=>{let e={};for(let[t,n]of Object.entries(i))if(n!=null){let r=String(n);e[t]=r.length>100?r.substring(0,100):r}return e},Ft=(...i)=>{let e={};for(let t of i)t&&Object.assign(e,t);return e},Br,sa=i=>{if(Br)return Br;if(i)return Br=tc(i),Br};var tc=i=>({latencyHistogram:i.createHistogram("ax_llm_request_duration_ms",{description:"Duration of LLM requests in milliseconds",unit:"ms"}),errorCounter:i.createCounter("ax_llm_errors_total",{description:"Total number of LLM request errors"}),requestCounter:i.createCounter("ax_llm_requests_total",{description:"Total number of LLM requests"}),tokenCounter:i.createCounter("ax_llm_tokens_total",{description:"Total number of LLM tokens consumed"}),inputTokenCounter:i.createCounter("ax_llm_input_tokens_total",{description:"Total number of input/prompt tokens consumed"}),outputTokenCounter:i.createCounter("ax_llm_output_tokens_total",{description:"Total number of output/completion tokens generated"}),errorRateGauge:i.createGauge("ax_llm_error_rate",{description:"Current error rate as a percentage (0-100)"}),meanLatencyGauge:i.createGauge("ax_llm_mean_latency_ms",{description:"Mean latency of LLM requests in milliseconds",unit:"ms"}),p95LatencyGauge:i.createGauge("ax_llm_p95_latency_ms",{description:"95th percentile latency of LLM requests in milliseconds",unit:"ms"}),p99LatencyGauge:i.createGauge("ax_llm_p99_latency_ms",{description:"99th percentile latency of LLM requests in milliseconds",unit:"ms"}),streamingRequestsCounter:i.createCounter("ax_llm_streaming_requests_total",{description:"Total number of streaming LLM requests"}),functionCallsCounter:i.createCounter("ax_llm_function_calls_total",{description:"Total number of function/tool calls made"}),functionCallLatencyHistogram:i.createHistogram("ax_llm_function_call_latency_ms",{description:"Latency of function calls in milliseconds",unit:"ms"}),requestSizeHistogram:i.createHistogram("ax_llm_request_size_bytes",{description:"Size of LLM request payloads in bytes",unit:"By"}),responseSizeHistogram:i.createHistogram("ax_llm_response_size_bytes",{description:"Size of LLM response payloads in bytes",unit:"By"}),temperatureGauge:i.createGauge("ax_llm_temperature_gauge",{description:"Temperature setting used for LLM requests"}),maxTokensGauge:i.createGauge("ax_llm_max_tokens_gauge",{description:"Maximum tokens setting used for LLM requests"}),estimatedCostCounter:i.createCounter("ax_llm_estimated_cost_total",{description:"Estimated cost of LLM requests in USD",unit:"$"}),promptLengthHistogram:i.createHistogram("ax_llm_prompt_length_chars",{description:"Length of prompts in characters"}),contextWindowUsageGauge:i.createGauge("ax_llm_context_window_usage_ratio",{description:"Context window utilization ratio (0-1)"}),timeoutsCounter:i.createCounter("ax_llm_timeouts_total",{description:"Total number of timed out LLM requests"}),abortsCounter:i.createCounter("ax_llm_aborts_total",{description:"Total number of aborted LLM requests"}),thinkingBudgetUsageCounter:i.createCounter("ax_llm_thinking_budget_usage_total",{description:"Total thinking budget tokens used"}),multimodalRequestsCounter:i.createCounter("ax_llm_multimodal_requests_total",{description:"Total number of multimodal requests (with images/audio)"}),cacheReadTokensCounter:i.createCounter("ax_llm_cache_read_tokens_total",{description:"Total number of tokens read from cache (prompt caching)"}),cacheWriteTokensCounter:i.createCounter("ax_llm_cache_write_tokens_total",{description:"Total number of tokens written to cache (prompt caching)"})}),ia=(i,e,t,n,r,o)=>{try{if(i.latencyHistogram){let s=jr({operation:e,ai_service:n,...r?{model:r}:{},...o});i.latencyHistogram.record(t,s)}}catch(s){console.warn("Failed to record latency metric:",s)}},aa=(i,e,t,n,r,o,s,a)=>{let l={operation:e,ai_service:o,...s?{model:s}:{},...a};i.meanLatencyGauge&&i.meanLatencyGauge.record(t,l),i.p95LatencyGauge&&i.p95LatencyGauge.record(n,l),i.p99LatencyGauge&&i.p99LatencyGauge.record(r,l)},la=(i,e,t,n,r)=>{try{if(i.errorCounter){let o=jr({operation:e,ai_service:t,...n?{model:n}:{},...r});i.errorCounter.add(1,o)}}catch(o){console.warn("Failed to record error metric:",o)}},ua=(i,e,t,n,r,o)=>{i.errorRateGauge&&i.errorRateGauge.record(t*100,{operation:e,ai_service:n,...r?{model:r}:{},...o})},ca=(i,e,t,n,r)=>{i.requestCounter&&i.requestCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},Wn=(i,e,t,n,r,o)=>{try{let s=jr({ai_service:n,...r?{model:r}:{},...o});i.tokenCounter&&i.tokenCounter.add(t,{token_type:e,...s}),e==="input"&&i.inputTokenCounter&&i.inputTokenCounter.add(t,s),e==="output"&&i.outputTokenCounter&&i.outputTokenCounter.add(t,s)}catch(s){console.warn("Failed to record token metric:",s)}},pa=(i,e,t,n,r,o)=>{t&&i.streamingRequestsCounter&&i.streamingRequestsCounter.add(1,{operation:e,ai_service:n,...r?{model:r}:{},...o})},da=(i,e,t,n,r,o)=>{let s={function_name:e,...n?{ai_service:n}:{},...r?{model:r}:{},...o};i.functionCallsCounter&&i.functionCallsCounter.add(1,s),t&&i.functionCallLatencyHistogram&&i.functionCallLatencyHistogram.record(t,s)},ws=(i,e,t,n,r,o)=>{i.requestSizeHistogram&&i.requestSizeHistogram.record(t,{operation:e,ai_service:n,...r?{model:r}:{},...o})},Rs=(i,e,t,n,r,o)=>{i.responseSizeHistogram&&i.responseSizeHistogram.record(t,{operation:e,ai_service:n,...r?{model:r}:{},...o})},ma=(i,e,t,n,r,o)=>{let s={...n?{ai_service:n}:{},...r?{model:r}:{},...o};e!==void 0&&i.temperatureGauge&&i.temperatureGauge.record(e,s),t!==void 0&&i.maxTokensGauge&&i.maxTokensGauge.record(t,s)},Ss=(i,e,t,n,r,o)=>{i.estimatedCostCounter&&i.estimatedCostCounter.add(t,{operation:e,ai_service:n,...r?{model:r}:{},...o})},ga=(i,e,t,n,r)=>{i.promptLengthHistogram&&i.promptLengthHistogram.record(e,{ai_service:t,...n?{model:n}:{},...r})},ha=(i,e,t,n,r)=>{i.contextWindowUsageGauge&&i.contextWindowUsageGauge.record(e,{ai_service:t,...n?{model:n}:{},...r})},fa=(i,e,t,n,r)=>{i.timeoutsCounter&&i.timeoutsCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},xa=(i,e,t,n,r)=>{i.abortsCounter&&i.abortsCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},Aa=(i,e,t,n,r)=>{i.thinkingBudgetUsageCounter&&i.thinkingBudgetUsageCounter.add(e,{ai_service:t,...n?{model:n}:{},...r})},ya=(i,e,t,n,r,o)=>{(e||t)&&i.multimodalRequestsCounter&&i.multimodalRequestsCounter.add(1,{ai_service:n,has_images:e.toString(),has_audio:t.toString(),...r?{model:r}:{},...o})},vs=(i,e,t,n,r,o)=>{try{if(t<=0)return;let s=jr({ai_service:n,...r?{model:r}:{},...o});e==="read"&&i.cacheReadTokensCounter&&i.cacheReadTokensCounter.add(t,s),e==="write"&&i.cacheWriteTokensCounter&&i.cacheWriteTokensCounter.add(t,s)}catch(s){console.warn("Failed to record cache token metric:",s)}};function ba(i){try{return JSON.stringify(i,null,2)}catch{return String(i)}}function gt(i,e={}){let t=[i];throw e.fieldPath!==void 0&&t.push(`Field: ${e.fieldPath}`),e.value!==void 0&&t.push(`Value: ${ba(e.value)}`),e.note&&t.push(`Note: ${e.note}`),e.item!==void 0&&t.push(`Chat item: ${ba(e.item)}`),new Error(t.join(`
168
+ `))}function Ht(i){let e=n=>JSON.stringify(n,null,2);if(!i)throw new Error(`Chat request message item cannot be null or undefined, received: ${e(i)}`);let t=typeof i=="object"&&i!==null&&"role"in i&&typeof i.role=="string"?i.role:void 0;if(!t)throw new Error(`Chat request message must have a role, received: ${e(t)}`);switch(t){case"system":{let n=typeof i=="object"&&i!==null&&"content"in i&&typeof i.content=="string"?i.content:void 0;if(!n||n.trim()==="")throw new Error(`System message content cannot be empty or whitespace-only, received: ${e(n)}`);break}case"user":{let n=typeof i=="object"&&i!==null&&"content"in i?i.content:void 0;if(n===void 0)throw new Error(`User message content cannot be undefined, received: ${e(n)}`);if(typeof n=="string"){if(n.trim()==="")throw new Error(`User message content cannot be empty or whitespace-only, received: ${e(n)}`)}else if(Array.isArray(n)){if(n.length===0)throw new Error(`User message content array cannot be empty, received: ${e(n)}`);for(let r=0;r<n.length;r++){let o=n[r];if(!o||typeof o!="object")throw new Error(`User message content item at index ${r} must be an object, received: ${e(o)}`);let s=typeof o=="object"&&o!==null&&"type"in o&&typeof o.type=="string"?o.type:void 0;if(!s)throw new Error(`User message content item at index ${r} must have a type, received: ${e(s)}`);switch(s){case"text":{let a="text"in o&&typeof o.text=="string"?o.text:void 0;if(!a||a.trim()==="")throw new Error(`User message text content at index ${r} cannot be empty or whitespace-only, received: ${e(a)}`);break}case"image":{let a="image"in o&&typeof o.image=="string"?o.image:void 0,l="mimeType"in o&&typeof o.mimeType=="string"?o.mimeType:void 0;if(!a||a.trim()==="")throw new Error(`User message image content at index ${r} cannot be empty, received: ${e(a)}`);if(!l||l.trim()==="")throw new Error(`User message image content at index ${r} must have a mimeType, received: ${e(l)}`);break}case"audio":{let a="data"in o&&typeof o.data=="string"?o.data:void 0;if(!a||a.trim()==="")throw new Error(`User message audio content at index ${r} cannot be empty, received: ${e(a)}`);break}case"file":{let a="fileUri"in o&&typeof o.fileUri=="string",l="data"in o&&typeof o.data=="string";if(!a&&!l)throw new Error(`User message file content at index ${r} must have either 'data' or 'fileUri', received: ${e(o)}`);if(a&&l)throw new Error(`User message file content at index ${r} cannot have both 'data' and 'fileUri', received: ${e(o)}`);if(a){let c=o.fileUri;if(!c||c.trim()==="")throw new Error(`User message file content at index ${r} fileUri cannot be empty, received: ${e(c)}`)}if(l){let c=o.data;if(!c||c.trim()==="")throw new Error(`User message file content at index ${r} data cannot be empty, received: ${e(c)}`)}let u="mimeType"in o&&typeof o.mimeType=="string"?o.mimeType:null;if(!u||u.trim()==="")throw new Error(`User message file content at index ${r} must have a mimeType, received: ${e(u)}`);break}case"url":{let a="url"in o&&typeof o.url=="string"?o.url:void 0;if(!a||a.trim()==="")throw new Error(`User message url content at index ${r} cannot be empty, received: ${e(a)}`);break}default:throw new Error(`User message content item at index ${r} has unsupported type: ${e(s)}`)}}}else throw new Error(`User message content must be a string or array of content objects, received: ${e(n)}`);break}case"assistant":{let n=typeof i=="object"&&i!==null&&"content"in i?i.content:void 0,r=typeof i=="object"&&i!==null&&"functionCalls"in i?i.functionCalls:void 0,o=typeof i=="object"&&i!==null&&"thoughtBlocks"in i?i.thoughtBlocks:void 0,s=typeof n=="string"&&n.trim()!=="",a=Array.isArray(r)&&r.length>0,l=Array.isArray(o)&&o.length>0;if(!s&&!a&&!l&&gt("Assistant message must include non-empty content, at least one function call, or thought blocks",{fieldPath:"content | functionCalls | thoughtBlocks",value:{content:n,functionCalls:r,thoughtBlocks:o},item:i}),n!==void 0&&typeof n!="string"&&gt("Assistant message content must be a string",{fieldPath:"content",value:n,item:i}),r!==void 0&&!Array.isArray(r)&&gt("Assistant message functionCalls must be an array when provided",{fieldPath:"functionCalls",value:r,item:i}),Array.isArray(r))for(let u=0;u<r.length;u++){let c=r[u];if((!c||typeof c!="object")&&gt("functionCalls entry must be an object",{fieldPath:`functionCalls[${u}]`,value:c,item:i}),(!("id"in c)||typeof c.id!="string"||c.id.trim()==="")&&gt("functionCalls entry must include a non-empty string id",{fieldPath:`functionCalls[${u}].id`,value:c.id,item:i}),(!("type"in c)||c.type!=="function")&&gt("functionCalls entry must have type 'function'",{fieldPath:`functionCalls[${u}].type`,value:c.type,item:i}),!("function"in c)||!c.function)gt("functionCalls entry must include a function object",{fieldPath:`functionCalls[${u}].function`,value:c.function,item:i});else{let p=c.function;(!("name"in p)||typeof p.name!="string"||p.name.trim()==="")&&gt("functionCalls entry must include a non-empty function name",{fieldPath:`functionCalls[${u}].function.name`,value:p?.name,item:i}),p.params!==void 0&&typeof p.params!="string"&&typeof p.params!="object"&&gt("functionCalls entry params must be a string or object when provided",{fieldPath:`functionCalls[${u}].function.params`,value:p.params,item:i})}}if(i.name!==void 0){let u=i.name;(typeof u!="string"||u.trim()==="")&&gt("Assistant message name must be a non-empty string when provided",{fieldPath:"name",value:u,item:i})}break}case"function":{let n=typeof i=="object"&&i!==null&&"functionId"in i&&typeof i.functionId=="string"?i.functionId:void 0,r=typeof i=="object"&&i!==null&&"result"in i?i.result:void 0;if(!n||n.trim()==="")throw new Error(`Function message must have a non-empty functionId, received: ${e(n)}`);if(r==null)throw new Error(`Function message must have a result, received: ${e(r)}`);if(typeof r!="string")throw new Error(`Function message result must be a string, received: ${e(r)}`);i.isError!==void 0&&typeof i.isError!="boolean"&&gt("Function message isError must be a boolean when provided",{fieldPath:"isError",value:i.isError,item:i});break}default:throw new Error(`Unsupported message role: ${e(t)}`)}}function qr(i){let e=n=>JSON.stringify(n,null,2),t=Array.isArray(i)?i:[i];if(t.length===0)throw new Error(`Chat response results cannot be empty, received: ${e(t)}`);for(let n=0;n<t.length;n++){let r=t[n];if(!r)throw new Error(`Chat response result at index ${n} cannot be null or undefined, received: ${e(r)}`);if(typeof r.index!="number")throw new Error(`Chat response result at index ${n} must have a numeric index, received: ${e(r.index)}`);if(r.index<0)throw new Error(`Chat response result at index ${n} must have a non-negative index, received: ${e(r.index)}`);if(!r.content&&!r.thought&&(!r.thoughtBlocks||r.thoughtBlocks.length===0)&&!r.functionCalls&&!r.finishReason)throw new Error(`Chat response result at index ${n} must have at least one of: content, thought, thoughtBlocks, functionCalls, or finishReason, received: ${e({content:r.content,thought:r.thought,thoughtBlocks:r.thoughtBlocks,functionCalls:r.functionCalls,finishReason:r.finishReason})}`);if(r.content!==void 0&&typeof r.content!="string")throw new Error(`Chat response result content at index ${n} must be a string, received: ${e(r.content)}`);if(r.thought!==void 0&&typeof r.thought!="string")throw new Error(`Chat response result thought at index ${n} must be a string, received: ${e(r.thought)}`);if(r.thoughtBlocks!==void 0){if(!Array.isArray(r.thoughtBlocks))throw new Error(`Chat response result thoughtBlocks at index ${n} must be an array, received: ${e(r.thoughtBlocks)}`);for(let o=0;o<r.thoughtBlocks.length;o++){let s=r.thoughtBlocks[o];if(typeof s!="object"||s===null)throw new Error(`Chat response result thoughtBlocks[${o}] at index ${n} must be an object, received: ${e(s)}`);if(typeof s.data!="string")throw new Error(`Chat response result thoughtBlocks[${o}].data at index ${n} must be a string, received: ${e(s.data)}`);if(typeof s.encrypted!="boolean")throw new Error(`Chat response result thoughtBlocks[${o}].encrypted at index ${n} must be a boolean, received: ${e(s.encrypted)}`);if(s.signature!==void 0&&typeof s.signature!="string")throw new Error(`Chat response result thoughtBlocks[${o}].signature at index ${n} must be a string when provided, received: ${e(s.signature)}`)}}if(r.name!==void 0){if(typeof r.name!="string")throw new Error(`Chat response result name at index ${n} must be a string, received: ${e(r.name)}`);if(r.name.trim()==="")throw new Error(`Chat response result name at index ${n} cannot be empty or whitespace-only, received: ${e(r.name)}`)}if(r.annotations!==void 0){if(!Array.isArray(r.annotations))throw new Error(`Chat response result annotations at index ${n} must be an array, received: ${e(r.annotations)}`);for(let o=0;o<r.annotations.length;o++){let s=r.annotations[o];if(!s||typeof s!="object")throw new Error(`Chat response result annotation at index ${n}[${o}] must be an object, received: ${e(s)}`);if(s.type!=="url_citation")throw new Error(`Chat response result annotation at index ${n}[${o}] must have type 'url_citation', received: ${e(s.type)}`);if(!s.url_citation||typeof s.url_citation!="object")throw new Error(`Chat response result annotation at index ${n}[${o}] must have a valid url_citation object, received: ${e(s.url_citation)}`);if(typeof s.url_citation.url!="string")throw new Error(`Chat response result annotation at index ${n}[${o}] url_citation.url must be a string, received: ${e(s.url_citation.url)}`)}}if(r.id!==void 0){if(typeof r.id!="string")throw new Error(`Chat response result id at index ${n} must be a string, received: ${e(r.id)}`);if(r.id.trim()==="")throw new Error(`Chat response result id at index ${n} cannot be empty or whitespace-only, received: ${e(r.id)}`)}if(r.functionCalls!==void 0){if(!Array.isArray(r.functionCalls))throw new Error(`Chat response result functionCalls at index ${n} must be an array, received: ${e(r.functionCalls)}`);for(let o=0;o<r.functionCalls.length;o++){let s=r.functionCalls[o];if(!s)throw new Error(`Function call at index ${o} in result ${n} cannot be null or undefined, received: ${e(s)}`);if(!s.id||typeof s.id!="string"||s.id.trim()==="")throw new Error(`Function call at index ${o} in result ${n} must have a non-empty string id, received: ${e(s.id)}`);if(s.type!=="function")throw new Error(`Function call at index ${o} in result ${n} must have type 'function', received: ${e(s.type)}`);if(!s.function)throw new Error(`Function call at index ${o} in result ${n} must have a function object, received: ${e(s.function)}`);if(!s.function.name||typeof s.function.name!="string"||s.function.name.trim()==="")throw new Error(`Function call at index ${o} in result ${n} must have a non-empty function name, received: ${e(s.function.name)}`);if(s.function.params!==void 0&&typeof s.function.params!="string"&&typeof s.function.params!="object")throw new Error(`Function call params at index ${o} in result ${n} must be a string or object, received: ${e(s.function.params)}`)}}if(r.finishReason!==void 0){let o=["stop","length","function_call","content_filter","error"];if(!o.includes(r.finishReason))throw new Error(`Chat response result finishReason at index ${n} must be one of: ${o.join(", ")}, received: ${e(r.finishReason)}`)}}}var ks=new Map;function nc(i){return`${i.providerName}:${i.contentHash}`}function rc(i,e){e.type==="text"?i.update(`text:${e.text}`):e.type==="image"?i.update(`image:${e.mimeType}:${e.image.slice(0,100)}`):e.type==="audio"?i.update(`audio:${e.format}:${e.data.slice(0,100)}`):e.type==="file"&&("fileUri"in e?i.update(`file:${e.mimeType}:${e.fileUri}`):i.update(`file:${e.mimeType}:${e.data.slice(0,100)}`))}function oc(i){let e=et("sha256"),t=-1;for(let n=i.length-1;n>=0;n--){let r=i[n];if("cache"in r&&r.cache){t=n;break}}for(let n=0;n<i.length;n++){let r=i[n];if(r.role==="system"){e.update(`system:${r.content}`);continue}if(t>=0&&n<=t)if(r.role==="user"){if(typeof r.content=="string")e.update(`user:${r.content}`);else if(Array.isArray(r.content))for(let o of r.content)rc(e,o)}else r.role==="assistant"&&r.content&&e.update(`assistant:${r.content}`)}return e.digest("hex")}var be=()=>structuredClone({temperature:0}),Ne=()=>structuredClone({temperature:.4,frequencyPenalty:.2}),Me=class{constructor(e,{name:t,apiURL:n,headers:r,modelInfo:o,defaults:s,options:a={},supportFor:l,models:u}){this.aiImpl=e;this.name=t,this.apiURL=n||"",this.headers=r,this.supportFor=l,this.tracer=a.tracer??de.tracer,this.meter=a.meter??de.meter,this.modelInfo=o,this.models=u,this.id=Be();let c=this.getModel(s.model)??s.model,p=this.getEmbedModel(s.embedModel)??s.embedModel;if(this.defaults={model:c,embedModel:p},!s.model||typeof s.model!="string"||s.model==="")throw new Error("No model defined");this.setOptions(a),u&&sc(u)}#e=!1;#t=!1;rt;fetch;tracer;meter;timeout;excludeContentFromTrace;models;abortSignal;logger=de.logger??Wi;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 sa(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??de.debug??!1,this.#t=e.verbose??!1,this.rt=e.rateLimiter,this.fetch=e.fetch,this.timeout=e.timeout,this.tracer=e.tracer??de.tracer,this.meter=e.meter??de.meter,this.excludeContentFromTrace=e.excludeContentFromTrace,this.abortSignal=e.abortSignal,this.logger=e.logger??de.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 Ft(de.customLabels,this.customLabels,e)}getModelList(){let e=[];for(let t of this.models??[])t.isInternal||("model"in t&&t.model&&e.push({key:t.key,description:t.description,model:t.model}),"embedModel"in t&&t.embedModel&&e.push({key:t.key,description:t.description,embedModel:t.embedModel}));return e}getName(){return this.name}getFeatures(e){return typeof this.supportFor=="function"?this.supportFor(e??this.defaults.model):this.supportFor}getLastUsedChatModel(){return this.lastUsedChatModel}getLastUsedEmbedModel(){return this.lastUsedEmbedModel}getLastUsedModelConfig(){return this.lastUsedModelConfig}calculatePercentile(e,t){if(e.length===0)return 0;let n=[...e].sort((o,s)=>o-s),r=Math.ceil(t/100*n.length)-1;return n[r]??0}updateLatencyMetrics(e,t,n){let r=this.metrics.latency[e];r.samples.push(t),r.samples.length>1e3&&r.samples.shift(),r.mean=r.samples.reduce((s,a)=>s+a,0)/r.samples.length,r.p95=this.calculatePercentile(r.samples,95),r.p99=this.calculatePercentile(r.samples,99);let o=this.getMetricsInstruments();if(o){let s=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel,a=this.getMergedCustomLabels(n);ia(o,e,t,this.name,s,a),aa(o,e,r.mean,r.p95,r.p99,this.name,s,a)}}updateErrorMetrics(e,t,n){let r=this.metrics.errors[e];r.total++,t&&r.count++,r.rate=r.count/r.total;let o=this.getMetricsInstruments();if(o){let s=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel,a=this.getMergedCustomLabels(n);ca(o,e,this.name,s,a),t&&la(o,e,this.name,s,a),ua(o,e,r.rate,this.name,s,a)}}recordTokenUsage(e,t){let n=this.getMetricsInstruments();if(n&&e?.tokens){let{promptTokens:r,completionTokens:o,totalTokens:s,thoughtsTokens:a,cacheReadTokens:l,cacheCreationTokens:u}=e.tokens,c=this.getMergedCustomLabels(t);r&&Wn(n,"input",r,this.name,e.model,c),o&&Wn(n,"output",o,this.name,e.model,c),s&&Wn(n,"total",s,this.name,e.model,c),a&&Wn(n,"thoughts",a,this.name,e.model,c),l&&vs(n,"read",l,this.name,e.model,c),u&&vs(n,"write",u,this.name,e.model,c)}}calculateRequestSize(e){try{return new TextEncoder().encode(JSON.stringify(e)).length}catch{return 0}}calculateResponseSize(e){try{return new TextEncoder().encode(JSON.stringify(e)).length}catch{return 0}}detectMultimodalContent(e){let t=!1,n=!1;if(e.chatPrompt&&Array.isArray(e.chatPrompt)){for(let r of e.chatPrompt)if(r.role==="user"&&Array.isArray(r.content))for(let o of r.content)o.type==="image"?t=!0:o.type==="audio"&&(n=!0)}return{hasImages:t,hasAudio:n}}calculatePromptLength(e){let t=0;if(e.chatPrompt&&Array.isArray(e.chatPrompt))for(let n of e.chatPrompt)if(n.role==="system"||n.role==="assistant")n.content&&(t+=n.content.length);else if(n.role==="user"){if(typeof n.content=="string")t+=n.content.length;else if(Array.isArray(n.content))for(let r of n.content)r.type==="text"&&(t+=r.text.length)}else n.role==="function"&&n.result&&(t+=n.result.length);return t}calculateContextWindowUsage(e,t){if(!t?.tokens?.promptTokens)return 0;let n=this.modelInfo.find(r=>r.name===e);return n?.contextWindow?t.tokens.promptTokens/n.contextWindow:0}estimateCost(e,t){if(!t?.tokens)return 0;let n=this.modelInfo.find(l=>l.name===e);if(!n||!n.promptTokenCostPer1M&&!n.completionTokenCostPer1M)return 0;let{promptTokens:r=0,completionTokens:o=0}=t.tokens,s=n.promptTokenCostPer1M||0,a=n.completionTokenCostPer1M||0;return r*s/1e6+o*a/1e6}estimateCostByName(e,t){if(!t?.tokens)return 0;let n=this.modelInfo.find(l=>l.name===e);if(!n||!n.promptTokenCostPer1M&&!n.completionTokenCostPer1M)return 0;let{promptTokens:r=0,completionTokens:o=0}=t.tokens,s=n.promptTokenCostPer1M||0,a=n.completionTokenCostPer1M||0;return r*s/1e6+o*a/1e6}recordFunctionCallMetrics(e,t,n){let r=this.getMetricsInstruments();if(!(!r||!e))for(let o of e)o&&typeof o=="object"&&"function"in o&&o.function&&typeof o.function=="object"&&"name"in o.function&&da(r,o.function.name,void 0,this.name,t,this.getMergedCustomLabels(n))}recordTimeoutMetric(e,t){let n=this.getMetricsInstruments();if(n){let r=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel;fa(n,e,this.name,r,this.getMergedCustomLabels(t))}}recordAbortMetric(e,t){let n=this.getMetricsInstruments();if(n){let r=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel;xa(n,e,this.name,r,this.getMergedCustomLabels(t))}}recordChatMetrics(e,t,n){let r=this.getMetricsInstruments();if(!r)return;let o=this.lastUsedChatModel,s=this.lastUsedModelConfig,a=this.getMergedCustomLabels(t?.customLabels),l=s?.stream??!1;pa(r,"chat",l,this.name,o,a);let{hasImages:u,hasAudio:c}=this.detectMultimodalContent(e);ya(r,u,c,this.name,o,a);let p=this.calculatePromptLength(e);ga(r,p,this.name,o,a),ma(r,s?.temperature,s?.maxTokens,this.name,o,a),t?.thinkingTokenBudget&&this.modelUsage?.tokens?.thoughtsTokens&&Aa(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(Rs(r,"chat",g,this.name,o,a),m.results)for(let I of m.results)I.functionCalls&&this.recordFunctionCallMetrics(I.functionCalls,this.lastUsedChatModel,a);let h=this.calculateContextWindowUsage(this.lastUsedChatModel,m.modelUsage);h>0&&ha(r,h,this.name,o,a);let f=this.estimateCost(this.lastUsedChatModel,m.modelUsage);f>0&&Ss(r,"chat",f,this.name,o,a)}}recordEmbedMetrics(e,t,n){let r=this.getMetricsInstruments();if(!r)return;let o=this.lastUsedEmbedModel,s=this.getMergedCustomLabels(n?.customLabels),a=this.calculateRequestSize(e);ws(r,"embed",a,this.name,o,s);let l=this.calculateResponseSize(t);Rs(r,"embed",l,this.name,o,s);let u=this.estimateCostByName(o,t.modelUsage);u>0&&Ss(r,"embed",u,this.name,o,s)}getMetrics(){return structuredClone(this.metrics)}async chat(e,t){let n=performance.now(),r=!1,o,s=this.getModelByKey(e.model),a=s?s.thinkingTokenBudget:void 0,l={...s?{thinkingTokenBudget:a,showThoughts:s.showThoughts,stream:s.stream,debug:s.debug,useExpensiveModel:s.useExpensiveModel}:void 0,...Object.fromEntries(Object.entries(t??{}).filter(([,u])=>u!==void 0))};try{return o=await this._chat1(e,l),o}catch(u){throw r=!0,u instanceof Error&&(u.message.includes("timeout")||u.name==="TimeoutError"?this.recordTimeoutMetric("chat",l?.customLabels):(u.message.includes("abort")||u.name==="AbortError")&&this.recordAbortMetric("chat",l?.customLabels)),u}finally{let u=performance.now()-n;this.updateLatencyMetrics("chat",u,l?.customLabels),this.updateErrorMetrics("chat",r,l?.customLabels),r||this.recordChatMetrics(e,l,o)}}async _chat1(e,t){let n=this.getModel(e.model)??e.model??this.defaults.model;if(Array.isArray(e.chatPrompt))for(let u of e.chatPrompt)Ht(u);let r=this.getModelByKey(e.model),o={...this.aiImpl.getModelConfig(),...r?r.modelConfig:void 0,...e.modelConfig},s=this.modelInfo.find(u=>u.name===n);if(s?.notSupported?.temperature&&"temperature"in o&&delete o.temperature,s?.notSupported?.topP&&"topP"in o&&delete o.topP,t?.thinkingTokenBudget&&!this.getFeatures(n).hasThinkingBudget)throw new Error(`Model ${n} does not support thinkingTokenBudget.`);if(t?.showThoughts&&!this.getFeatures(n).hasShowThoughts)throw new Error(`Model ${n} does not support showThoughts.`);if(this.modelInfo.find(u=>u.name===n)?.isExpensive&&t?.useExpensiveModel!=="yes")throw new Error(`Model ${n} is marked as expensive and requires explicit confirmation. Set useExpensiveModel: "yes" to proceed.`);return o.stream=(t?.stream!==void 0?t.stream:o.stream)??!0,this.getFeatures(n).streaming||(o.stream=!1),this.tracer?await this.tracer.startActiveSpan("AI Chat Request",{kind:Kt.SpanKind.SERVER,attributes:{[ge.LLM_SYSTEM]:this.name,[ge.LLM_OPERATION_NAME]:"chat",[ge.LLM_REQUEST_MODEL]:n,[ge.LLM_REQUEST_MAX_TOKENS]:o.maxTokens??"Not set",[ge.LLM_REQUEST_TEMPERATURE]:o.temperature,[ge.LLM_REQUEST_TOP_P]:o.topP??"Not set",[ge.LLM_REQUEST_TOP_K]:o.topK??"Not set",[ge.LLM_REQUEST_FREQUENCY_PENALTY]:o.frequencyPenalty??"Not set",[ge.LLM_REQUEST_PRESENCE_PENALTY]:o.presencePenalty??"Not set",[ge.LLM_REQUEST_STOP_SEQUENCES]:o.stopSequences?.join(", ")??"Not set",[ge.LLM_REQUEST_LLM_IS_STREAMING]:o.stream??"Not set"}},t?.traceContext??Kt.context.active(),async u=>await this._chat2(n,o,e,t,u)):await this._chat2(n,o,e,t)}cleanupFunctionSchema(e){let t={...e};if(t.parameters){let n={...t.parameters};Array.isArray(n.required)&&n.required.length===0&&delete n.required,n.properties&&Object.keys(n.properties).length===0&&delete n.properties,Object.keys(n).length===0||Object.keys(n).length===1&&n.type==="object"?delete t.parameters:t.parameters=n}return t}async _chat2(e,t,n,r,o){if(!this.aiImpl.createChatReq)throw new Error("createChatReq not implemented");let s=r?.debug??this.#e,a=r?.verbose??this.#t,l;n.functions&&n.functions.length>0&&(l=n.functions.map(A=>this.cleanupFunctionSchema(A)));let u={...n,model:e,functions:l,modelConfig:t};this.lastUsedChatModel=e,this.lastUsedModelConfig=t,s&&Ji(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(A=>{if(A.role==="assistant"){let{content:w,name:b,cache:T}=A;return{role:"assistant",content:w,name:b,cache:T}}return A.role==="function"?{role:"user",content:A.result}:A}),functions:[]}:u,g=await this.handleContextCaching(e,m,r,o),h=async()=>{if(g?.preparedRequest){let{apiConfig:T,request:v}=g.preparedRequest;return o?.isRecording()&&Ia(n,o,this.excludeContentFromTrace),await qe({name:T.name,url:this.apiURL,localCall:T.localCall,headers:await this.buildHeaders(T.headers),stream:t.stream,timeout:this.timeout,verbose:a,fetch:this.fetch,span:o,abortSignal:r?.abortSignal??this.abortSignal,corsProxy:this.corsProxy,retry:r?.retry??this.retry},v)}let[A,w]=await this.aiImpl.createChatReq(m,r);return o?.isRecording()&&Ia(n,o,this.excludeContentFromTrace),await qe({name:A.name,url:this.apiURL,localCall:A.localCall,headers:await this.buildHeaders(A.headers),stream:t.stream,timeout:this.timeout,verbose:a,fetch:this.fetch,span:o,abortSignal:r?.abortSignal??this.abortSignal,corsProxy:this.corsProxy,retry:r?.retry??this.retry},w)},f=r?.rateLimiter??this.rt,I=f?await f(h,{modelUsage:this.modelUsage}):await h();if(t.stream){if(!this.aiImpl.createChatStreamResp)throw new Error("createChatStreamResp not implemented");let A=this.aiImpl.createChatStreamResp.bind(this),w=F=>O=>{let D=A(O,F);if(D.sessionId=r?.sessionId,!D.modelUsage){let B=this.aiImpl.getTokenUsage();B&&(D.modelUsage={ai:this.name,model:e,tokens:B})}return this.modelUsage=D.modelUsage,this.recordTokenUsage(D.modelUsage,r?.customLabels),o?.isRecording()&&Ca(D,o,this.excludeContentFromTrace),D},b=async F=>{o?.isRecording()&&o.end(),s&&Yi(F,r?.logger??this.logger)};if(typeof window<"u"){let F=I,O={},D=[],B=r?.abortSignal??this.abortSignal;return new ReadableStream({start:Q=>{let S=F.getReader(),G=()=>{try{S.cancel().catch(()=>{})}catch{}try{this.recordAbortMetric("chat",r?.customLabels)}catch{}try{o?.isRecording()&&o.end()}catch{}try{Q.error(new DOMException("Aborted","AbortError"))}catch{Q.error(new Error("Aborted"))}};if(B){if(B.aborted){G();return}B.addEventListener("abort",G,{once:!0})}async function N(){try{for(;;){let{done:Y,value:M}=await S.read();if(Y){b&&await b(D),Q.close();break}let L=w(O)(M);L&&(D.push(L),Q.enqueue(L))}}catch(Y){if(Q.error(Y),o?.isRecording())try{o.end()}catch{}}finally{if(S.releaseLock(),B)try{B.removeEventListener("abort",G)}catch{}}}N()}})}return I.pipeThrough(new Dr(w({}),b))}if(!this.aiImpl.createChatResp)throw new Error("createChatResp not implemented");let x=this.aiImpl.createChatResp(I);if(x.sessionId=r?.sessionId,!x.modelUsage){let A=this.aiImpl.getTokenUsage();A&&(x.modelUsage={ai:this.name,model:e,tokens:A})}return x.modelUsage&&(this.modelUsage=x.modelUsage,this.recordTokenUsage(x.modelUsage,r?.customLabels)),o?.isRecording()&&(Ca(x,o,this.excludeContentFromTrace),o.end()),s&&Qi(x,r?.logger??this.logger),x}async embed(e,t){let n=performance.now(),r=!1,o,s=this.getModelByKey(e.embedModel),a={...s?{thinkingTokenBudget:s.thinkingTokenBudget,showThoughts:s.showThoughts,stream:s.stream,debug:s.debug,useExpensiveModel:s.useExpensiveModel}:void 0,...t};try{return o=await this._embed1(e,a),o}catch(l){throw r=!0,l instanceof Error&&(l.message.includes("timeout")||l.name==="TimeoutError"?this.recordTimeoutMetric("embed",a?.customLabels):(l.message.includes("abort")||l.name==="AbortError")&&this.recordAbortMetric("embed",a?.customLabels)),l}finally{let l=performance.now()-n;this.updateLatencyMetrics("embed",l,a?.customLabels),this.updateErrorMetrics("embed",r,a?.customLabels),!r&&o&&this.recordEmbedMetrics(e,o,a)}}async _embed1(e,t){let n=this.getEmbedModel(e.embedModel)??e.embedModel??this.defaults.embedModel;if(!n)throw new Error("No embed model defined");return this.tracer?await this.tracer.startActiveSpan("AI Embed Request",{kind:Kt.SpanKind.SERVER,attributes:{[ge.LLM_SYSTEM]:this.name,[ge.LLM_OPERATION_NAME]:"embeddings",[ge.LLM_REQUEST_MODEL]:n}},t?.traceContext??Kt.context.active(),async r=>await this._embed2(n,e,t,r)):await this._embed2(n,e,t)}async _embed2(e,t,n,r){if(!this.aiImpl.createEmbedReq)throw new Error("createEmbedReq not implemented");if(!this.aiImpl.createEmbedResp)throw new Error("createEmbedResp not implemented");let o=this.aiImpl.createEmbedReq.bind(this.aiImpl),s=n?.debug??this.#e,a=n?.verbose??this.#t,l={...t,embedModel:e};this.lastUsedEmbedModel=e,s&&na(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(mt.GEN_AI_USAGE,{[ge.LLM_USAGE_INPUT_TOKENS]:d.modelUsage.tokens.promptTokens,[ge.LLM_USAGE_OUTPUT_TOKENS]:d.modelUsage.tokens.completionTokens??0,[ge.LLM_USAGE_TOTAL_TOKENS]:d.modelUsage.tokens.totalTokens}),s&&ra(d.embeddings,n?.logger??this.logger),r?.end(),d}async buildHeaders(e={}){return{...e,...await this.headers()}}getModelByKey(e){return e?this.models?.find(n=>n.key===e):void 0}getModel(e){let t=this.getModelByKey(e);return t&&"model"in t?t.model:void 0}getEmbedModel(e){let t=this.getModelByKey(e);return t&&"embedModel"in t?t.embedModel:void 0}async handleContextCaching(e,t,n,r){let o=n?.contextCache;if(!o)return null;let s=this.aiImpl.supportsContextCache?.(e)??!1,a=this.aiImpl.supportsImplicitCaching?.(e)??!1;if(!s&&!a)throw new Error(`Context caching is not supported by this provider/model (${this.getName()}/${e}). Remove the contextCache option or use a provider that supports caching.`);if(!s)return null;let l=o.ttlSeconds??3600,u=o.refreshWindowSeconds??300,c=o.minTokens??2048;if(o.name)return this.useCacheByName(e,t,o.name,n,r);let p=oc(t.chatPrompt);if(!p||p===et("sha256").digest("hex"))return null;let d={providerName:this.getName(),contentHash:p},m=nc(d),g=Date.now(),h=o.registry,f=h?await h.get(m):ks.get(m);if(f&&f.expiresAt>g){if(f.expiresAt-g<u*1e3&&this.aiImpl.buildCacheUpdateTTLOp){await this.executeCacheOperation(this.aiImpl.buildCacheUpdateTTLOp(f.cacheName,l),n,r);let w={cacheName:f.cacheName,expiresAt:g+l*1e3,tokenCount:f.tokenCount};h?await h.set(m,w):ks.set(m,{...w,contentHash:p,lastTouchedAt:g})}return this.useCacheByName(e,t,f.cacheName,n,r)}if(this.estimateCacheableTokens(t.chatPrompt)<c)return null;let x=this.aiImpl.buildCacheCreateOp?.(t,n);if(x){let A=await this.executeCacheOperation(x,n,r);if(A){let w={cacheName:A.name,expiresAt:new Date(A.expiresAt).getTime(),tokenCount:A.tokenCount};return h?await h.set(m,w):ks.set(m,{...w,contentHash:p,lastTouchedAt:g}),this.useCacheByName(e,t,A.name,n,r)}}return null}async useCacheByName(e,t,n,r,o){if(this.aiImpl.prepareCachedChatReq){let s=await this.aiImpl.prepareCachedChatReq(t,r??{},n);return{preparedRequest:{apiConfig:s.apiConfig,request:s.request}}}return null}async executeCacheOperation(e,t,n){let r=t?.verbose??this.#t;try{n?.addEvent("context_cache.operation",{type:e.type,endpoint:e.apiConfig.name});let o=await qe({name:e.apiConfig.name,url:this.apiURL,localCall:e.apiConfig.localCall,headers:await this.buildHeaders(e.apiConfig.headers),stream:!1,timeout:this.timeout,verbose:r,fetch:this.fetch,span:n,abortSignal:t?.abortSignal??this.abortSignal,corsProxy:this.corsProxy,retry:t?.retry??this.retry},e.request);return e.parseResponse(o)}catch(o){n?.addEvent("context_cache.error",{type:e.type,error:o instanceof Error?o.message:String(o)});return}}estimateCacheableTokens(e){let t=0;for(let n of e){if(n.role==="system"){t+=n.content.length;continue}if("cache"in n&&n.cache)if(n.role==="user"){if(typeof n.content=="string")t+=n.content.length;else if(Array.isArray(n.content))for(let r of n.content)"cache"in r&&r.cache&&(r.type==="text"?t+=r.text.length:r.type==="image"?t+=1e3:r.type==="audio"?t+=2e3:r.type==="file"&&(t+=500))}else n.role==="assistant"&&n.content&&(t+=n.content.length)}return Math.ceil(t/4)}};function Ia(i,e,t){let n=[];if(i.chatPrompt&&Array.isArray(i.chatPrompt)&&i.chatPrompt.length>0)for(let o of i.chatPrompt)switch(o.role){case"system":if(o.content){let s={};t||(s.content=o.content),e.addEvent(mt.GEN_AI_SYSTEM_MESSAGE,s)}break;case"user":if(typeof o.content=="string")n.push(o.content);else if(Array.isArray(o.content))for(let s of o.content)s.type==="text"&&n.push(s.text);break;case"assistant":{let s=o.functionCalls?.map(a=>({id:a.id,type:a.type,function:a.function.name,arguments:a.function.params}));if(s&&s.length>0){let a={function_calls:JSON.stringify(s,null,2)};!t&&o.content&&(a.content=o.content),e.addEvent(mt.GEN_AI_ASSISTANT_MESSAGE,a)}else if(o.content){let a={};t||(a.content=o.content),e.addEvent(mt.GEN_AI_ASSISTANT_MESSAGE,a)}break}case"function":{let s={id:o.functionId};t||(s.content=o.result),e.addEvent(mt.GEN_AI_TOOL_MESSAGE,s);break}}let r={};t||(r.content=n.join(`
169
+ `)),e.addEvent(mt.GEN_AI_USER_MESSAGE,r)}function Ca(i,e,t){if(i.modelUsage?.tokens){let n=i.modelUsage.tokens.thoughtsTokens?{[ge.LLM_USAGE_THOUGHTS_TOKENS]:i.modelUsage.tokens.thoughtsTokens}:{};e.addEvent(mt.GEN_AI_USAGE,{[ge.LLM_USAGE_INPUT_TOKENS]:i.modelUsage.tokens.promptTokens,[ge.LLM_USAGE_OUTPUT_TOKENS]:i.modelUsage.tokens.completionTokens??0,[ge.LLM_USAGE_TOTAL_TOKENS]:i.modelUsage.tokens.totalTokens,...n})}if(i.results)for(let n=0;n<i.results.length;n++){let r=i.results[n];if(!r||!r.content&&!r.thought&&!r.functionCalls?.length&&!r.finishReason)continue;let o=r.functionCalls?.map(a=>({id:a.id,type:a.type,function:a.function.name,arguments:a.function.params})),s={};o&&o.length>0?(t||(s.content=r.content),s.tool_calls=o):t||(s.content=r.content??""),e.addEvent(mt.GEN_AI_CHOICE,{finish_reason:r.finishReason,index:n,message:JSON.stringify(s,null,2)})}}function Ta(i){let e=0;for(let t of i){if(!t||typeof t!="object")throw new Error(`AxMessage array validation failed: Item at index ${e} is not a valid message object`);if(t.role!=="user"&&t.role!=="assistant")throw new Error(`AxMessage array validation failed: Item at index ${e} has invalid role: ${t.role}`);e++}}function sc(i){let e=new Set;for(let t of i){if(e.has(t.key))throw new Error(`Duplicate model key detected: "${t.key}". Each model key must be unique.`);e.add(t.key)}}var Vn=(f=>(f.Claude46Opus="claude-opus-4-6",f.Claude46Sonnet="claude-sonnet-4-6",f.Claude45Opus="claude-opus-4-5-20251101",f.Claude41Opus="claude-opus-4-1-20250805",f.Claude4Opus="claude-opus-4-20250514",f.Claude4Sonnet="claude-sonnet-4-20250514",f.Claude45Sonnet="claude-sonnet-4-5-20250929",f.Claude45Haiku="claude-haiku-4-5",f.Claude37Sonnet="claude-3-7-sonnet-latest",f.Claude35Sonnet="claude-3-5-sonnet-latest",f.Claude35Haiku="claude-3-5-haiku-latest",f.Claude3Opus="claude-3-opus-latest",f.Claude3Sonnet="claude-3-sonnet-20240229",f.Claude3Haiku="claude-3-haiku-20240307",f.Claude21="claude-2.1",f.ClaudeInstant12="claude-instant-1.2",f))(Vn||{}),Jn=(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))(Jn||{});var Qn=[{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 kt=i=>{if(!i||typeof i!="object")return i;let e={...i};if(e.type==="object"||Array.isArray(e.type)&&e.type.includes("object")){if(!e.properties||Object.keys(e.properties).length===0)throw new Error("Anthropic models do not support arbitrary JSON objects (e.g. f.json() or f.object() with no properties) in structured outputs. Please use f.string() and instruct the model to return a JSON string, or define the expected structure with f.object({ ... })");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,kt(r)]))),e.items&&(e.items=kt(e.items)),Array.isArray(e.anyOf)&&(e.anyOf=e.anyOf.map(n=>kt(n))),Array.isArray(e.allOf)&&(e.allOf=e.allOf.map(n=>kt(n))),Array.isArray(e.oneOf)&&(e.oneOf=e.oneOf.map(n=>kt(n))),e},Ms=()=>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()}),Ra=()=>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()}),Os=class{constructor(e,t){this.config=e;this.isVertex=t}tokensUsed;currentPromptConfig;usedStructuredOutput=!1;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens??4096,temperature:e.temperature,topP:e.topP,topK:e.topK,stream:e.stream,stopSequences:e.stopSequences,endSequences:e.endSequences,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,n:e.n}}createChatReq=async(e,t)=>{this.currentPromptConfig=t;let n=e.model,r=e.modelConfig?.stream??this.config.stream,o;this.isVertex?o={name:r?`/models/${n}:streamRawPredict?alt=sse`:`/models/${n}:rawPredict`}:o={name:"/messages"};let s;if(e.functionCall&&e.functions&&e.functions.length>0)if(typeof e.functionCall=="string")switch(e.functionCall){case"auto":s={tool_choice:{type:"auto"}};break;case"required":s={tool_choice:{type:"any"}};break;case"none":throw new Error("functionCall none not supported")}else if("function"in e.functionCall)s={tool_choice:{type:"tool",name:e.functionCall.function.name}};else throw new Error("Invalid function call type, must be string or object");let a=e.chatPrompt.some(N=>"cache"in N&&N.cache)||e.functions?.some(N=>N.cache),l=e.chatPrompt.filter(N=>N.role==="system"),u=l.map((N,Y)=>({type:"text",text:N.content,...N.cache||a&&Y===l.length-1?{cache_control:{type:"ephemeral"}}:{}})),c=e.chatPrompt.filter(N=>N.role!=="system"),p=e.functions?.map((N,Y,M)=>{let L={type:"object",properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]},X=N.parameters?kt(N.parameters):void 0;return X===void 0||X&&typeof X=="object"&&Object.keys(X).length===0?X={...L}:X&&typeof X=="object"&&X.type==="object"&&(!("properties"in X)||!X.properties||Object.keys(X.properties).length===0)&&(X={...X,properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]}),{name:N.name,description:N.description,input_schema:X,...N.cache||a&&Y===M.length-1?{cache_control:{type:"ephemeral"}}:{}}}),m=(this.config.tools??[]).map(N=>N&&typeof N=="object"&&"type"in N?N:{name:N.name,description:N.description,input_schema:N.input_schema?kt(N.input_schema):void 0,...N.cache_control?{cache_control:N.cache_control}:{}}),g=[...p??[],...m];g.length===0&&(g=void 0);let h=e.modelConfig?.maxTokens??this.config.maxTokens,f=e.modelConfig?.stopSequences??this.config.stopSequences,I=e.modelConfig?.temperature,x=e.modelConfig?.topP,A=e.modelConfig?.topK??this.config.topK,w=e.modelConfig?.n??this.config.n;if(w&&w>1)throw new Error("Anthropic does not support sampling (n > 1)");let b=N=>N.includes("claude-opus-4-6"),T=N=>N.includes("claude-opus-4-5"),v,F,O=n;if(t?.thinkingTokenBudget){let N=this.config.thinkingTokenBudgetLevels,Y=this.config.effortLevelMapping;if(t.thinkingTokenBudget==="none")v=void 0,F=void 0;else{let M=t.thinkingTokenBudget;if(b(O))v={type:"adaptive"},F={effort:Y?.[M]??"medium"};else if(T(O)){v={type:"enabled",budget_tokens:N?.[M]??1e4};let X=Y?.[M]??"medium";X==="max"&&(X="high"),F={effort:X}}else v={type:"enabled",budget_tokens:N?.[M]??1e4}}}let B=ic(c,!!v);B.some(N=>N.role==="assistant"&&Array.isArray(N.content)&&N.content.length>0&&N.content[0]?.type==="tool_use")&&(v=void 0,F=void 0);let S;if(this.usedStructuredOutput=!1,e.responseFormat&&e.responseFormat.type==="json_schema"&&e.responseFormat.schema){let N=e.responseFormat.schema.schema||e.responseFormat.schema;S={type:"json_schema",schema:kt(N)},this.usedStructuredOutput=!0}let G={...this.isVertex?{anthropic_version:"vertex-2023-10-16"}:{model:n},...h?{max_tokens:h}:{},...f&&f.length>0?{stop_sequences:f}:{},...I!==void 0&&!v?{temperature:I}:{},...x!==void 0&&(!v||x>=.95)?{top_p:x}:{},...A&&!v?{top_k:A}:{},...s,...g?{tools:g}:{},...r?{stream:!0}:{},...u?{system:u}:{},...v?{thinking:v}:{},...F?{output_config:F}:{},...S?{output_format:S}:{},messages:B};return[o,G]};createChatResp=e=>{if(e.type==="error")throw new Re(e.error.message,void 0,void 0);let t=wa(e.stop_reason),n=this.currentPromptConfig?.thinkingTokenBudget!=="none"&&this.currentPromptConfig?.showThoughts!==!1,r="",o=[],s=[],a=[];for(let c of e.content)switch(c.type){case"text":if(r+=c.text??"",Array.isArray(c.citations))for(let p of c.citations)p?.url&&a.push({url:String(p.url),title:typeof p.title=="string"?p.title:void 0,snippet:typeof p.cited_text=="string"?p.cited_text:void 0});break;case"thinking":if(n){let p=c.thinking??"",d=c.signature;s.push({data:p,encrypted:!1,...typeof d=="string"?{signature:d}:{}})}break;case"redacted_thinking":if(n){let p=c.data??"",d=c.signature;s.push({data:p,encrypted:!0,...typeof d=="string"?{signature:d}:{}})}break;case"tool_use":o.push({id:c.id,type:"function",function:{name:c.name,params:c.input}});break}let l={index:0,id:e.id,finishReason:t};r&&(l.content=r),s.length>0&&(l.thoughtBlocks=s,l.thought=s.map(c=>c.data).join("")),o.length>0&&(l.functionCalls=o),a.length>0&&(l.citations=a);let u=[l];return this.tokensUsed={promptTokens:e.usage.input_tokens,completionTokens:e.usage.output_tokens,totalTokens:e.usage.input_tokens+e.usage.output_tokens+(e.usage.cache_creation_input_tokens||0)+(e.usage.cache_read_input_tokens||0),cacheCreationTokens:e.usage.cache_creation_input_tokens,cacheReadTokens:e.usage.cache_read_input_tokens},{results:u,remoteId:e.id}};createChatStreamResp=(e,t)=>{if(!("type"in e))throw new Error("Invalid Anthropic streaming event");let n=t;if(n.indexIdMap||(n.indexIdMap={}),e.type==="error"){let{error:o}=e;throw new Re(o.message,void 0,void 0)}let r=0;if(e.type==="message_start"){let{message:o}=e,s=[{index:r,content:"",id:o.id}];return this.tokensUsed={promptTokens:o.usage?.input_tokens??0,completionTokens:o.usage?.output_tokens??0,totalTokens:(o.usage?.input_tokens??0)+(o.usage?.output_tokens??0)+(o.usage?.cache_creation_input_tokens??0)+(o.usage?.cache_read_input_tokens??0),cacheCreationTokens:o.usage?.cache_creation_input_tokens,cacheReadTokens:o.usage?.cache_read_input_tokens},{results:s}}if(e.type==="content_block_start"){let{content_block:o}=e;if(o.type==="text"){let s=[];if(Array.isArray(o.citations))for(let a of o.citations)a?.url&&s.push({url:String(a.url),title:typeof a.title=="string"?a.title:void 0,snippet:typeof a.cited_text=="string"?a.cited_text:void 0});return{results:[{index:r,content:o.text,...s.length?{citations:s}:{}}]}}if(o.type==="thinking")return this.currentPromptConfig?.thinkingTokenBudget!=="none"&&this.currentPromptConfig?.showThoughts!==!1?{results:[{index:r,thought:o.thinking,thoughtBlocks:[{data:o.thinking,encrypted:!1}]}]}:{results:[{index:r,content:""}]};if(o.type==="tool_use"&&typeof o.id=="string"&&typeof e.index=="number"&&!n.indexIdMap[e.index]){n.indexIdMap[e.index]=o.id;let s=[{id:o.id,type:"function",function:{name:o.name,params:""}}];return{results:[{index:r,functionCalls:s}]}}if(o.type==="web_search_tool_result"||o.type==="server_tool_use")return{results:[{index:r,content:""}]}}if(e.type==="content_block_delta"){let{delta:o}=e;if(o.type==="citations_delta"){let s=o.citation;if(s&&typeof s.url=="string"&&s.url.length>0){let a=[{url:String(s.url),title:typeof s.title=="string"?s.title:void 0,snippet:typeof s.cited_text=="string"?s.cited_text:void 0}];return{results:[{index:r,content:"",citations:a}]}}return{results:[{index:r,content:""}]}}if(o.type==="text_delta"){let s=[];if(Array.isArray(o.citations))for(let a of o.citations)a?.url&&s.push({url:String(a.url),title:typeof a.title=="string"?a.title:void 0,snippet:typeof a.cited_text=="string"?a.cited_text:void 0});return{results:[{index:r,content:o.text,...s.length?{citations:s}:{}}]}}if(o.type==="thinking_delta")return this.currentPromptConfig?.thinkingTokenBudget!=="none"&&this.currentPromptConfig?.showThoughts!==!1?{results:[{index:r,thought:o.thinking,thoughtBlocks:[{data:o.thinking,encrypted:!1}]}]}:{results:[{index:r,content:""}]};if(o.type==="signature_delta")return{results:[{index:r,thoughtBlocks:[{data:"",encrypted:!1,signature:o.signature}]}]};if(o.type==="input_json_delta"){let s=n.indexIdMap[e.index];if(!s)return{results:[{index:r,content:""}]};let a=[{id:s,type:"function",function:{name:"",params:o.partial_json}}];return{results:[{index:r,functionCalls:a}]}}}if(e.type==="message_delta"){let{delta:o,usage:s}=e;return this.tokensUsed={promptTokens:this.tokensUsed?.promptTokens??0,completionTokens:s.output_tokens,totalTokens:(this.tokensUsed?.promptTokens??0)+s.output_tokens+(this.tokensUsed?.cacheCreationTokens??0)+(this.tokensUsed?.cacheReadTokens??0),cacheCreationTokens:this.tokensUsed?.cacheCreationTokens,cacheReadTokens:this.tokensUsed?.cacheReadTokens},{results:[{index:r,content:"",finishReason:wa(o.stop_reason)}]}}return{results:[{index:r,content:""}]}};supportsImplicitCaching=()=>!0},Wt=class i extends Me{static create(e){return new i(e)}constructor({apiKey:e,projectId:t,region:n,config:r,options:o,models:s}){let a=t!==void 0&&n!==void 0,l,u;if(a){if(!e)throw new Error("Anthropic Vertex API key not set");if(typeof e!="function")throw new Error("Anthropic Vertex API key must be a function for token-based authentication");l=`https://${n==="global"?"aiplatform":`${n}-aiplatform`}.googleapis.com/v1/projects/${t}/locations/${n}/publishers/anthropic/`,u=async()=>({Authorization:`Bearer ${await e()}`,"anthropic-beta":"web-search-2025-03-05"})}else{if(!e)throw new Error("Anthropic API key not set");l="https://api.anthropic.com/v1",u=async()=>({"anthropic-version":"2023-06-01","anthropic-beta":"structured-outputs-2025-11-13, web-search-2025-03-05","x-api-key":typeof e=="function"?await e():e})}let c={...Ms(),...r},p=new Os(c,a),d=g=>{let h=nt({model:g,modelInfo:Qn,models:s});return{functions:!0,streaming:!0,hasThinkingBudget:h?.supported?.thinkingBudget??!1,hasShowThoughts:h?.supported?.showThoughts??!1,structuredOutputs:h?.supported?.structuredOutputs??!1,functionCot:!0,media:{images:{supported:!0,formats:["image/jpeg","image/png","image/gif","image/webp"],maxSize:5*1024*1024,detailLevels:["high","low","auto"]},audio:{supported:!1,formats:[],maxDuration:0},files:{supported:!1,formats:[],maxSize:0,uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!0,types:["ephemeral"],cacheBreakpoints:!1},thinking:h?.supported?.thinkingBudget??!1,multiTurn:!0}},m=s?.map(g=>{let h=g,f=h?.config;if(!f)return g;let I={};f.maxTokens!==void 0&&(I.maxTokens=f.maxTokens),f.temperature!==void 0&&(I.temperature=f.temperature),f.topP!==void 0&&(I.topP=f.topP),f.topK!==void 0&&(I.topK=f.topK),f.presencePenalty!==void 0&&(I.presencePenalty=f.presencePenalty),f.frequencyPenalty!==void 0&&(I.frequencyPenalty=f.frequencyPenalty),f.stopSequences!==void 0&&(I.stopSequences=f.stopSequences),f.endSequences!==void 0&&(I.endSequences=f.endSequences),f.stream!==void 0&&(I.stream=f.stream),f.n!==void 0&&(I.n=f.n);let x={...h};Object.keys(I).length>0&&(x.modelConfig={...h.modelConfig??{},...I});let A=f.thinking?.thinkingTokenBudget;if(typeof A=="number"){let w=c.thinkingTokenBudgetLevels,b=[["minimal",w?.minimal??1024],["low",w?.low??5e3],["medium",w?.medium??1e4],["high",w?.high??2e4],["highest",w?.highest??32e3]],T="minimal",v=Number.POSITIVE_INFINITY;for(let[F,O]of b){let D=Math.abs(A-O);D<v&&(v=D,T=F)}x.thinkingTokenBudget=T}return f.thinking?.includeThoughts!==void 0&&(x.showThoughts=!!f.thinking.includeThoughts),x});super(p,{name:"Anthropic",apiURL:l,headers:u,modelInfo:Qn,defaults:{model:c.model},options:o,supportFor:d,models:m??s})}};function ic(i,e){let t=i.map(r=>{switch(r.role){case"function":return{role:"user",content:[{type:"tool_result",content:r.result,tool_use_id:r.functionId,...r.isError?{is_error:!0}:{},...r.cache?{cache:{type:"ephemeral"}}:{}}]};case"user":return typeof r.content=="string"?{role:"user",content:r.content,...r.cache?{cache_control:{type:"ephemeral"}}:{}}:{role:"user",content:r.content.map(s=>{switch(s.type){case"text":return{type:"text",text:s.text,...s.cache?{cache_control:{type:"ephemeral"}}:{}};case"image":return{type:"image",source:{type:"base64",media_type:s.mimeType,data:s.image},...s.cache?{cache_control:{type:"ephemeral"}}:{}};default:throw new Error("Invalid content type")}})};case"assistant":{let o="",s=[],a=r.thoughtBlocks;if(Array.isArray(a)&&a.length>0)for(let l of a)l.encrypted?s.push(l.signature?{type:"redacted_thinking",data:l.data,signature:l.signature}:{type:"redacted_thinking",data:l.data}):s.push(l.signature?{type:"thinking",thinking:l.data,signature:l.signature}:{type:"thinking",thinking:l.data});if(typeof r.content=="string"&&(s.length>0?o=[...s,{type:"text",text:r.content}]:o=r.content),typeof r.functionCalls<"u"&&(o=r.functionCalls.map(l=>{let u={};if(typeof l.function.params=="string"){let c=l.function.params;if(c.trim().length===0)u={};else try{u=JSON.parse(c)}catch{throw new Error(`Failed to parse function params JSON: ${c}`)}}else typeof l.function.params=="object"&&(u=l.function.params);return{type:"tool_use",id:l.id,name:l.function.name,input:u,...r.cache?{cache_control:{type:"ephemeral"}}:{}}}),Array.isArray(o)&&s.length>0&&(o=[...s,...o])),r.cache){if(typeof o=="string")o=[{type:"text",text:o,cache_control:{type:"ephemeral"}}];else if(Array.isArray(o)&&o.length>0){let l=o.length-1,u=o[l];u&&u.type==="text"&&(o[l]={...u,cache_control:{type:"ephemeral"}})}}return{role:"assistant",content:o}}default:throw new Error("Invalid role")}}),n=ac(t);return lc(n)}function ac(i){let e=[];for(let[t,n]of i.entries()){if(n.role!=="assistant"){e.push(n);continue}if(t>0&&i.at(t-1)?.role==="assistant"){let r=e.pop();e.push({...r||{},...n})}else e.push(n)}return e}function lc(i){return i.map(e=>e.role==="assistant"&&typeof e.content=="string"?{...e,content:e.content.replace(/\s+$/,"")}:e)}function wa(i){if(i)switch(i){case"stop_sequence":return"stop";case"max_tokens":return"length";case"tool_use":return"function_call";case"end_turn":return"stop";default:return"stop"}}var Yn=(L=>(L.GPT4="gpt-4",L.GPT41="gpt-4.1",L.GPT41Mini="gpt-4.1-mini",L.GPT41Nano="gpt-4.1-nano",L.GPT4O="gpt-4o",L.GPT4OMini="gpt-4o-mini",L.GPT4ChatGPT4O="chatgpt-4o-latest",L.GPT4Turbo="gpt-4-turbo",L.GPT35Turbo="gpt-3.5-turbo",L.GPT35TurboInstruct="gpt-3.5-turbo-instruct",L.GPT35TextDavinci002="text-davinci-002",L.GPT3TextBabbage002="text-babbage-002",L.GPT3TextAda001="text-ada-001",L.GPT5="gpt-5",L.GPT5Nano="gpt-5-nano",L.GPT5Mini="gpt-5-mini",L.GPT5Chat="gpt-5-chat",L.GPT5ChatLatest="gpt-5-chat-latest",L.GPT5Codex="gpt-5-codex",L.GPT5Pro="gpt-5-pro",L.GPT51="gpt-5.1",L.GPT51ChatLatest="gpt-5.1-chat-latest",L.GPT51Codex="gpt-5.1-codex",L.GPT51CodexMini="gpt-5.1-codex-mini",L.GPT51CodexMax="gpt-5.1-codex-max",L.GPT52="gpt-5.2",L.GPT52ChatLatest="gpt-5.2-chat-latest",L.GPT52Codex="gpt-5.2-codex",L.GPT52Pro="gpt-5.2-pro",L.O1="o1",L.O1Mini="o1-mini",L.O3="o3",L.O3Mini="o3-mini",L.O4Mini="o4-mini",L))(Yn||{}),Vt=(n=>(n.TextEmbeddingAda002="text-embedding-ada-002",n.TextEmbedding3Small="text-embedding-3-small",n.TextEmbedding3Large="text-embedding-3-large",n))(Vt||{});var Jt=(_=>(_.GPT4="gpt-4",_.GPT41="gpt-4.1",_.GPT41Mini="gpt-4.1-mini",_.GPT41Nano="gpt-4.1-nano",_.GPT4O="gpt-4o",_.GPT4OMini="gpt-4o-mini",_.GPT4ChatGPT4O="chatgpt-4o-latest",_.GPT4Turbo="gpt-4-turbo",_.GPT35Turbo="gpt-3.5-turbo",_.GPT35TurboInstruct="gpt-3.5-turbo-instruct",_.GPT35TextDavinci002="text-davinci-002",_.GPT3TextBabbage002="text-babbage-002",_.GPT3TextAda001="text-ada-001",_.GPT5="gpt-5",_.GPT5Nano="gpt-5-nano",_.GPT5Mini="gpt-5-mini",_.GPT5Chat="gpt-5-chat",_.GPT5ChatLatest="gpt-5-chat-latest",_.GPT5Codex="gpt-5-codex",_.GPT5Pro="gpt-5-pro",_.GPT51="gpt-5.1",_.GPT51ChatLatest="gpt-5.1-chat-latest",_.GPT51Codex="gpt-5.1-codex",_.GPT51CodexMini="gpt-5.1-codex-mini",_.GPT51CodexMax="gpt-5.1-codex-max",_.GPT52="gpt-5.2",_.GPT52ChatLatest="gpt-5.2-chat-latest",_.GPT52Codex="gpt-5.2-codex",_.GPT52Pro="gpt-5.2-pro",_.O1Pro="o1-pro",_.O1="o1",_.O1Mini="o1-mini",_.O3Pro="o3-pro",_.O3="o3",_.O3Mini="o3-mini",_.O4Mini="o4-mini",_))(Jt||{});var Qt=[{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}],zr=[{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 uc=i=>{let e=["o1","o1-mini","o3","o3-mini","o4-mini","o1-pro","o3-pro"];return e.includes(i)||e.includes(i)},_t=()=>structuredClone({model:"gpt-5-mini",embedModel:"text-embedding-3-small",...be()}),Hr=()=>structuredClone({..._t(),model:"gpt-5"}),Kr=()=>structuredClone({model:"gpt-5-mini",embedModel:"text-embedding-3-small",...Ne()}),Wr=()=>({..._t(),model:"gpt-5-nano"}),Es=class{constructor(e,t,n){this.config=e;this.streamingUsage=t;this.chatReqUpdater=n}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,endSequences:e.endSequences,topP:e.topP,n:e.n,stream:e.stream}}createChatReq=(e,t)=>{let n=e.model;if(!e.chatPrompt||e.chatPrompt.length===0)throw new Error("Chat prompt is empty");let r={name:"/chat/completions"},o=e.functions?.map(m=>({type:"function",function:{name:m.name,description:m.description,parameters:m.parameters}})),s=!e.functionCall&&e.functions&&e.functions.length>0?"auto":e.functionCall,a=cc(e),l=e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,u=e.modelConfig?.stream??this.config.stream,c=this.config.store,p=uc(n),d={model:n,messages:a,...e.responseFormat?{response_format:e.responseFormat.type==="json_schema"?{type:"json_schema",json_schema:e.responseFormat.schema}:e.responseFormat}:this.config?.responseFormat?{response_format:{type:this.config.responseFormat}}:{},...o?{tools:o}:{},...s?{tool_choice:s}:{},...p?{}:{...(e.modelConfig?.maxTokens??this.config.maxTokens)!==void 0?{max_completion_tokens:e.modelConfig?.maxTokens??this.config.maxTokens}:{},...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},...(e.modelConfig?.n??this.config.n)!==void 0?{n:e.modelConfig?.n??this.config.n}:{},...(e.modelConfig?.presencePenalty??this.config.presencePenalty)!==void 0?{presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty}:{},...l!==void 0?{frequency_penalty:l}:{}},...(e.modelConfig?.stopSequences??this.config.stop)&&(e.modelConfig?.stopSequences??this.config.stop).length>0?{stop:e.modelConfig?.stopSequences??this.config.stop}:{},...this.config.logitBias!==void 0?{logit_bias:this.config.logitBias}:{},...u&&this.streamingUsage?{stream:!0,stream_options:{include_usage:!0}}:{},...c?{store:c}:{},...this.config.serviceTier?{service_tier:this.config.serviceTier}:{},...this.config.user?{user:this.config.user}:{}};if(this.config.reasoningEffort&&(d.reasoning_effort=this.config.reasoningEffort),this.config.webSearchOptions&&(d.web_search_options={...this.config.webSearchOptions.searchContextSize&&{search_context_size:this.config.webSearchOptions.searchContextSize},...this.config.webSearchOptions.userLocation&&{user_location:{approximate:{type:"approximate",...this.config.webSearchOptions.userLocation.approximate.city&&{city:this.config.webSearchOptions.userLocation.approximate.city},...this.config.webSearchOptions.userLocation.approximate.country&&{country:this.config.webSearchOptions.userLocation.approximate.country},...this.config.webSearchOptions.userLocation.approximate.region&&{region:this.config.webSearchOptions.userLocation.approximate.region},...this.config.webSearchOptions.userLocation.approximate.timezone&&{timezone:this.config.webSearchOptions.userLocation.approximate.timezone}}}}}),t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"none":d.reasoning_effort=void 0;break;case"minimal":d.reasoning_effort="minimal";break;case"low":d.reasoning_effort="medium";break;case"medium":d.reasoning_effort="high";break;case"high":d.reasoning_effort="high";break;case"highest":d.reasoning_effort="high";break}if(!d.reasoning_effort&&t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"minimal":d.reasoning_effort="minimal";break;case"low":d.reasoning_effort="medium";break;case"medium":case"high":case"highest":d.reasoning_effort="high";break}return this.chatReqUpdater&&(d=this.chatReqUpdater(d)),[r,d]};createEmbedReq=e=>{let t=e.embedModel;if(!t)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let n={name:"/embeddings"},r={model:t,input:e.texts,dimensions:this.config.dimensions};return[n,r]};createChatResp(e){let{id:t,usage:n,choices:r,error:o}=e;if(o)throw o;return this.tokensUsed=n?{promptTokens:n.prompt_tokens,completionTokens:n.completion_tokens,totalTokens:n.total_tokens}:void 0,{results:r.map(a=>{if(a.message.refusal)throw new Re(a.message.refusal,e.model,e.id);let l=Sa(a.finish_reason),u=a.message.tool_calls?.map(({id:c,function:{arguments:p,name:d}})=>({id:c,type:"function",function:{name:d,params:p}}));return{index:a.index,id:`${a.index}`,content:a.message.content??void 0,thought:a.message.reasoning_content,citations:a.message.annotations?.filter(c=>c?.type==="url_citation"&&c.url_citation).map(c=>({url:c.url_citation?.url,title:c.url_citation?.title,description:c.url_citation?.description})),functionCalls:u,finishReason:l}}),remoteId:t}}createChatStreamResp(e,t){let{id:n,usage:r,choices:o}=e;this.tokensUsed=r?{promptTokens:r.prompt_tokens,completionTokens:r.completion_tokens,totalTokens:r.total_tokens}:void 0;let s=t;return s.indexIdMap||(s.indexIdMap={}),{results:o.map(({index:l,delta:{content:u,role:c,refusal:p,tool_calls:d,reasoning_content:m,annotations:g},finish_reason:h})=>{if(p)throw new Re(p,void 0,n);let f=Sa(h),I=d?.map(({id:x,index:A,function:{name:w,arguments:b}})=>{typeof x=="string"&&typeof A=="number"&&!s.indexIdMap[A]&&(s.indexIdMap[A]=x);let T=s.indexIdMap[A];return T?{id:T,type:"function",function:{name:w,params:b}}:null}).filter(x=>x!==null);return{index:l,content:u??void 0,role:c,thought:m,citations:g?.filter(x=>x?.type==="url_citation"&&x.url_citation).map(x=>({url:x.url_citation?.url,title:x.url_citation?.title,description:x.url_citation?.description})),functionCalls:I,finishReason:f,id:n}})}}createEmbedResp(e){let{data:t,usage:n}=e;return this.tokensUsed=n?{promptTokens:n.prompt_tokens,completionTokens:n.completion_tokens,totalTokens:n.total_tokens}:void 0,{embeddings:t.map(r=>r.embedding)}}},Sa=i=>{switch(i){case"stop":return"stop";case"length":return"length";case"content_filter":return"error";case"tool_calls":return"function_call"}};function cc(i){return i.chatPrompt.map(t=>{switch(t.role){case"system":return{role:"system",content:t.content};case"user":{let n=Array.isArray(t.content)?t.content.map(r=>{switch(r.type){case"text":return{type:"text",text:r.text};case"image":return{type:"image_url",image_url:{url:`data:${r.mimeType};base64,${r.image}`,details:r.details??"auto"}};case"audio":return{type:"input_audio",input_audio:{data:r.data,format:r.format==="wav"?"wav":void 0}};default:throw new Error("Invalid content type")}}):t.content;return{role:"user",...t.name?{name:t.name}:{},content:n}}case"assistant":{let n=t.functionCalls?.map(r=>({id:r.id,type:"function",function:{name:r.function.name,arguments:typeof r.function.params=="object"?JSON.stringify(r.function.params):r.function.params}}));if(n&&n.length>0)return{role:"assistant",...t.content?{content:t.content}:{},name:t.name,tool_calls:n};if(t.content===void 0)throw new Error("Assistant content is required when no tool calls are provided");return{role:"assistant",content:t.content,...t.name?{name:t.name}:{}}}case"function":return{role:"tool",content:t.result,tool_call_id:t.functionId};default:throw new Error("Invalid role")}})}var ke=class extends Me{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o,models:s,chatReqUpdater:a,supportFor:l}){if(!e||e==="")throw new Error("OpenAI API key not set");let u=new Es(t,n?.streamingUsage??!0,a);super(u,{name:"OpenAI",apiURL:r||"https://api.openai.com/v1",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:o,defaults:{model:t.model,embedModel:t.embedModel},options:n,supportFor:l,models:s})}},Yt=class extends ke{constructor({apiKey:e,apiURL:t,config:n,options:r,models:o,modelInfo:s}){if(!e||e==="")throw new Error("OpenAI API key not set");s=[...Qt,...s??[]];let a=u=>{let c=nt({model:u,modelInfo:s,models:o});return{functions:!0,streaming:!0,hasThinkingBudget:c?.supported?.thinkingBudget??!1,hasShowThoughts:c?.supported?.showThoughts??!1,structuredOutputs:c?.supported?.structuredOutputs??!1,media:{images:{supported:!0,formats:["image/jpeg","image/png","image/gif","image/webp"],maxSize:20*1024*1024,detailLevels:["high","low","auto"]},audio:{supported:!0,formats:["wav","mp3","ogg"],maxDuration:25*60},files:{supported:!0,formats:["text/plain","application/pdf","image/jpeg","image/png"],maxSize:512*1024*1024,uploadMethod:"upload"},urls:{supported:!1,webSearch:!0,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:c?.supported?.thinkingBudget??!1,multiTurn:!0}},l=o?.map(u=>{let c=u,p=c?.config;if(!p)return u;let d={};p.maxTokens!==void 0&&(d.maxTokens=p.maxTokens),p.temperature!==void 0&&(d.temperature=p.temperature),p.topP!==void 0&&(d.topP=p.topP),p.presencePenalty!==void 0&&(d.presencePenalty=p.presencePenalty),p.frequencyPenalty!==void 0&&(d.frequencyPenalty=p.frequencyPenalty);let m=p.stopSequences??p.stop;m!==void 0&&(d.stopSequences=m),p.n!==void 0&&(d.n=p.n),p.stream!==void 0&&(d.stream=p.stream);let g={...c};Object.keys(d).length>0&&(g.modelConfig={...c.modelConfig??{},...d});let h=p?.thinking?.thinkingTokenBudget;if(typeof h=="number"){let f=[["minimal",200],["low",800],["medium",5e3],["high",1e4],["highest",24500]],I="minimal",x=Number.POSITIVE_INFINITY;for(let[A,w]of f){let b=Math.abs(h-w);b<x&&(x=b,I=A)}g.thinkingTokenBudget=I}return p?.thinking?.includeThoughts!==void 0&&(g.showThoughts=!!p.thinking.includeThoughts),g});super({apiKey:e,apiURL:t,config:{..._t(),...n},options:r,modelInfo:s,models:l??o,supportFor:a}),super.setName("OpenAI")}};var pc=i=>{let e=i.trim();if(!e)return e;if(e.includes("api-version=")){let t=e.indexOf("api-version="),n=e.slice(t);return new URLSearchParams(n).get("api-version")??e}return e},dc=i=>{let e=i.match(/^(\d{4}-\d{2}-\d{2})/);return e?e[1]>="2024-08-01":!1},Ps=_t,va=Kr,ka=Wr,Oa=Hr,Xt=class extends ke{constructor({apiKey:e,resourceName:t,deploymentName:n,version:r="api-version=2024-02-15-preview",config:o,options:s,models:a,modelInfo:l}){if(!e||e==="")throw new Error("Azure OpenAPI API key not set");if(!t||t==="")throw new Error("Azure OpenAPI resource name not set");if(!n||n==="")throw new Error("Azure OpenAPI deployment id not set");let u={...Ps(),...o},c=pc(r),p=dc(c);l=[...Qt,...l??[]];let d=g=>{let h=nt({model:g,modelInfo:l,models:a});return{functions:!0,streaming:!0,hasThinkingBudget:h?.supported?.thinkingBudget??!1,hasShowThoughts:h?.supported?.showThoughts??!1,structuredOutputs:p&&(h?.supported?.structuredOutputs??!1),functionCot:!1,media:{images:{supported:!0,formats:["image/jpeg","image/png","image/gif","image/webp"],maxSize:20*1024*1024,detailLevels:["high","low","auto"]},audio:{supported:!1,formats:[],maxDuration:0},files:{supported:!1,formats:[],maxSize:0,uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:h?.supported?.thinkingBudget??!1,multiTurn:!0}};super({apiKey:e,config:u,options:s,models:a,modelInfo:l,supportFor:d});let m=t.includes("://")?t:`https://${t}.openai.azure.com/`;super.setName("Azure OpenAI"),super.setAPIURL(new URL(`/openai/deployments/${n}?api-version=${c}`,m).href),super.setHeaders(async()=>({"api-key":e}))}};var Vr=class i{services;currentServiceIndex=0;currentService;debug;initialBackoffMs;maxBackoffMs;maxRetries;serviceFailures=new Map;constructor(e,t){if(e.length===0)throw new Error("No AI services provided.");mc(e),this.services=[...e].sort(t?.comparator??i.metricComparator);let n=this.services[this.currentServiceIndex];if(n===void 0)throw new Error("Error initializing the AI services.");this.currentService=n,this.debug=t?.debug??!0,this.initialBackoffMs=t?.initialBackoffMs??1e3,this.maxBackoffMs=t?.maxBackoffMs??32e3,this.maxRetries=t?.maxRetries??3}static create(e,t){return new i(e,t)}getLastUsedChatModel(){return this.currentService.getLastUsedChatModel()}getLastUsedEmbedModel(){return this.currentService.getLastUsedEmbedModel()}getLastUsedModelConfig(){return this.currentService.getLastUsedModelConfig()}static inputOrderComparator=()=>0;static metricComparator=(e,t)=>{let n=e.getMetrics(),r=t.getMetrics();return n.latency.chat.mean-r.latency.chat.mean};getModelList(){for(let e of this.services){let t=e.getModelList();if(t)return t}}getNextService(e,t){let n=t+1,r=e[n];return r===void 0?{service:void 0,index:n}:{service:r,index:n}}reset(){this.currentServiceIndex=0;let e=this.services[this.currentServiceIndex];if(e===void 0)throw new Error("No AI services provided.");this.currentService=e}getName(){return this.currentService.getName()}getId(){return this.currentService.getId()}getFeatures(e){let t={functions:!1,streaming:!1,thinking:!1,multiTurn:!1,structuredOutputs:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]}};for(let n of this.services){let r=n.getFeatures(e);r.functions&&(t.functions=!0),r.streaming&&(t.streaming=!0),r.thinking&&(t.thinking=!0),r.multiTurn&&(t.multiTurn=!0),r.structuredOutputs&&(t.structuredOutputs=!0),r.functionCot&&(t.functionCot=!0),r.hasThinkingBudget&&(t.hasThinkingBudget=!0),r.hasShowThoughts&&(t.hasShowThoughts=!0),r.media.images.supported&&(t.media.images.supported=!0),t.media.images.formats=Array.from(new Set([...t.media.images.formats,...r.media.images.formats])),r.media.audio.supported&&(t.media.audio.supported=!0),t.media.audio.formats=Array.from(new Set([...t.media.audio.formats,...r.media.audio.formats])),r.media.files.supported&&(t.media.files.supported=!0),t.media.files.formats=Array.from(new Set([...t.media.files.formats,...r.media.files.formats])),r.media.files.uploadMethod!=="none"&&(t.media.files.uploadMethod=r.media.files.uploadMethod),r.media.urls.supported&&(t.media.urls.supported=!0),r.media.urls.webSearch&&(t.media.urls.webSearch=!0),r.media.urls.contextFetching&&(t.media.urls.contextFetching=!0),r.caching.supported&&(t.caching.supported=!0),t.caching.types=Array.from(new Set([...t.caching.types,...r.caching.types]))}return t}getMetrics(){let e={latency:{chat:{mean:0,p95:0,p99:0,samples:[]},embed:{mean:0,p95:0,p99:0,samples:[]}},errors:{chat:{count:0,rate:0,total:0},embed:{count:0,rate:0,total:0}}},t=0,n=0,r=0,o=0;for(let s of this.services){let a=s.getMetrics();e.errors.chat.count+=a.errors.chat.count,e.errors.chat.total+=a.errors.chat.total,e.errors.embed.count+=a.errors.embed.count,e.errors.embed.total+=a.errors.embed.total;let l=a.latency.chat.samples.length;l>0&&(t+=a.latency.chat.mean*l,n+=l);let u=a.latency.embed.samples.length;u>0&&(r+=a.latency.embed.mean*u,o+=u)}e.errors.chat.total>0&&(e.errors.chat.rate=e.errors.chat.count/e.errors.chat.total),e.errors.embed.total>0&&(e.errors.embed.rate=e.errors.embed.count/e.errors.embed.total),n>0&&(e.latency.chat.mean=t/n),o>0&&(e.latency.embed.mean=r/o);for(let s of this.services){let a=s.getMetrics();e.latency.chat.p95=Math.max(e.latency.chat.p95,a.latency.chat.p95),e.latency.chat.p99=Math.max(e.latency.chat.p99,a.latency.chat.p99),e.latency.embed.p95=Math.max(e.latency.embed.p95,a.latency.embed.p95),e.latency.embed.p99=Math.max(e.latency.embed.p99,a.latency.embed.p99)}return e}canRetryService(e){let t=this.serviceFailures.get(e.getId());if(!t)return!0;let{retries:n,lastFailureTime:r}=t,o=Date.now()-r,s=Math.min(this.initialBackoffMs*2**n,this.maxBackoffMs);return o>=s}handleFailure(e,t){let r=(this.serviceFailures.get(e.getId())?.retries??0)+1;this.serviceFailures.set(e.getId(),{retries:r,lastFailureTime:Date.now()}),this.debug&&console.warn(`AxBalancer: Service ${e.getName()} failed (retry ${r}/${this.maxRetries})`,t)}handleSuccess(e){this.serviceFailures.delete(e.getId())}async chat(e,t){let n=e.responseFormat?.type==="json_schema",r=e.capabilities,o=r?.requiresImages,s=r?.requiresAudio,a=this.services,l=e.model;if((n||o||s)&&(a=this.services.filter(p=>{let d=p.getFeatures(l);return!(n&&!d.structuredOutputs||o&&!d.media.images.supported||s&&!d.media.audio.supported)}),a.length===0)){let p=[];throw n&&p.push("structured outputs"),o&&p.push("images"),s&&p.push("audio"),new Error(`No services available that support required capabilities: ${p.join(", ")}.`)}let u=0,c=a[u];if(!c)throw new Error("No matching AI services available for request.");for(this.currentService=c;;){if(!this.canRetryService(c)){let p=this.getNextService(a,u);if(!p.service)throw new Error(`All candidate services exhausted (tried ${a.length} service(s))`);c=p.service,u=p.index,this.currentService=c;continue}try{let p=await c.chat(e,t);return this.handleSuccess(c),p}catch(p){if(!(p instanceof je))throw p;switch(p.constructor){case St:throw p;case Ve:{if(![408,429,500,502,503,504].includes(p.status))throw p;break}case De:break;case Rt:break;case rt:break;case tt: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 Ve&&![408,429,500,502,503,504].includes(r.status)||r instanceof St)throw r;if(this.handleFailure(this.currentService,r),(this.serviceFailures.get(this.currentService.getId())?.retries??0)>=this.maxRetries){let s=this.getNextService(this.services,n);if(!s.service)throw r;this.currentService=s.service,n=s.index,this.currentServiceIndex=n}}}}setOptions(e){for(let t of this.services)t.setOptions(e);this.currentService.setOptions(e),this.debug=e.debug??this.debug}getOptions(){return this.currentService.getOptions()}getLogger(){return this.currentService.getLogger()}};function mc(i){let e=i.find(r=>r.getModelList()!==void 0);if(!e)return;let t=e.getModelList();if(!t)throw new Error("No model list found in any service.");let n=new Set(t.map(r=>r.key));for(let r=0;r<i.length;r++){let o=i[r];if(!o)throw new Error(`Service at index ${r} is undefined`);let s=o.getModelList();if(!s)throw new Error(`Service at index ${r} (${o.getName()}) has no model list while another service does.`);let a=new Set(s.map(l=>l.key));for(let l of n)if(!a.has(l))throw new Error(`Service at index ${r} (${o.getName()}) is missing model "${l}"`);for(let l of a)if(!n.has(l))throw new Error(`Service at index ${r} (${o.getName()}) has extra model "${l}"`)}}function Nt(i){let e=!1,t=!1,n=!1,r=!1,o=!1,s=!1,a=!1,l=new Set,u=0;if(i.chatPrompt&&Array.isArray(i.chatPrompt))for(let c of i.chatPrompt){if(c.role==="user"&&Array.isArray(c.content))for(let p of c.content)switch(l.add(p.type),p.type){case"image":e=!0,p.cache&&(a=!0),u+=85;break;case"audio":t=!0,p.cache&&(a=!0),u+=p.duration||60;break;case"file":n=!0,p.cache&&(a=!0),u+=Math.ceil((p.extractedText?.length||1e3)/4);break;case"url":r=!0,p.cache&&(a=!0),u+=Math.ceil((p.cachedContent?.length||2e3)/4);break;case"text":p.cache&&(a=!0),u+=Math.ceil(p.text.length/4);break}else"content"in c&&typeof c.content=="string"&&(u+=Math.ceil(c.content.length/4));"cache"in c&&c.cache&&(a=!0)}return i.functions&&i.functions.length>0&&(o=!0),i.modelConfig?.stream===!0&&(s=!0),i.capabilities&&(i.capabilities.requiresImages&&(e=!0),i.capabilities.requiresAudio&&(t=!0),i.capabilities.requiresFiles&&(n=!0),i.capabilities.requiresWebSearch&&(r=!0)),{hasImages:e,hasAudio:t,hasFiles:n,hasUrls:r,requiresFunctions:o,requiresStreaming:s,requiresCaching:a,contentTypes:l,estimatedTokens:u}}function Fs(i,e){let t=i.getFeatures(),n=[],r=[],o=[];if(e.hasImages&&!t.media.images.supported&&(n.push("Image support"),o.push("Use altText for images or imageToText service")),e.hasAudio&&!t.media.audio.supported&&(n.push("Audio support"),o.push("Pre-transcribe audio or use transcription field")),e.hasFiles&&!t.media.files.supported&&(n.push("File support"),o.push("Pre-extract text content or use extractedText field")),e.hasUrls&&!t.media.urls.supported&&(n.push("URL/Web search support"),o.push("Pre-fetch content or use cachedContent field")),e.requiresFunctions&&!t.functions&&n.push("Function calling"),e.requiresStreaming&&!t.streaming&&(n.push("Streaming responses"),o.push("Use non-streaming mode")),e.requiresCaching&&!t.caching.supported&&(n.push("Content caching"),o.push("Repeated content will not be cached")),e.hasImages&&t.media.images.supported){let a=t.media.images.maxSize;a&&a<10*1024*1024&&r.push(`Image size limit is ${Math.round(a/(1024*1024))}MB`)}if(e.hasAudio&&t.media.audio.supported){let a=t.media.audio.maxDuration;a&&a<600&&r.push(`Audio duration limit is ${Math.round(a/60)} minutes`)}return{isSupported:n.length===0,missingCapabilities:n,warnings:r,alternatives:o}}function Jr(i,e){return i.map(t=>{let n=t.getFeatures(),r=Fs(t,e),o=0,s=[];return o+=10,e.hasImages&&n.media.images.supported&&(o+=25,s.push("Images"),n.media.images.detailLevels?.includes("high")&&(o+=5),n.media.images.maxSize&&n.media.images.maxSize>10*1024*1024&&(o+=3)),e.hasAudio&&n.media.audio.supported&&(o+=25,s.push("Audio"),n.media.audio.maxDuration&&n.media.audio.maxDuration>600&&(o+=5)),e.hasFiles&&n.media.files.supported&&(o+=25,s.push("Files"),n.media.files.uploadMethod==="cloud"&&(o+=3)),e.hasUrls&&n.media.urls.supported&&(o+=25,s.push("URLs"),n.media.urls.webSearch&&(o+=5)),e.requiresFunctions&&n.functions&&(o+=15,s.push("Functions"),n.functionCot&&(o+=3)),e.requiresStreaming&&n.streaming&&(o+=10,s.push("Streaming")),e.requiresCaching&&n.caching.supported&&(o+=8,s.push("Caching"),n.caching.types.includes("persistent")&&(o+=3)),n.thinking&&(o+=2),n.multiTurn&&(o+=2),n.hasThinkingBudget&&(o+=1),n.hasShowThoughts&&(o+=1),o-=r.missingCapabilities.length*10,{provider:t,score:o,missingCapabilities:r.missingCapabilities,supportedCapabilities:s}}).sort((t,n)=>n.score-t.score)}function Qr(i,e,t={}){if(e.length===0)throw new Error("No providers available");let n=Nt(i),r=Jr(e,n);if(t.requireExactMatch){let o=r.filter(s=>s.missingCapabilities.length===0);if(o.length===0)throw new Error(`No providers fully support the request requirements: ${r[0]?.missingCapabilities.join(", ")||"unknown requirements"}`);return o[0].provider}if(!t.allowDegradation){let o=r[0];if(o.missingCapabilities.length>0)throw new Error(`Best available provider (${o.provider.getName()}) is missing: ${o.missingCapabilities.join(", ")}`)}return r[0].provider}function Ma(i,e){let t=Nt(i),n=Jr(e,t),r=n[0]?.provider||null,o=[t.hasImages&&"images",t.hasAudio&&"audio",t.hasFiles&&"files",t.hasUrls&&"URLs",t.requiresFunctions&&"functions",t.requiresStreaming&&"streaming",t.requiresCaching&&"caching"].filter(Boolean).length,s=r?n[0].supportedCapabilities.length:0,a=r?`${r.getName()} supports ${s}/${o} requirements (${Math.round(s/Math.max(o,1)*100)}% compatibility)`:"No suitable providers found";return{requirements:t,providerScores:n,recommendedProvider:r,summary:a}}function Ea(i,e){return i.filter(t=>t.getFeatures().media[e].supported)}function Pa(i,e){let t={};for(let n of i){let o=n.getFeatures().media[e];if(o.supported)for(let s of o.formats)t[s]||(t[s]=[]),t[s].push(n)}return t}var Xn=(r=>(r.CommandRPlus="command-r-plus",r.CommandR="command-r",r.Command="command",r.CommandLight="command-light",r))(Xn||{}),Zn=(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))(Zn||{});var Yr=[{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 gc(i){if(Array.isArray(i)){let t=["string","number","integer","boolean","object","array"].find(n=>i.includes(n));return t||(i.find(n=>n!=="null")??i[0]??"string")}return typeof i=="string"?i:"string"}var Ns=()=>structuredClone({model:"command-r-plus",embedModel:"embed-english-v3.0",...be()}),_a=()=>structuredClone({model:"command-r",embedModel:"embed-english-v3.0",...Ne()}),_s=class{constructor(e){this.config=e}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,frequencyPenalty:e.frequencyPenalty,presencePenalty:e.presencePenalty,endSequences:e.endSequences,stopSequences:e.stopSequences,stream:e.stream,n:e.n}}createChatReq(e){let t=e.model,n=e.chatPrompt.at(-1),r=e.chatPrompt.slice(0,-1),o;n&&n.role==="user"&&typeof n.content=="string"&&(o=n?.content);let s=hc(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:gc(g.type),required:p.parameters.required?.includes(m)??!1};return{name:p.name,description:p.description,parameter_definitions:d}}),l=e.chatPrompt.filter(p=>p.role==="function").map(p=>{let d=a?.find(m=>m.name===p.functionId);if(!d)throw new Error("Function not found");return{call:{name:d.name,parameters:d.parameter_definitions},outputs:[{result:p.result??""}]}}),u={name:"/chat"},c={message:o,model:t,tools:a,...l&&!o?{tool_results:l}:{},chat_history:s,max_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},k:e.modelConfig?.topK??this.config.topK,...e.modelConfig?.topP!==void 0?{p:e.modelConfig.topP}:{},frequency_penalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,end_sequences:this.config.endSequences,stop_sequences:e.modelConfig?.stopSequences??this.config.stopSequences};return[u,c]}createEmbedReq=e=>{let t=e.embedModel;if(!t)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let n={name:"/embed"},r={model:t,texts:e.texts??[],input_type:"classification",truncate:""};return[n,r]};createChatResp=e=>{this.tokensUsed=e.meta.billed_units?{promptTokens:e.meta.billed_units.input_tokens,completionTokens:e.meta.billed_units.output_tokens,totalTokens:e.meta.billed_units.input_tokens+e.meta.billed_units.output_tokens}:void 0;let t;if("finish_reason"in e)switch(e.finish_reason){case"COMPLETE":t="stop";break;case"MAX_TOKENS":t="length";break;case"ERROR":throw new Error("Finish reason: ERROR");case"ERROR_TOXIC":throw new Error("Finish reason: CONTENT_FILTER");default:t="stop";break}let n;return"tool_calls"in e&&(n=e.tool_calls?.map(o=>({id:o.name,type:"function",function:{name:o.name,params:o.parameters}}))),{results:[{index:0,id:e.generation_id,content:e.text,functionCalls:n,finishReason:t}],remoteId:e.response_id}};createChatStreamResp=(e,t)=>{let n=t;e.event_type==="stream-start"&&(n.generation_id=e.generation_id),this.tokensUsed={promptTokens:0,completionTokens:e.meta.billed_units?.output_tokens??0,totalTokens:e.meta.billed_units?.output_tokens??0};let{results:r}=this.createChatResp(e),o=r[0];if(!o)throw new Error("No result");return o.id=n.generation_id??"",{results:r}};createEmbedResp(e){return{remoteId:e.id,embeddings:e.embeddings}}},Zt=class extends Me{constructor({apiKey:e,config:t,options:n,models:r}){if(!e||e==="")throw new Error("Cohere API key not set");let o={...Ns(),...t},s=new _s(o),a=r?.map(l=>{let u=l,c=u?.config;if(!c)return l;let p={};c.maxTokens!==void 0&&(p.maxTokens=c.maxTokens),c.temperature!==void 0&&(p.temperature=c.temperature),c.topP!==void 0&&(p.topP=c.topP),c.topK!==void 0&&(p.topK=c.topK),c.presencePenalty!==void 0&&(p.presencePenalty=c.presencePenalty),c.frequencyPenalty!==void 0&&(p.frequencyPenalty=c.frequencyPenalty),c.stopSequences!==void 0&&(p.stopSequences=c.stopSequences),c.endSequences!==void 0&&(p.endSequences=c.endSequences),c.stream!==void 0&&(p.stream=c.stream),c.n!==void 0&&(p.n=c.n);let d={...u};return Object.keys(p).length>0&&(d.modelConfig={...u.modelConfig??{},...p}),d});super(s,{name:"Cohere",apiURL:"https://api.cohere.ai/v1",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:Yr,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 hc(i){return i.map(e=>{let t="";if(e.role==="system"||e.role==="assistant"||e.role==="user")if(typeof e.content=="string")t=e.content;else throw new Error("Multi-modal content not supported");switch(e.role){case"user":return{role:"USER",message:t};case"system":return{role:"SYSTEM",message:t};case"assistant":{let n=Fa(e.functionCalls);return{role:"CHATBOT",message:t,tool_calls:n}}case"function":{let n=i.map(s=>{if(s.role==="assistant")return s.functionCalls?.find(a=>a.id===e.functionId)}).filter(s=>s!==void 0),r=Fa(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 Fa(i){return i?.map(e=>{let t;if(typeof e.function.params=="string"){let n=e.function.params;if(n.trim().length===0)t={};else try{t=JSON.parse(n)}catch{throw new Error(`Failed to parse function params JSON: ${n}`)}}else t=e.function.params;return{name:e.function.name,parameters:t}})}var er=(n=>(n.DeepSeekChat="deepseek-chat",n.DeepSeekCoder="deepseek-coder",n.DeepSeekReasoner="deepseek-reasoner",n))(er||{});var Xr=[{name:"deepseek-chat",currency:"USD",promptTokenCostPer1M:.27,completionTokenCostPer1M:1.1},{name:"deepseek-reasoner",currency:"USD",promptTokenCostPer1M:.55,completionTokenCostPer1M:2.19}];var Ls=()=>structuredClone({model:"deepseek-chat",...be()}),Na=()=>structuredClone({model:"deepseek-coder",...Ne()}),en=class extends ke{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("DeepSeek API key not set");let s={...Ls(),...t};o=[...Xr,...o??[]],super({apiKey:e,config:s,options:n,apiURL:"https://api.deepseek.com",modelInfo:o,supportFor:{functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},models:r}),super.setName("DeepSeek")}};var tr=(A=>(A.Gemini31Pro="gemini-3.1-pro-preview",A.Gemini3Flash="gemini-3-flash-preview",A.Gemini3Pro="gemini-3-pro-preview",A.Gemini3ProImage="gemini-3-pro-image-preview",A.Gemini25Pro="gemini-2.5-pro",A.Gemini25Flash="gemini-2.5-flash",A.Gemini25FlashLite="gemini-2.5-flash-lite",A.Gemini20Flash="gemini-2.0-flash",A.Gemini20FlashLite="gemini-2.0-flash-lite",A.Gemini20ProExp="gemini-2.0-pro-exp-02-05",A.Gemini20FlashThinkingExp="gemini-2.0-flash-thinking-exp-01-21",A.Gemini1Pro="gemini-1.0-pro",A.Gemini15Flash="gemini-1.5-flash",A.Gemini15Flash002="gemini-1.5-flash-002",A.Gemini15Flash8B="gemini-1.5-flash-8b",A.Gemini15Pro="gemini-1.5-pro",A.GeminiFlashLatest="gemini-flash-latest",A.GeminiFlashLiteLatest="gemini-flash-lite-latest",A.GeminiProLatest="gemini-pro-latest",A))(tr||{}),Zr=(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))(Zr||{}),eo=(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))(eo||{}),to=(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))(to||{}),$s=(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))($s||{}),La=["gemini-3.1-pro-preview","gemini-3.1-pro-preview-customtools","gemini-3-flash-preview","gemini-3-pro-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 no=[{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-pro-preview",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:2,completionTokenCostPer1M:12,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 Lt=i=>i.includes("gemini-3"),$a=i=>i.includes("gemini-3")&&i.includes("pro"),ro=i=>{if(!i||typeof i!="object")return i;let e={...i};return delete e.additionalProperties,delete e.default,delete e.optional,delete e.maximum,delete e.oneOf,delete e.anyOf,Array.isArray(e.type)&&(e.type=e.type.includes("object")?"object":e.type[0]??"string"),e.properties&&typeof e.properties=="object"&&(e.properties=Object.fromEntries(Object.entries(e.properties).map(([t,n])=>[t,ro(n)]))),e.items&&(e.items=ro(e.items)),e},Ga=[{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"}],Ds=()=>structuredClone({model:"gemini-2.5-flash",embedModel:"text-embedding-005",safetySettings:Ga,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...be()}),Da=()=>structuredClone({model:"gemini-2.0-flash",embedModel:"text-embedding-005",safetySettings:Ga,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...Ne()}),Gs=class{constructor(e,t,n,r,o){this.config=e;this.isVertex=t;this.endpointId=n;this.apiKey=r;this.options=o;if(!this.isVertex&&this.config.autoTruncate)throw new Error("Auto truncate is not supported for GoogleGemini");let s=this.config.model;if(Lt(s)){if(this.config.thinking?.thinkingTokenBudget!==void 0&&typeof this.config.thinking.thinkingTokenBudget=="number")throw new Error(`Gemini 3 models (${s}) do not support numeric thinkingTokenBudget. Use thinkingLevel ('low', 'medium', 'high') instead, or pass thinkingTokenBudget as a string level via options.`);if($a(s)&&this.config.thinking?.thinkingLevel){let a=this.config.thinking.thinkingLevel;if(a!=="low"&&a!=="high")throw new Error(`Gemini 3 Pro (${s}) only supports thinkingLevel 'low' or 'high', got '${a}'. Use 'low' for less thinking or 'high' for more thinking.`)}}}tokensUsed;models;setModels(e){this.models=e}getEffectiveMappings(e){let t=this.models?.find(n=>n.model===e);return{thinkingLevelMapping:{...this.config.thinkingLevelMapping,...t?.thinkingLevelMapping??{}},thinkingTokenBudgetLevels:{...this.config.thinkingTokenBudgetLevels,...t?.thinkingTokenBudgetLevels??{}}}}getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,endSequences:e.endSequences,stream:e.stream,n:e.n}}createChatReq=async(e,t)=>{let n=e.model,r=e.modelConfig?.stream??this.config.stream;if(!e.chatPrompt||e.chatPrompt.length===0)throw new Error("Chat prompt is empty");let o;if(this.endpointId?o={name:r?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:o={name:r?`/models/${n}:streamGenerateContent?alt=sse`:`/models/${n}:generateContent`},!this.isVertex){let x=r?"&":"?",A=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;o.name+=`${x}key=${A}`}let s=e.chatPrompt.filter(x=>x.role==="system").map(x=>x.content),a=s.length>0?{role:"user",parts:[{text:s.join(" ")}]}:void 0,l=[],u=e.chatPrompt.filter(x=>x.role!=="system");for(let x=0;x<u.length;x++){let A=u[x];switch(A.role){case"user":{let w=Array.isArray(A.content)?A.content.map((b,T)=>{switch(b.type){case"text":return{text:b.text};case"image":return{inlineData:{mimeType:b.mimeType,data:b.image}};case"audio":return{inlineData:{mimeType:`audio/${b.format??"mp3"}`,data:b.data}};case"file":return"fileUri"in b?{fileData:{mimeType:b.mimeType,fileUri:b.fileUri}}:{inlineData:{mimeType:b.mimeType,data:b.data}};default:throw new Error(`Chat prompt content type not supported (index: ${T})`)}}):[{text:A.content}];l.push({role:"user",parts:w});break}case"assistant":{let w=[],b=A.thoughtBlocks,T=A.functionCalls&&A.functionCalls.length>0,v=b?.[0],F=b?.map(D=>D.data).join("")??"",O=v?.signature;if(F&&w.push({...T?{}:{thought:!0},text:F,...O&&!T?{thought_signature:O}:{}}),A.functionCalls){let D=A.functionCalls.map((B,Q)=>{let S;if(typeof B.function.params=="string"){let N=B.function.params;if(N.trim().length===0)S={};else try{S=JSON.parse(N)}catch{throw new Error(`Failed to parse function params JSON: ${N}`)}}else S=B.function.params;let G={functionCall:{name:B.function.name,args:S}};return O&&Q===0&&(G.thought_signature=O),G});w.push(...D)}if(A.content&&w.push({text:A.content}),w.length===0)throw new Error("Assistant content is empty");l.push({role:"model",parts:w});break}case"function":{let w=[],b=A,T=x;for(;;){if(!("functionId"in b))throw new Error(`Chat prompt functionId is empty (index: ${T})`);if(w.push({functionResponse:{name:b.functionId,response:{result:b.result}}}),T+1<u.length&&u[T+1].role==="function")T++,b=u[T];else break}x=T,l.push({role:"user",parts:w});break}default:throw new Error(`Invalid role: ${JSON.stringify(A)} (index: ${x})`)}}let c=[];if(e.functions&&e.functions.length>0){let x=e.functions.map(A=>{let w={type:"object",properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]},b=A.parameters?ro(A.parameters):void 0;return b===void 0||b&&typeof b=="object"&&Object.keys(b).length===0?b={...w}:b&&typeof b=="object"&&b.type==="object"&&(!("properties"in b)||!b.properties||Object.keys(b.properties).length===0)&&(b={...b,properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]}),{name:A.name,description:A.description,parameters:b}});c.push({function_declarations:x})}if(this.options?.codeExecution&&c.push({code_execution:{}}),this.options?.googleSearchRetrieval&&c.push({google_search_retrieval:{dynamic_retrieval_config:this.options.googleSearchRetrieval}}),this.options?.googleSearch&&c.push({google_search:{}}),this.options?.googleMaps){let x=this.options.googleMaps,A=x?.enableWidget!==void 0?{enableWidget:x.enableWidget}:{};c.push({google_maps:A})}this.options?.urlContext&&c.push({url_context:{}}),c.length===0&&(c=void 0);let p,d=Array.isArray(c)?c.some(x=>x&&Array.isArray(x.function_declarations)&&x.function_declarations.length>0):!1;if(e.functionCall)if(e.functionCall==="none")p={function_calling_config:{mode:"NONE"}};else if(e.functionCall==="auto")p={function_calling_config:{mode:"AUTO"}};else if(e.functionCall==="required")p={function_calling_config:{mode:"ANY"}};else{let x=e.functionCall.function?.name?{allowedFunctionNames:[e.functionCall.function.name]}:{};p={function_calling_config:{mode:"ANY"},...x}}else d&&(p={function_calling_config:{mode:"AUTO"}});this.options?.retrievalConfig&&(p={...p??{},retrievalConfig:{...this.options.retrievalConfig.latLng?{latLng:this.options.retrievalConfig.latLng}:{}}});let m={};if(this.config.thinking?.includeThoughts&&(m.includeThoughts=!0),this.config.thinking?.thinkingTokenBudget&&(m.thinkingBudget=this.config.thinking.thinkingTokenBudget),this.config.thinking?.thinkingLevel&&Lt(n)&&(m.thinkingLevel=this.config.thinking.thinkingLevel),t?.thinkingTokenBudget){let x=this.getEffectiveMappings(n),A=x.thinkingTokenBudgetLevels;if(Lt(n)){let b=$a(n),T=x.thinkingLevelMapping;if(t.thinkingTokenBudget==="none")m.thinkingLevel=T?.minimal??"minimal";else{let v=t.thinkingTokenBudget,F=T?.[v];F||(F=v==="highest"?"high":v),m.thinkingLevel=F}if(b&&m.thinkingLevel){let v=m.thinkingLevel;v!=="low"&&v!=="high"&&(m.thinkingLevel=v==="minimal"?"low":"high")}}else switch(t.thinkingTokenBudget){case"none":m.thinkingBudget=0,m.includeThoughts=!1,delete m.thinkingLevel;break;case"minimal":m.thinkingBudget=A?.minimal??200;break;case"low":m.thinkingBudget=A?.low??800;break;case"medium":m.thinkingBudget=A?.medium??5e3;break;case"high":m.thinkingBudget=A?.high??1e4;break;case"highest":m.thinkingBudget=A?.highest??24500;break}}m.thinkingLevel&&delete m.thinkingBudget,Lt(n)||delete m.thinkingLevel,Lt(n)&&delete m.thinkingBudget;let g=e.modelConfig?.maxTokens??this.config.maxTokens;if(m.thinkingLevel&&g!==void 0)throw new Error("Cannot set maxTokens when using thinkingLevel with Gemini models. When thinking is enabled, the model manages output tokens automatically. Remove the maxTokens setting or disable thinking.");t?.showThoughts!==void 0&&t?.thinkingTokenBudget!=="none"&&(m.includeThoughts=t.showThoughts);let h={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,temperature:e.modelConfig?.temperature??this.config.temperature,...e.modelConfig?.topP!==void 0?{topP:e.modelConfig.topP}:{},topK:e.modelConfig?.topK??this.config.topK,frequencyPenalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,candidateCount:1,stopSequences:e.modelConfig?.stopSequences??this.config.stopSequences,responseMimeType:"text/plain",...Object.keys(m).length>0?{thinkingConfig:m}:{}};if(Lt(n)&&(h.temperature===void 0||h.temperature<1)&&(h.temperature=1),e.responseFormat){if(h.responseMimeType="application/json",e.responseFormat.type==="json_schema"&&e.responseFormat.schema){let x=e.responseFormat.schema.schema||e.responseFormat.schema;h.responseSchema=ro(x)}}else this.config.responseFormat&&this.config.responseFormat==="json_object"&&(h.responseMimeType="application/json");let f=this.config.safetySettings;return[o,{contents:l,tools:c,toolConfig:p,systemInstruction:a,generationConfig:h,safetySettings:f}]};createEmbedReq=async e=>{let t=e.embedModel;if(!t)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let n,r;if(this.isVertex)this.endpointId?n={name:`/${this.endpointId}:predict`}:n={name:`/models/${t}:predict`},r={instances:e.texts.map(o=>({content:o,...this.config.embedType&&{taskType:this.config.embedType}})),parameters:{autoTruncate:this.config.autoTruncate,outputDimensionality:this.config.dimensions}};else{let o=typeof this.apiKey=="function"?this.apiKey():this.apiKey;n={name:`/models/${t}:batchEmbedContents?key=${o}`},r={requests:e.texts.map(s=>({model:`models/${t}`,content:{parts:[{text:s}]},outputDimensionality:this.config.dimensions,...this.config.embedType&&{taskType:this.config.embedType}}))}}return[n,r]};createChatResp=e=>{let t,n=e.candidates?.map(o=>{let s={index:0};switch(o.finishReason){case"MAX_TOKENS":s.finishReason="length";break;case"STOP":s.finishReason="stop";break;case"SAFETY":throw new Re("Content was blocked due to safety settings",void 0,void 0);case"RECITATION":throw new Re("Content was blocked due to recitation policy",void 0,void 0);case"MALFORMED_FUNCTION_CALL":throw new Re("Function call was malformed and blocked",void 0,void 0);case"UNEXPECTED_TOOL_CALL":throw new Re("Unexpected tool call",void 0,void 0);case"FINISH_REASON_UNSPECIFIED":throw new Re("Finish reason unspecified",void 0,void 0);case"BLOCKLIST":throw new Re("Content was blocked due to blocklist",void 0,void 0);case"PROHIBITED_CONTENT":throw new Re("Content was blocked due to prohibited content",void 0,void 0);case"SPII":throw new Re("Content was blocked due to SPII",void 0,void 0);case"OTHER":throw new Re("Other finish reason",void 0,void 0)}if(!o.content||!o.content.parts)return s;for(let u of o.content.parts){if("text"in u){if("thought"in u&&u.thought||u.thought===!0){s.thought=u.text;let c=u.thoughtSignature||u.thought_signature;s.thoughtBlocks||(s.thoughtBlocks=[]),s.thoughtBlocks.push({data:u.text,encrypted:!1,...c?{signature:c}:{}})}else s.content=u.text;continue}if("functionCall"in u){let c=u.thoughtSignature||u.thought_signature;if(c)if(!s.thoughtBlocks||s.thoughtBlocks.length===0)s.thoughtBlocks=[{data:"",encrypted:!1,signature:c}];else{let p=s.thoughtBlocks[s.thoughtBlocks.length-1];p&&!p.signature&&(p.signature=c)}s.functionCalls=[...s.functionCalls??[],{id:Be(),type:"function",function:{name:u.functionCall.name,params:u.functionCall.args}}]}}let a=o.citationMetadata?.citations;if(Array.isArray(a)&&a.length){let u=c=>c?`${c.year}-${String(c.month).padStart(2,"0")}-${String(c.day).padStart(2,"0")}`:void 0;s.citations=a.filter(c=>typeof c?.uri=="string").map(c=>({url:c.uri,title:c.title,license:c.license,publicationDate:u(c.publicationDate)}))}let l=o.groundingMetadata;if(l){if(Array.isArray(l.groundingChunks)){let u=l.groundingChunks.map(c=>c?.maps).filter(c=>c&&typeof c.uri=="string").map(c=>({url:c.uri,title:c.title}));u.length&&(s.citations=[...s.citations??[],...u])}typeof l.googleMapsWidgetContextToken=="string"&&(t=l.googleMapsWidgetContextToken)}return s});e.usageMetadata&&(this.tokensUsed={totalTokens:e.usageMetadata.totalTokenCount,promptTokens:e.usageMetadata.promptTokenCount,completionTokens:e.usageMetadata.candidatesTokenCount,thoughtsTokens:e.usageMetadata.thoughtsTokenCount,...e.usageMetadata.cachedContentTokenCount!==void 0?{cacheReadTokens:e.usageMetadata.cachedContentTokenCount}:{}});let r={results:n};return t&&(r.providerMetadata={...r.providerMetadata,google:{...r.providerMetadata?.google??{},mapsWidgetContextToken:t}}),r};createChatStreamResp=e=>this.createChatResp(e);createEmbedResp=e=>{let t;return this.isVertex?t=e.predictions.map(n=>n.embeddings.values):t=e.embeddings.map(n=>n.values),{embeddings:t}};supportsContextCache=e=>{let t=e;return La.some(n=>t.includes(n)||n.includes(t))};buildCacheCreateOp=(e,t)=>{let n=e.model,r=t.contextCache?.ttlSeconds??3600,{systemInstruction:o,contents:s}=this.extractCacheableContent(e.chatPrompt);if(!o&&(!s||s.length===0))return;let a={model:this.isVertex?n:`models/${n}`,ttl:`${r}s`,displayName:`ax-cache-${Date.now()}`};o&&(a.systemInstruction=o),s&&s.length>0&&(a.contents=s);let l;if(this.isVertex)l="/cachedContents";else{l="/cachedContents";let u=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;l+=`?key=${u}`}return{type:"create",apiConfig:{name:l},request:a,parseResponse:u=>{let c=u;if(c?.name)return{name:c.name,expiresAt:c.expireTime,tokenCount:c.usageMetadata?.totalTokenCount}}}};buildCacheUpdateTTLOp=(e,t)=>{let n={ttl:`${t}s`},r=`/${e}`;if(!this.isVertex&&this.apiKey){let o=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;r+=`?key=${o}`}return{type:"update",apiConfig:{name:r,headers:{"Content-Type":"application/json"}},request:n,parseResponse:o=>{let s=o;if(s?.name)return{name:s.name,expiresAt:s.expireTime,tokenCount:s.usageMetadata?.totalTokenCount}}}};buildCacheDeleteOp=e=>{let t=`/${e}`;if(!this.isVertex&&this.apiKey){let n=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;t+=`?key=${n}`}return{type:"delete",apiConfig:{name:t,headers:{"Content-Type":"application/json"}},request:{},parseResponse:()=>{}}};prepareCachedChatReq=async(e,t,n)=>{let r=e.model,o=e.modelConfig?.stream??this.config.stream,{dynamicContents:s,dynamicSystemInstruction:a}=this.extractDynamicContent(e.chatPrompt),l;if(this.endpointId?l={name:o?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:l={name:o?`/models/${r}:streamGenerateContent?alt=sse`:`/models/${r}:generateContent`},!this.isVertex){let d=o?"&":"?",m=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;l.name+=`${d}key=${m}`}let u={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,temperature:e.modelConfig?.temperature??this.config.temperature,...e.modelConfig?.topP!==void 0?{topP:e.modelConfig.topP}:{},topK:e.modelConfig?.topK??this.config.topK,frequencyPenalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,candidateCount:1,stopSequences:e.modelConfig?.stopSequences??this.config.stopSequences,responseMimeType:"text/plain"};Lt(r)&&(u.temperature===void 0||u.temperature<1)&&(u.temperature=1);let c=this.config.safetySettings,p={contents:s,cachedContent:n,generationConfig:u,safetySettings:c};return a&&(p.systemInstruction=a),{apiConfig:l,request:p}};extractCacheableContent(e){let t,n=[],r=-1;for(let o=e.length-1;o>=0;o--){let s=e[o];if("cache"in s&&s.cache){r=o;break}}for(let o=0;o<e.length;o++){let s=e[o];if(s.role==="system"){t={role:"user",parts:[{text:s.content}]};continue}if(r>=0&&o<=r)if(s.role==="user"){let a=[];if(typeof s.content=="string")a.push({text:s.content});else if(Array.isArray(s.content))for(let l of s.content)switch(l.type){case"text":a.push({text:l.text});break;case"image":a.push({inlineData:{mimeType:l.mimeType,data:l.image}});break;case"audio":a.push({inlineData:{mimeType:`audio/${l.format??"mp3"}`,data:l.data}});break;case"file":"fileUri"in l?a.push({fileData:{mimeType:l.mimeType,fileUri:l.fileUri}}):a.push({inlineData:{mimeType:l.mimeType,data:l.data}});break}a.length>0&&n.push({role:"user",parts:a})}else s.role==="assistant"&&s.content&&n.push({role:"model",parts:[{text:s.content}]})}return{systemInstruction:t,contents:n}}extractDynamicContent(e){let n=[];for(let r of e)if(r.role!=="system"&&!("cache"in r&&r.cache))if(r.role==="user"){let o=[];if(typeof r.content=="string")o.push({text:r.content});else if(Array.isArray(r.content)){for(let s of r.content)if(!("cache"in s&&s.cache))switch(s.type){case"text":o.push({text:s.text});break;case"image":o.push({inlineData:{mimeType:s.mimeType,data:s.image}});break;case"audio":o.push({inlineData:{mimeType:`audio/${s.format??"mp3"}`,data:s.data}});break;case"file":"fileUri"in s?o.push({fileData:{mimeType:s.mimeType,fileUri:s.fileUri}}):o.push({inlineData:{mimeType:s.mimeType,data:s.data}});break}}o.length>0&&n.push({role:"user",parts:o})}else if(r.role==="assistant"){let o=[];if(r.content&&o.push({text:r.content}),r.functionCalls)for(let s of r.functionCalls){let a;if(typeof s.function.params=="string")try{a=JSON.parse(s.function.params)}catch{a={}}else a=s.function.params??{};o.push({functionCall:{name:s.function.name,args:a}})}o.length>0&&n.push({role:"model",parts:o})}else r.role==="function"&&n.push({role:"user",parts:[{functionResponse:{name:r.functionId,response:{result:r.result}}}]});return{dynamicContents:n,dynamicSystemInstruction:void 0}}},tn=class i extends Me{static create(e){return new i(e)}constructor({apiKey:e,projectId:t,region:n,endpointId:r,config:o,options:s,models:a,modelInfo:l}){let u=t!==void 0&&n!==void 0,c,p;if(u){if(!e)throw new Error("GoogleGemini Vertex API key not set");if(typeof e!="function")throw new Error("GoogleGemini Vertex API key must be a function for token-based authentication");let f;r?f="endpoints":f="publishers/google",c=`https://${n==="global"?"aiplatform":`${n}-aiplatform`}.googleapis.com/v1/projects/${t}/locations/${n}/${f}`,p=async()=>({Authorization:`Bearer ${typeof e=="function"?await e():e}`})}else{if(!e)throw new Error("GoogleGemini AI API key not set");c="https://generativelanguage.googleapis.com/v1beta",p=async()=>({})}let d={...Ds(),...o},m=new Gs(d,u,r,e,s);l=[...no,...l??[]];let g=f=>{let I=nt({model:f,modelInfo:l,models:a});return{functions:!0,streaming:!0,hasThinkingBudget:I?.supported?.thinkingBudget??!1,hasShowThoughts:I?.supported?.showThoughts??!1,structuredOutputs:I?.supported?.structuredOutputs??!1,media:{images:{supported:!0,formats:["image/jpeg","image/png","image/gif","image/webp"],maxSize:20*1024*1024,detailLevels:["high","low","auto"]},audio:{supported:!0,formats:["wav","mp3","aac","ogg"],maxDuration:9.5*60},files:{supported:!0,formats:["application/pdf","text/plain","text/csv","text/html","text/xml"],maxSize:2*1024*1024*1024,uploadMethod:"cloud"},urls:{supported:!0,webSearch:!0,contextFetching:!0}},caching:{supported:m.supportsContextCache(f),types:["persistent"]},thinking:I?.supported?.thinkingBudget??!1,multiTurn:!0}},h=a?.map(f=>{let I=f,x=I?.config;if(!x)return f;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 w={...I};Object.keys(A).length>0&&(w.modelConfig={...I.modelConfig??{},...A});let b=x.thinking?.thinkingTokenBudget;if(typeof b=="number"){let T=d.thinkingTokenBudgetLevels,v=[["minimal",T?.minimal??200],["low",T?.low??800],["medium",T?.medium??5e3],["high",T?.high??1e4],["highest",T?.highest??24500]],F="minimal",O=Number.POSITIVE_INFINITY;for(let[D,B]of v){let Q=Math.abs(b-B);Q<O&&(O=Q,F=D)}w.thinkingTokenBudget=F}return x.thinking?.includeThoughts!==void 0&&(w.showThoughts=!!x.thinking.includeThoughts),x.thinkingLevelMapping&&(w.thinkingLevelMapping=x.thinkingLevelMapping),x.thinkingTokenBudgetLevels&&(w.thinkingTokenBudgetLevels=x.thinkingTokenBudgetLevels),w});h?m.setModels(h):a&&m.setModels(a),super(m,{name:"GoogleGeminiAI",apiURL:c,headers:p,modelInfo:l,defaults:{model:d.model,embedModel:d.embedModel},options:s,supportFor:g,models:h??a})}};var fc=new Je,nn=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(fc.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 nr=(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))(nr||{});var oo=[{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 xc=()=>structuredClone({model:"llama-3.3-70b-versatile",...be()}),rn=class extends ke{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Groq API key not set");let s={...xc(),...t},a={...n,streamingUsage:!1};o=[...oo,...o??[]];let l={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0};super({apiKey:e,config:s,options:a,modelInfo:o,apiURL:"https://api.groq.com/openai/v1",models:r,supportFor:l}),super.setName("Groq"),this.setOptions(a)}setOptions=e=>{let t=this.newRateLimiter(e);super.setOptions({...e,rateLimiter:t})};newRateLimiter=e=>{if(e?.rateLimiter)return e.rateLimiter;let t=e?.tokensPerMinute??4800,n=new nn(t,t/60,{debug:e?.debug});return async(o,s)=>{let a=s.modelUsage?.tokens?.totalTokens||0;return await n.acquire(a),await o()}}};var so=[];var io=(e=>(e.MetaLlama270BChatHF="meta-llama/Llama-2-70b-chat-hf",e))(io||{});var Bs=()=>structuredClone({model:"meta-llama/Llama-2-70b-chat-hf",...be()}),Ua=()=>structuredClone({model:"meta-llama/Llama-2-70b-chat-hf",...Ne()}),Us=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(),s={name:"/models"},a={model:t,inputs:o,parameters:{max_new_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,repetition_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},top_k:e.modelConfig?.topK??this.config.topK,return_full_text:this.config.returnFullText,num_return_sequences:this.config.n,do_sample:this.config.doSample,max_time:this.config.maxTime},options:{use_cache:this.config.useCache,wait_for_model:this.config.waitForModel}};return[s,a]};createChatResp=e=>({results:[{index:0,content:e.generated_text}]})},sn=class extends Ee{constructor({apiKey:e,config:t,options:n,models:r}){if(!e||e==="")throw new Error("HuggingFace API key not set");let o={...Bs(),...t},s=new Us(o);super(s,{name:"HuggingFace",apiURL:"https://api-inference.huggingface.co",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:so,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 or=(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))(or||{}),js=(e=>(e.MistralEmbed="mistral-embed",e))(js||{});var ao=[{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 lo=()=>structuredClone({model:"mistral-small-latest",...be(),topP:1}),Ba=()=>structuredClone({...lo(),model:"mistral-large-latest"}),an=class extends ke{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Mistral API key not set");let s={...lo(),...t};o=[...ao,...o??[]];let a={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},l=u=>{let{max_completion_tokens:c,messages:p,...d}=u;return{...d,messages:this.updateMessages(p),max_tokens:c}};super({apiKey:e,config:s,options:n,apiURL:"https://api.mistral.ai/v1",modelInfo:o,models:r,supportFor:a,chatReqUpdater:l}),super.setName("Mistral")}updateMessages(e){let t=[];if(!Array.isArray(e))return e;for(let n of e)if(n.role==="user"&&Array.isArray(n.content)){let r=n.content.map(o=>typeof o=="object"&&o!==null&&"image_url"in o?{type:"image_url",image_url:{url:o.image_url?.url}}:o);t.push({...n,content:r})}else t.push(n);return t}};var uo=class{constructor(e={}){this.config=e;this.config.id=this.config.id??ze()}metrics={latency:{chat:{mean:0,p95:0,p99:0,samples:[]},embed:{mean:0,p95:0,p99:0,samples:[]}},errors:{chat:{count:0,rate:0,total:0},embed:{count:0,rate:0,total:0}}};getLastUsedChatModel(){return this.config.modelInfo?.name??"mock-model"}getLastUsedEmbedModel(){return this.config.embedModelInfo?.name??"mock-embed-model"}getLastUsedModelConfig(){return this.config.modelInfo?{maxTokens:this.config.modelInfo.maxTokens,temperature:.7,stream:this.config.features?.streaming??!1}:void 0}getName(){return this.config.name??"mock-ai-service"}getId(){return this.config.id??"mock-ai-service-id"}getFeatures(e){let t=this.config.features?.media;return{functions:this.config.features?.functions??!1,streaming:this.config.features?.streaming??!1,structuredOutputs:this.config.features?.structuredOutputs??!1,media:{images:{supported:t?.images?.supported??!1,formats:t?.images?.formats??[]},audio:{supported:t?.audio?.supported??!1,formats:t?.audio?.formats??[]},files:{supported:t?.files?.supported??!1,formats:t?.files?.formats??[],uploadMethod:t?.files?.uploadMethod??"none"},urls:{supported:t?.urls?.supported??!1,webSearch:t?.urls?.webSearch??!1,contextFetching:t?.urls?.contextFetching??!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}getModelList(){return this.config.models}getMetrics(){return this.metrics}async chat(e,t){if(this.config.latencyMs&&await new Promise(n=>setTimeout(n,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock chat error");return this.updateMetrics("chat"),typeof this.config.chatResponse=="function"?await this.config.chatResponse(e):this.config.chatResponse??{results:[{index:0,content:"Mock response",finishReason:"stop"}],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:10,completionTokens:5,totalTokens:15}}}}async embed(e,t){if(this.config.latencyMs&&await new Promise(n=>setTimeout(n,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock embed error");return this.updateMetrics("embed"),typeof this.config.embedResponse=="function"?this.config.embedResponse(e):this.config.embedResponse??{embeddings:[[.1,.2,.3]],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:5,completionTokens:0,totalTokens:5}}}}setOptions(e){this.config.options=e}getOptions(){return this.config.options??{}}getLogger(){return this.config.options?.logger??(e=>{console.log(e)})}updateMetrics(e){let t=this.config.latencyMs??0;this.metrics.latency[e].samples.push(t);let n=this.metrics.latency[e].samples;if(this.metrics.latency[e].mean=n.reduce((r,o)=>r+o,0)/n.length,n.length>0){let r=[...n].sort((a,l)=>a-l),o=Math.max(0,Math.floor(r.length*.95)-1);this.metrics.latency[e].p95=r[o]??t;let s=Math.max(0,Math.floor(r.length*.99)-1);this.metrics.latency[e].p99=r[s]??t}if(this.config.shouldError){this.metrics.errors[e].count++,this.metrics.errors[e].total++;let r=this.metrics.latency[e].samples.length;this.metrics.errors[e].rate=r>0?this.metrics.errors[e].count/r:0}}};var co=class i{options;lastUsedService;services=new Map;constructor(e){if(e.length===0)throw new Error("No AI services provided.");for(let[t,n]of e.entries())if("key"in n){if(this.services.has(n.key))throw new Error(`Duplicate model key: ${n.key}`);let{service:o,description:s,isInternal:a}=n;this.services.set(n.key,{service:o,description:s,isInternal:a})}else{let o=n.getModelList();if(!o)throw new Error(`Service ${t} \`${n.getName()}\` has no model list.`);for(let s of o){if(this.services.has(s.key)){let a=this.services.get(s.key)?.service;throw new Error(`Service ${t} \`${n.getName()}\` has duplicate model key: ${s.key} as service ${a?.getName()}`)}if("model"in s&&typeof s.model)this.services.set(s.key,{description:s.description,service:n,model:s.model});else if("embedModel"in s&&s.embedModel)this.services.set(s.key,{description:s.description,service:n,embedModel:s.embedModel});else throw new Error(`Key ${s.key} in model list for service ${t} \`${n.getName()}\` is missing a model or embedModel property.`)}}}static create(e){return new i(e)}getLastUsedChatModel(){return this.lastUsedService?.getLastUsedChatModel()}getLastUsedEmbedModel(){return this.lastUsedService?.getLastUsedEmbedModel()}getLastUsedModelConfig(){return this.lastUsedService?.getLastUsedModelConfig()}async chat(e,t){let n=e.model;if(!n)throw new Error("Model key must be specified for multi-service");let r=this.services.get(n);if(!r)throw new Error(`No service found for model key: ${n}`);if(this.lastUsedService=r.service,!r.model){let{model:o,...s}=e;return await r.service.chat(s,t)}return await r.service.chat({model:n,...e},t)}async embed(e,t){let n=e.embedModel;if(!n)throw new Error("Embed model key must be specified for multi-service");let r=this.services.get(n);if(!r)throw new Error(`No service found for embed model key: ${n}`);if(this.lastUsedService=r.service,!r.model){let{embedModel:o,...s}=e;return await r.service.embed(s,t)}return await r.service.embed({embedModel:n,...e},t)}getId(){return`MultiServiceRouter:${Array.from(this.services.values()).map(e=>e.service.getId()).join(",")}`}getName(){return"MultiServiceRouter"}getModelList(){return Array.from(this.services).filter(([,e])=>!e.isInternal).map(([e,t])=>{if(t.model)return{key:e,description:t.description,model:t.model};if(t.embedModel)return{key:e,description:t.description,embedModel:t.embedModel};throw new Error(`Service ${e} has no model or embedModel`)})}getFeatures(e){if(e){let t=this.services.get(e);if(t)return t.service.getFeatures(e)}return{functions:!1,streaming:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}getMetrics(){let e=this.lastUsedService;if(!e){let t=this.services.values().next().value;t&&(e="service"in t?t.service:t)}if(!e)throw new Error("No service available to get metrics.");return e.getMetrics()}setOptions(e){for(let t of this.services.values())t.service.setOptions(e);this.options=e}getOptions(){return this.options??{}}getLogger(){let e=this.lastUsedService;if(!e){let t=this.services.values().next().value;t&&(e=t.service)}if(!e)throw new Error("No service available to get logger.");return e.getLogger()}setServiceEntry(e,t){this.services.set(e,t)}};var qs=()=>structuredClone({...be(),model:"nous-hermes2",embedModel:"all-minilm"}),ja=()=>structuredClone({...Le(),model:"nous-hermes2",embedModel:"all-minilm"}),ln=class extends ke{constructor({apiKey:e="not-set",url:t="http://localhost:11434/v1",config:n,options:r,models:o}){let s={...qs(),...n};super({apiKey:e,options:r,config:s,apiURL:t,models:o,modelInfo:[],supportFor:{functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}),super.setName("Ollama")}};var fc=i=>["o1","o1-mini","o1-pro","o3","o3-mini","o3-pro","o4-mini"].includes(i),un=class{constructor(e,t,n){this.config=e;this.streamingUsage=t;this.responsesReqUpdater=n}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,stopSequences:e.stopSequences,topP:e.topP,stream:e.stream}}mapInternalContentToResponsesInput(e,t){let n=[];for(let r of e){if(r.type==="text"){t==="assistant"?n.push({type:"output_text",text:r.text}):n.push({type:"input_text",text:r.text});continue}if(t==="assistant")continue;if(r.type==="image"){let s=`data:${r.mimeType};base64,${r.image}`;n.push({type:"input_image",image_url:{url:s,details:r.details??"auto"}});continue}if(r.type==="audio"){n.push({type:"input_audio",input_audio:{data:r.data,format:r.format==="wav"?"wav":void 0}});continue}let o=r;throw new Error(`Unsupported content part: ${JSON.stringify(o)}`)}return n}createResponsesReqInternalInput(e,t=!1){let n=[];for(let r of e){if(t&&r.role==="system")continue;let o;if(r.role==="system"||r.role==="user"||r.role==="assistant"&&r.content)if(typeof r.content=="string")r.role==="system"?o=r.content:r.role==="assistant"?o=[{type:"output_text",text:r.content}]:o=[{type:"input_text",text:r.content}];else if(Array.isArray(r.content))o=this.mapInternalContentToResponsesInput(r.content,r.role==="assistant"?"assistant":"user");else{if(!(r.role==="assistant"&&!r.content&&r.functionCalls))throw new Error(`Invalid content type for role ${r.role}`);o=""}else r.role,o="";switch(r.role){case"system":n.push({type:"message",role:"system",content:o});break;case"user":n.push({type:"message",role:"user",content:o,name:r.name});break;case"assistant":if(r.content||r.functionCalls){let s={type:"message",role:"assistant",content:""};if(r.content&&(s.content=o),r.name&&(s.name=r.name),r.content&&n.push(s),r.functionCalls)for(let a of r.functionCalls)n.push({type:"function_call",call_id:a.id,name:a.function.name,arguments:typeof a.function.params=="object"?JSON.stringify(a.function.params):a.function.params||""})}break;case"function":n.push({type:"function_call_output",call_id:r.functionId,output:r.result});break;default:{let s=r.role;throw new Error(`Invalid role in chat prompt: ${s}`)}}}return n}createChatReq(e,t){let n=e.model,r={name:"/responses"},o=null,s=!1;if(e.chatPrompt){for(let b of e.chatPrompt)if(b.role==="system"&&typeof b.content=="string"){o=b.content,s=!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=fc(n),p=this.config.reasoningSummary;t?.showThoughts?p||(p="auto"):p=void 0;let d=this.config.reasoningEffort;if(t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"none":d=void 0;break;case"minimal":d="minimal";break;case"low":d="medium";break;case"medium":case"high":case"highest":d="high";break}let m={model:n,input:"",instructions:a,tools:l?.length?l:void 0,tool_choice:e.functionCall==="none"||e.functionCall==="auto"||e.functionCall==="required"?e.functionCall:typeof e.functionCall=="object"&&e.functionCall.function?{type:"function",name:e.functionCall.function.name}:void 0,...c?{max_output_tokens:e.modelConfig?.maxTokens??this.config.maxTokens??void 0}:{...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty??void 0,frequency_penalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty??void 0,max_output_tokens:e.modelConfig?.maxTokens??this.config.maxTokens??void 0},stream:e.modelConfig?.stream??this.config.stream??!1,background:void 0,include:u.length>0?u:void 0,metadata:void 0,parallel_tool_calls:this.config.parallelToolCalls,previous_response_id:void 0,...d?{reasoning:{effort:d,summary:p}}:{},service_tier:this.config.serviceTier,store:this.config.store,text:void 0,truncation:void 0,user:this.config.user,seed:this.config.seed};this.config.user&&(m.user=this.config.user),this.config.parallelToolCalls!==void 0&&(m.parallel_tool_calls=this.config.parallelToolCalls),e.responseFormat?m.text={format:e.responseFormat.type==="json_schema"?{type:"json_schema",json_schema:e.responseFormat.schema}:{type:e.responseFormat.type}}:this.config.responseFormat&&(m.text={format:{type:this.config.responseFormat}}),this.config.seed&&(m.seed=this.config.seed);let g=e.chatPrompt?this.createResponsesReqInternalInput(e.chatPrompt,s):[];if(g.length>0)m.input=g;else if(e.chatPrompt&&e.chatPrompt.length===1&&e.chatPrompt[0]?.role==="user"&&e.chatPrompt[0]?.content&&typeof e.chatPrompt[0].content=="string"&&!a)m.input=e.chatPrompt[0].content;else if(g.length===0&&!a)throw new Error("Responses API request must have input or instructions.");let h=m.reasoning??{};if(this.config.reasoningEffort&&(h={...h,effort:this.config.reasoningEffort}),t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"none":h={};break;case"minimal":h={...h,effort:"minimal"};break;case"low":h={...h,effort:"medium"};break;case"medium":case"high":case"highest":h={...h,effort:"high"};break}Object.keys(h).length>0&&h.effort?m.reasoning=h:m.reasoning=void 0;let f=m;return this.responsesReqUpdater&&(f=this.responsesReqUpdater(f)),[r,f]}createChatResp(e){let{id:t,output:n,usage:r}=e;r&&(this.tokensUsed={promptTokens:r.prompt_tokens,completionTokens:r.completion_tokens??r.output_tokens??0,totalTokens:r.total_tokens});let o={};for(let s of n??[])switch(s.type){case"message":o.id=s.id,o.content=zs(s.content,t),o.finishReason=s.status==="completed"?"stop":"content_filter",o.citations=po(s.content);break;case"reasoning":o.id=s.id,s.encrypted_content?o.thought=s.encrypted_content:o.thought=s.summary.map(a=>typeof a=="object"?JSON.stringify(a):a).join(`
175
+ `),o=`${n} ${r}`.trim(),s={name:"/models"},a={model:t,inputs:o,parameters:{max_new_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,repetition_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},top_k:e.modelConfig?.topK??this.config.topK,return_full_text:this.config.returnFullText,num_return_sequences:this.config.n,do_sample:this.config.doSample,max_time:this.config.maxTime},options:{use_cache:this.config.useCache,wait_for_model:this.config.waitForModel}};return[s,a]};createChatResp=e=>({results:[{index:0,content:e.generated_text}]})},on=class extends Me{constructor({apiKey:e,config:t,options:n,models:r}){if(!e||e==="")throw new Error("HuggingFace API key not set");let o={...Bs(),...t},s=new Us(o);super(s,{name:"HuggingFace",apiURL:"https://api-inference.huggingface.co",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:so,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 rr=(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))(rr||{}),js=(e=>(e.MistralEmbed="mistral-embed",e))(js||{});var ao=[{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 lo=()=>structuredClone({model:"mistral-small-latest",...be(),topP:1}),Ba=()=>structuredClone({...lo(),model:"mistral-large-latest"}),sn=class extends ke{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Mistral API key not set");let s={...lo(),...t};o=[...ao,...o??[]];let a={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},l=u=>{let{max_completion_tokens:c,messages:p,...d}=u;return{...d,messages:this.updateMessages(p),max_tokens:c}};super({apiKey:e,config:s,options:n,apiURL:"https://api.mistral.ai/v1",modelInfo:o,models:r,supportFor:a,chatReqUpdater:l}),super.setName("Mistral")}updateMessages(e){let t=[];if(!Array.isArray(e))return e;for(let n of e)if(n.role==="user"&&Array.isArray(n.content)){let r=n.content.map(o=>typeof o=="object"&&o!==null&&"image_url"in o?{type:"image_url",image_url:{url:o.image_url?.url}}:o);t.push({...n,content:r})}else t.push(n);return t}};var uo=class{constructor(e={}){this.config=e;this.config.id=this.config.id??Be()}metrics={latency:{chat:{mean:0,p95:0,p99:0,samples:[]},embed:{mean:0,p95:0,p99:0,samples:[]}},errors:{chat:{count:0,rate:0,total:0},embed:{count:0,rate:0,total:0}}};getLastUsedChatModel(){return this.config.modelInfo?.name??"mock-model"}getLastUsedEmbedModel(){return this.config.embedModelInfo?.name??"mock-embed-model"}getLastUsedModelConfig(){return this.config.modelInfo?{maxTokens:this.config.modelInfo.maxTokens,temperature:.7,stream:this.config.features?.streaming??!1}:void 0}getName(){return this.config.name??"mock-ai-service"}getId(){return this.config.id??"mock-ai-service-id"}getFeatures(e){let t=this.config.features?.media;return{functions:this.config.features?.functions??!1,streaming:this.config.features?.streaming??!1,structuredOutputs:this.config.features?.structuredOutputs??!1,media:{images:{supported:t?.images?.supported??!1,formats:t?.images?.formats??[]},audio:{supported:t?.audio?.supported??!1,formats:t?.audio?.formats??[]},files:{supported:t?.files?.supported??!1,formats:t?.files?.formats??[],uploadMethod:t?.files?.uploadMethod??"none"},urls:{supported:t?.urls?.supported??!1,webSearch:t?.urls?.webSearch??!1,contextFetching:t?.urls?.contextFetching??!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}getModelList(){return this.config.models}getMetrics(){return this.metrics}async chat(e,t){if(this.config.latencyMs&&await new Promise(n=>setTimeout(n,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock chat error");return this.updateMetrics("chat"),typeof this.config.chatResponse=="function"?await this.config.chatResponse(e):this.config.chatResponse??{results:[{index:0,content:"Mock response",finishReason:"stop"}],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:10,completionTokens:5,totalTokens:15}}}}async embed(e,t){if(this.config.latencyMs&&await new Promise(n=>setTimeout(n,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock embed error");return this.updateMetrics("embed"),typeof this.config.embedResponse=="function"?this.config.embedResponse(e):this.config.embedResponse??{embeddings:[[.1,.2,.3]],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:5,completionTokens:0,totalTokens:5}}}}setOptions(e){this.config.options=e}getOptions(){return this.config.options??{}}getLogger(){return this.config.options?.logger??(e=>{console.log(e)})}updateMetrics(e){let t=this.config.latencyMs??0;this.metrics.latency[e].samples.push(t);let n=this.metrics.latency[e].samples;if(this.metrics.latency[e].mean=n.reduce((r,o)=>r+o,0)/n.length,n.length>0){let r=[...n].sort((a,l)=>a-l),o=Math.max(0,Math.floor(r.length*.95)-1);this.metrics.latency[e].p95=r[o]??t;let s=Math.max(0,Math.floor(r.length*.99)-1);this.metrics.latency[e].p99=r[s]??t}if(this.config.shouldError){this.metrics.errors[e].count++,this.metrics.errors[e].total++;let r=this.metrics.latency[e].samples.length;this.metrics.errors[e].rate=r>0?this.metrics.errors[e].count/r:0}}};var co=class i{options;lastUsedService;services=new Map;constructor(e){if(e.length===0)throw new Error("No AI services provided.");for(let[t,n]of e.entries())if("key"in n){if(this.services.has(n.key))throw new Error(`Duplicate model key: ${n.key}`);let{service:o,description:s,isInternal:a}=n;this.services.set(n.key,{service:o,description:s,isInternal:a})}else{let o=n.getModelList();if(!o)throw new Error(`Service ${t} \`${n.getName()}\` has no model list.`);for(let s of o){if(this.services.has(s.key)){let a=this.services.get(s.key)?.service;throw new Error(`Service ${t} \`${n.getName()}\` has duplicate model key: ${s.key} as service ${a?.getName()}`)}if("model"in s&&typeof s.model)this.services.set(s.key,{description:s.description,service:n,model:s.model});else if("embedModel"in s&&s.embedModel)this.services.set(s.key,{description:s.description,service:n,embedModel:s.embedModel});else throw new Error(`Key ${s.key} in model list for service ${t} \`${n.getName()}\` is missing a model or embedModel property.`)}}}static create(e){return new i(e)}getLastUsedChatModel(){return this.lastUsedService?.getLastUsedChatModel()}getLastUsedEmbedModel(){return this.lastUsedService?.getLastUsedEmbedModel()}getLastUsedModelConfig(){return this.lastUsedService?.getLastUsedModelConfig()}async chat(e,t){let n=e.model;if(!n)throw new Error("Model key must be specified for multi-service");let r=this.services.get(n);if(!r)throw new Error(`No service found for model key: ${n}`);if(this.lastUsedService=r.service,!r.model){let{model:o,...s}=e;return await r.service.chat(s,t)}return await r.service.chat({model:n,...e},t)}async embed(e,t){let n=e.embedModel;if(!n)throw new Error("Embed model key must be specified for multi-service");let r=this.services.get(n);if(!r)throw new Error(`No service found for embed model key: ${n}`);if(this.lastUsedService=r.service,!r.model){let{embedModel:o,...s}=e;return await r.service.embed(s,t)}return await r.service.embed({embedModel:n,...e},t)}getId(){return`MultiServiceRouter:${Array.from(this.services.values()).map(e=>e.service.getId()).join(",")}`}getName(){return"MultiServiceRouter"}getModelList(){return Array.from(this.services).filter(([,e])=>!e.isInternal).map(([e,t])=>{if(t.model)return{key:e,description:t.description,model:t.model};if(t.embedModel)return{key:e,description:t.description,embedModel:t.embedModel};throw new Error(`Service ${e} has no model or embedModel`)})}getFeatures(e){if(e){let t=this.services.get(e);if(t)return t.service.getFeatures(e)}return{functions:!1,streaming:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}getMetrics(){let e=this.lastUsedService;if(!e){let t=this.services.values().next().value;t&&(e="service"in t?t.service:t)}if(!e)throw new Error("No service available to get metrics.");return e.getMetrics()}setOptions(e){for(let t of this.services.values())t.service.setOptions(e);this.options=e}getOptions(){return this.options??{}}getLogger(){let e=this.lastUsedService;if(!e){let t=this.services.values().next().value;t&&(e=t.service)}if(!e)throw new Error("No service available to get logger.");return e.getLogger()}setServiceEntry(e,t){this.services.set(e,t)}};var qs=()=>structuredClone({...be(),model:"nous-hermes2",embedModel:"all-minilm"}),ja=()=>structuredClone({...Ne(),model:"nous-hermes2",embedModel:"all-minilm"}),an=class extends ke{constructor({apiKey:e="not-set",url:t="http://localhost:11434/v1",config:n,options:r,models:o}){let s={...qs(),...n};super({apiKey:e,options:r,config:s,apiURL:t,models:o,modelInfo:[],supportFor:{functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}),super.setName("Ollama")}};var Ac=i=>["o1","o1-mini","o1-pro","o3","o3-mini","o3-pro","o4-mini"].includes(i),ln=class{constructor(e,t,n){this.config=e;this.streamingUsage=t;this.responsesReqUpdater=n}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,stopSequences:e.stopSequences,topP:e.topP,stream:e.stream}}mapInternalContentToResponsesInput(e,t){let n=[];for(let r of e){if(r.type==="text"){t==="assistant"?n.push({type:"output_text",text:r.text}):n.push({type:"input_text",text:r.text});continue}if(t==="assistant")continue;if(r.type==="image"){let s=`data:${r.mimeType};base64,${r.image}`;n.push({type:"input_image",image_url:{url:s,details:r.details??"auto"}});continue}if(r.type==="audio"){n.push({type:"input_audio",input_audio:{data:r.data,format:r.format==="wav"?"wav":void 0}});continue}let o=r;throw new Error(`Unsupported content part: ${JSON.stringify(o)}`)}return n}createResponsesReqInternalInput(e,t=!1){let n=[];for(let r of e){if(t&&r.role==="system")continue;let o;if(r.role==="system"||r.role==="user"||r.role==="assistant"&&r.content)if(typeof r.content=="string")r.role==="system"?o=r.content:r.role==="assistant"?o=[{type:"output_text",text:r.content}]:o=[{type:"input_text",text:r.content}];else if(Array.isArray(r.content))o=this.mapInternalContentToResponsesInput(r.content,r.role==="assistant"?"assistant":"user");else{if(!(r.role==="assistant"&&!r.content&&r.functionCalls))throw new Error(`Invalid content type for role ${r.role}`);o=""}else r.role,o="";switch(r.role){case"system":n.push({type:"message",role:"system",content:o});break;case"user":n.push({type:"message",role:"user",content:o,name:r.name});break;case"assistant":if(r.content||r.functionCalls){let s={type:"message",role:"assistant",content:""};if(r.content&&(s.content=o),r.name&&(s.name=r.name),r.content&&n.push(s),r.functionCalls)for(let a of r.functionCalls)n.push({type:"function_call",call_id:a.id,name:a.function.name,arguments:typeof a.function.params=="object"?JSON.stringify(a.function.params):a.function.params||""})}break;case"function":n.push({type:"function_call_output",call_id:r.functionId,output:r.result});break;default:{let s=r.role;throw new Error(`Invalid role in chat prompt: ${s}`)}}}return n}createChatReq(e,t){let n=e.model,r={name:"/responses"},o=null,s=!1;if(e.chatPrompt){for(let I of e.chatPrompt)if(I.role==="system"&&typeof I.content=="string"){o=I.content,s=!0;break}}let a=o??this.config.systemPrompt??null,l=e.functions?.map(I=>({type:"function",name:I.name,description:I.description,parameters:I.parameters??{}})),u=[],c=Ac(n),p=this.config.reasoningSummary;t?.showThoughts?p||(p="auto"):p=void 0;let d=this.config.reasoningEffort;if(t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"none":d=void 0;break;case"minimal":d="minimal";break;case"low":d="medium";break;case"medium":case"high":case"highest":d="high";break}let m={model:n,input:"",instructions:a,tools:l?.length?l:void 0,tool_choice:e.functionCall==="none"||e.functionCall==="auto"||e.functionCall==="required"?e.functionCall:typeof e.functionCall=="object"&&e.functionCall.function?{type:"function",name:e.functionCall.function.name}:void 0,...c?{max_output_tokens:e.modelConfig?.maxTokens??this.config.maxTokens??void 0}:{...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty??void 0,frequency_penalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty??void 0,max_output_tokens:e.modelConfig?.maxTokens??this.config.maxTokens??void 0},stream:e.modelConfig?.stream??this.config.stream??!1,background:void 0,include:u.length>0?u:void 0,metadata:void 0,parallel_tool_calls:this.config.parallelToolCalls,previous_response_id:void 0,...d?{reasoning:{effort:d,summary:p}}:{},service_tier:this.config.serviceTier,store:this.config.store,text:void 0,truncation:void 0,user:this.config.user,seed:this.config.seed};this.config.user&&(m.user=this.config.user),this.config.parallelToolCalls!==void 0&&(m.parallel_tool_calls=this.config.parallelToolCalls),e.responseFormat?m.text={format:e.responseFormat.type==="json_schema"?{type:"json_schema",json_schema:e.responseFormat.schema}:{type:e.responseFormat.type}}:this.config.responseFormat&&(m.text={format:{type:this.config.responseFormat}}),this.config.seed&&(m.seed=this.config.seed);let g=e.chatPrompt?this.createResponsesReqInternalInput(e.chatPrompt,s):[];if(g.length>0)m.input=g;else if(e.chatPrompt&&e.chatPrompt.length===1&&e.chatPrompt[0]?.role==="user"&&e.chatPrompt[0]?.content&&typeof e.chatPrompt[0].content=="string"&&!a)m.input=e.chatPrompt[0].content;else if(g.length===0&&!a)throw new Error("Responses API request must have input or instructions.");let h=m.reasoning??{};if(this.config.reasoningEffort&&(h={...h,effort:this.config.reasoningEffort}),t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"none":h={};break;case"minimal":h={...h,effort:"minimal"};break;case"low":h={...h,effort:"medium"};break;case"medium":case"high":case"highest":h={...h,effort:"high"};break}Object.keys(h).length>0&&h.effort?m.reasoning=h:m.reasoning=void 0;let f=m;return this.responsesReqUpdater&&(f=this.responsesReqUpdater(f)),[r,f]}createChatResp(e){let{id:t,output:n,usage:r}=e;r&&(this.tokensUsed={promptTokens:r.prompt_tokens,completionTokens:r.completion_tokens??r.output_tokens??0,totalTokens:r.total_tokens});let o={};for(let s of n??[])switch(s.type){case"message":o.id=s.id,o.content=zs(s.content,t),o.finishReason=s.status==="completed"?"stop":"content_filter",o.citations=po(s.content);break;case"reasoning":o.id=s.id,s.encrypted_content?o.thought=s.encrypted_content:o.thought=s.summary.map(a=>typeof a=="object"?JSON.stringify(a):a).join(`
176
176
  `);break;case"file_search_call":o.id=s.id,o.functionCalls=[{id:s.id,type:"function",function:{name:"file_search",params:{queries:s.queries,results:s.results}}}],o.finishReason="function_call";break;case"web_search_call":o.id=s.id,o.functionCalls=[{id:s.id,type:"function",function:{name:"web_search",params:{queries:s.queries}}}],o.finishReason="function_call";break;case"computer_call":o.id=s.id,o.functionCalls=[{id:s.id,type:"function",function:{name:"computer_use",params:{action:s.action}}}],o.finishReason="function_call";break;case"code_interpreter_call":o.id=s.id,o.functionCalls=[{id:s.id,type:"function",function:{name:"code_interpreter",params:{code:s.code,results:s.results}}}],o.finishReason="function_call";break;case"image_generation_call":o.id=s.id,o.functionCalls=[{id:s.id,type:"function",function:{name:"image_generation",params:{result:s.result}}}],o.finishReason="function_call";break;case"local_shell_call":o.id=s.id,o.functionCalls=[{id:s.id,type:"function",function:{name:"local_shell",params:{action:s.action}}}],o.finishReason="function_call";break;case"mcp_call":o.id=s.id,o.functionCalls=[{id:s.id,type:"function",function:{name:"mcp",params:{name:s.name,args:s.args,serverLabel:s.server_label,output:s.output,error:s.error}}}],o.finishReason="function_call";break;case"function_call":o.id=s.id,o.functionCalls=[{id:s.id,type:"function",function:{name:s.name,params:s.arguments}}],o.finishReason="function_call";break}return{results:[{...o,index:0}],remoteId:t}}createChatStreamResp(e){let t=e,n={index:0,id:"",content:"",finishReason:"stop"},r;switch(t.type){case"response.created":case"response.in_progress":case"response.queued":r=t.response.id,n.id=`${t.response.id}_res_0`;break;case"response.output_item.added":switch(t.item.type){case"message":n.id=t.item.id,n.content=zs(t.item.content,t.item.id),n.citations=po(t.item.content);break;case"function_call":n.id=t.item.id,n.functionCalls=[{id:t.item.id,type:"function",function:{name:t.item.name,params:t.item.arguments}}];break;case"file_search_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"file_search",params:{queries:o.queries||[],results:o.results?.map(s=>({fileId:s.file_id,filename:s.filename,score:s.score,text:s.text,attributes:s.attributes}))}}}]}break;case"web_search_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"web_search",params:{queries:o.queries||[]}}}]}break;case"computer_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"computer_use",params:{action:o.action||{}}}}]}break;case"code_interpreter_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"code_interpreter",params:{code:o.code||"",results:o.results}}}]}break;case"image_generation_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"image_generation",params:{result:o.result}}}]}break;case"local_shell_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"local_shell",params:{action:o.action||{}}}}]}break;case"mcp_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"mcp",params:{name:o.name||"",args:o.args||"",serverLabel:o.server_label||"",output:o.output,error:o.error}}}]}break}break;case"response.content_part.added":n.id=t.item_id,n.content=zs([t.part],t.item_id),n.citations=po([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=po(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]}},zs=(i,e)=>{let t=i.filter(n=>n.type==="refusal");if(t.length>0){let n=t.map(r=>r.refusal).join(`
177
- `);throw new Se(n,void 0,e)}return i.filter(n=>n.type==="output_text").map(n=>n.text).join(`
178
- `)};function po(i){let e=[];for(let t of i??[])if(t?.type==="output_text"&&Array.isArray(t.annotations))for(let n of t.annotations)n&&n.type==="url_citation"&&typeof n.url=="string"&&e.push({url:n.url,title:n.title,description:n.description});return e.length?e:void 0}var ir=()=>({model:"gpt-4o",embedModel:"text-embedding-ada-002",temperature:.7,topP:1,stream:!0}),qa=()=>({...ir(),model:"gpt-4o",temperature:.5}),za=()=>({...ir(),model:"gpt-4o",temperature:.9}),sr=class extends Ee{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o=[],models:s,responsesReqUpdater:a,supportFor:l={functions:!0,streaming:!0,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}){if(!e||e==="")throw new Error("OpenAI API key not set");let u=new un(t,n?.streamingUsage??!0,a),c=s?.map(p=>{let d=p,m=d?.config;if(!m)return p;let g={};m.maxTokens!==void 0&&(g.maxTokens=m.maxTokens),m.temperature!==void 0&&(g.temperature=m.temperature),m.topP!==void 0&&(g.topP=m.topP),m.presencePenalty!==void 0&&(g.presencePenalty=m.presencePenalty),m.frequencyPenalty!==void 0&&(g.frequencyPenalty=m.frequencyPenalty);let h=m.stopSequences??m.stop;h!==void 0&&(g.stopSequences=h),m.n!==void 0&&(g.n=m.n),m.stream!==void 0&&(g.stream=m.stream);let f={...d};Object.keys(g).length>0&&(f.modelConfig={...d.modelConfig??{},...g});let b=m?.thinking?.thinkingTokenBudget;if(typeof b=="number"){let x=[["minimal",200],["low",800],["medium",5e3],["high",1e4],["highest",24500]],A="minimal",w=Number.POSITIVE_INFINITY;for(let[I,T]of x){let v=Math.abs(b-T);v<w&&(w=v,A=I)}f.thinkingTokenBudget=A}return m?.thinking?.includeThoughts!==void 0&&(f.showThoughts=!!m.thinking.includeThoughts),f});super(u,{name:"OpenAI",apiURL:r||"https://api.openai.com/v1",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:o,defaults:{model:t.model??"gpt-4o",embedModel:t.embedModel??"text-embedding-ada-002"},options:n,supportFor:l,models:c??s})}},cn=class extends sr{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("OpenAI API key not set");o=[...zr,...o??[]];let s=a=>{let l=rt({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:s})}};var Hs=()=>structuredClone({model:"openrouter/auto",...be()}),pn=class extends ke{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o,referer:s,title:a}){if(!e||e==="")throw new Error("OpenRouter API key not set");let l={...Hs(),...t},u={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},c=o??[];super({apiKey:e,config:l,options:n,apiURL:"https://openrouter.ai/api/v1",modelInfo:c,models:r,supportFor:u}),super.setName("OpenRouter"),super.setHeaders(async()=>{let p={Authorization:`Bearer ${e}`};return s&&(p["HTTP-Referer"]=s),a&&(p["X-Title"]=a),p})}};async function mo(i,e,t={}){if(typeof i=="string")return[{type:"text",text:i}];if(!Array.isArray(i))return[{type:"text",text:String(i)}];let n=e.getFeatures(),r=[];for(let o of i)try{switch(o.type){case"text":r.push({type:"text",text:o.text});break;case"image":if(n.media.images.supported)o.altText?r.push({type:"text",text:`[Image: ${o.altText}]`}):r.push({type:"text",text:"[Image content]"});else if(o.altText)r.push({type:"text",text:o.altText});else if(t.imageToText)try{let s=await t.imageToText(o.image);r.push({type:"text",text:s})}catch(s){throw new dt(s,"image","vision analysis")}else switch(t.fallbackBehavior){case"error":throw new st("Images",e.getName(),!1);case"skip":continue;default:r.push({type:"text",text:"[Image content not supported by this provider]"})}break;case"audio":if(n.media.audio.supported)o.transcription?r.push({type:"text",text:o.transcription}):r.push({type:"text",text:"[Audio content]"});else if(o.transcription)r.push({type:"text",text:o.transcription});else if(t.audioToText)try{let s=await t.audioToText(o.data,o.format);r.push({type:"text",text:s})}catch(s){throw new dt(s,"audio","transcription")}else switch(t.fallbackBehavior){case"error":throw new st("Audio",e.getName(),!1);case"skip":continue;case"degrade":r.push({type:"text",text:"[Audio content not supported by this provider]"})}break;case"file":if(n.media.files.supported)o.extractedText?r.push({type:"text",text:o.extractedText}):r.push({type:"text",text:`[File: ${o.filename}]`});else if(o.extractedText)r.push({type:"text",text:o.extractedText});else if(t.fileToText)try{let s=await t.fileToText(o.data,o.mimeType);r.push({type:"text",text:s})}catch(s){throw new dt(s,"file","text extraction")}else switch(t.fallbackBehavior){case"error":throw new st("Files",e.getName(),!1);case"skip":continue;default:r.push({type:"text",text:`[File: ${o.filename} - content not accessible by this provider]`})}break;case"url":if(n.media.urls.supported)o.cachedContent?r.push({type:"text",text:o.cachedContent}):r.push({type:"text",text:`[Link: ${o.url}${o.title?` - ${o.title}`:""}]`});else if(o.cachedContent)r.push({type:"text",text:o.cachedContent});else if(t.urlToText)try{let s=await t.urlToText(o.url);r.push({type:"text",text:s})}catch(s){throw new dt(s,"url","content fetching")}else switch(t.fallbackBehavior){case"error":throw new st("URLs",e.getName(),!1);case"skip":continue;case"degrade":r.push({type:"text",text:`[Link: ${o.url}${o.title?` - ${o.title}`:""}]`})}break;default:typeof o=="object"&&o.text?r.push({type:"text",text:o.text}):r.push({type:"text",text:String(o)})}}catch(s){throw s instanceof st||s instanceof dt?s:new dt(s,o.type||"unknown","content processing")}return r}function Ha(i){let e=!1,t=!1,n=!1,r=!1;for(let o of i)if(o.role==="user"&&Array.isArray(o.content))for(let s of o.content)switch(s.type){case"image":e=!0;break;case"audio":t=!0;break;case"file":n=!0;break;case"url":r=!0;break}return{hasImages:e,hasAudio:t,hasFiles:n,hasUrls:r}}var ar=(n=>(n.RekaCore="reka-core",n.RekaFlash="reka-flash",n.RekaEdge="reka-edge",n))(ar||{});var go=[{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 lr=()=>structuredClone({model:"reka-core",...be()}),Wa=()=>structuredClone({...lr(),model:"reka-core"}),Va=()=>structuredClone({model:"reka-core",...Le()}),Ja=()=>({...lr(),model:"reka-flash"}),Ks=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=xc(e),o=e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,s=e.modelConfig?.stream??this.config.stream,a={model:t,messages:r,max_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},top_k:e.modelConfig?.n??this.config.n,...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},stop:e.modelConfig?.stopSequences??this.config.stop,presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,...o?{frequency_penalty:o}:{},...s?{stream:!0}:{}};return[n,a]};createChatResp=e=>{let{id:t,usage:n,responses:r}=e;return this.tokensUsed=n?{promptTokens:n.input_tokens,completionTokens:n.output_tokens,totalTokens:n.input_tokens+n.output_tokens}:void 0,{results:r.map((s,a)=>{let l=Ka(s.finish_reason),u;return typeof s.message.content=="string"?u=s.message.content:u=s.message.content.text,{index:a,id:`${t}`,content:u,finishReason:l}}),remoteId:t}};createChatStreamResp=e=>{let{id:t,usage:n,responses:r}=e;return this.tokensUsed=n?{promptTokens:n.input_tokens,completionTokens:n.output_tokens,totalTokens:n.input_tokens+n.output_tokens}:void 0,{results:r.map((s,a)=>{let l=Ka(s.finish_reason),u;return typeof s.chunk.content=="string"?u=s.chunk.content:u=s.chunk.content.text,{index:a,id:`${t}`,content:u,finishReason:l}})}}},Ka=i=>{switch(i){case"stop":return"stop";case"context":return"length";case"length":return"length"}};function xc(i){return i.chatPrompt.map(e=>{switch(e.role){case"system":return{role:"user",content:e.content};case"user":return Array.isArray(e.content)?{role:"user",content:e.content.map(t=>{switch(t.type){case"text":return{type:"text",text:t.text};case"image":throw new Error("Image type not supported");default:throw new Error("Invalid content type")}})}:{role:"user",content:e.content};case"assistant":if(Array.isArray(e.content))return{role:"assistant",content:e.content.map(t=>{switch(t.type){case"text":return{type:"text",text:t.text};case"image":throw new Error("Image type not supported");default:throw new Error("Invalid content type")}})};if(!e.content)throw new Error("Assistant content is empty");return{role:"user",content:e.content};default:throw new Error("Invalid role")}})}var dn=class extends Ee{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o=go,models:s}){if(!e||e==="")throw new Error("Reka API key not set");let a={...lr(),...t},l=new Ks(a);super(l,{name:"Reka",apiURL:r||"https://api.reka.ai/v1/chat",headers:async()=>({"X-Api-Key":e}),modelInfo:o,defaults:{model:a.model},options:n,supportFor:{functions:!0,streaming:!0,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},models:s})}};var ho=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 st&&t.fallbackProviders?.length)return await this.tryFallbackProviders(e,t.fallbackProviders,t);throw o}}async preprocessRequest(e,t,n){let r={...n,fallbackBehavior:n?.fallbackBehavior||"degrade",imageToText:n?.imageToText||this.processingServices.imageToText,audioToText:n?.audioToText||this.processingServices.audioToText,fileToText:n?.fileToText||this.processingServices.fileToText,urlToText:n?.urlToText||this.processingServices.urlToText},o=[];for(let s of e.chatPrompt)if(s.role==="user"&&Array.isArray(s.content)){let a=await mo(s.content,t,r);a.every(u=>u.type==="text")&&a.length===1?o.push({...s,content:a[0].text}):o.push({...s,content:a.map(u=>({type:"text",text:u.text}))})}else o.push(s);return{...e,chatPrompt:o}}async selectProviderWithDegradation(e,t){let n=Nt(e),r=[],o=[],s=[];try{let a=Qr(e,this.providers,{requireExactMatch:t.requireExactMatch??this.config.capability.requireExactMatch,allowDegradation:t.allowDegradation??this.config.capability.allowDegradation}),l=a.getFeatures();return n.hasImages&&!l.media.images.supported&&(o.push("Images will be converted to text descriptions"),r.push("Image-to-text conversion")),n.hasAudio&&!l.media.audio.supported&&(o.push("Audio will be transcribed to text"),r.push("Audio-to-text transcription")),n.hasFiles&&!l.media.files.supported&&(o.push("File content will be extracted to text"),r.push("File-to-text extraction")),n.hasUrls&&!l.media.urls.supported&&(o.push("URL content will be pre-fetched"),r.push("URL content fetching")),n.requiresStreaming&&!l.streaming&&s.push("Streaming not supported - will use non-streaming mode"),n.requiresCaching&&!l.caching.supported&&s.push("Content caching not supported"),{provider:a,processingApplied:r,degradations:o,warnings:s}}catch(a){throw new Error(`Provider selection failed: ${a instanceof Error?a.message:"Unknown error"}`)}}async tryFallbackProviders(e,t,n){for(let r of t)try{let o={provider:r,processingApplied:["Fallback provider selection"],degradations:["Using fallback provider due to primary provider failure"],warnings:[]},s=await this.preprocessRequest(e,r,{fallbackBehavior:"degrade"});return{response:await r.chat(s,n),routing:o}}catch{}throw new Error("All fallback providers failed")}async getRoutingRecommendation(e){return await this.selectProviderWithDegradation(e,{})}async validateRequest(e){let t=Nt(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 ur=(x=>(x.KimiK25="moonshotai/Kimi-K2.5",x.KimiK2Instruct0905="moonshotai/Kimi-K2-Instruct-0905",x.KimiK2Thinking="moonshotai/Kimi-K2-Thinking",x.DeepSeekV31="deepseek-ai/DeepSeek-V3.1",x.DeepSeekR1="deepseek-ai/DeepSeek-R1",x.GPTOSS120B="openai/gpt-oss-120b",x.GPTOSS20B="openai/gpt-oss-20b",x.Qwen35_397B="Qwen/Qwen3.5-397B-A17B",x.Qwen3CoderNext="Qwen/Qwen3-Coder-Next-FP8",x.Qwen3Coder480B="Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8",x.Qwen3_235BInstruct2507="Qwen/Qwen3-235B-A22B-Instruct-2507-tput",x.Qwen3_235BThinking2507="Qwen/Qwen3-235B-A22B-Thinking-2507",x.Qwen3Next80BInstruct="Qwen/Qwen3-Next-80B-A3B-Instruct",x.Qwen3Next80BThinking="Qwen/Qwen3-Next-80B-A3B-Thinking",x.GLM5="zai-org/GLM-5",x.GLM47="zai-org/GLM-4.7",x.Llama4Maverick="meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8",x.Llama33_70B="meta-llama/Llama-3.3-70B-Instruct-Turbo",x))(ur||{});var fo=[{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 Ws=()=>structuredClone({model:"meta-llama/Llama-3.3-70B-Instruct-Turbo",...be()}),mn=class extends ke{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Together API key not set");let s={...Ws(),...t};o=[...fo,...o??[]];let a={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0};super({apiKey:e,config:s,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 xo=[{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 Js=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...be()}),Qa=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...Le()}),Vs=class{constructor(e,t){this.config=e;this.engine=t}tokensUsed;engine;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,endSequences:e.endSequences,stream:e.stream,n:e.n}}createChatReq(e){let t=e.model,n=e.chatPrompt.map(a=>{if(a.role==="function")return{role:"function",name:a.functionId,content:typeof a.result=="string"?a.result:JSON.stringify(a.result)};let l="";typeof a.content=="string"?l=a.content:Array.isArray(a.content)&&(l=a.content.filter(c=>c.type==="text").map(c=>c.text).join(`
179
- `));let u={role:a.role,content:l};return a.role==="assistant"&&a.functionCalls?.length?{...u,tool_calls:a.functionCalls.map(c=>({id:c.id,type:"function",function:{name:c.function.name,arguments:typeof c.function.params=="string"?c.function.params:JSON.stringify(c.function.params||{})}}))}:u}),r=e.functions?.map(a=>({type:"function",function:{name:a.name,description:a.description,parameters:a.parameters||{type:"object",properties:{}}}})),o={name:"/chat/completions",localCall:async(a,l)=>{try{let u=await this.engine.chat.completions.create({...a,stream:l||!1});return l?new ReadableStream({async start(c){try{for await(let p of u)c.enqueue(p);c.close()}catch(p){c.error(p)}}}):u}catch(u){throw new Error(`WebLLM API error: ${u}`)}}},s={model:t,messages:n,...r?.length?{tools:r}:{},max_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,frequency_penalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,stop:e.modelConfig?.stopSequences??this.config.stopSequences,stream:e.modelConfig?.stream??this.config.stream,n:e.modelConfig?.n??this.config.n};return[o,s]}createEmbedReq=e=>{throw new Error("WebLLM does not support embeddings")};createChatResp=e=>(this.tokensUsed={promptTokens:e.usage?.prompt_tokens??0,completionTokens:e.usage?.completion_tokens??0,totalTokens:e.usage?.total_tokens??0},{results:e.choices.map((n,r)=>{let o="stop";switch(n.finish_reason){case"stop":o="stop";break;case"length":o="length";break;case"tool_calls":o="function_call";break;case"content_filter":o="content_filter";break;default:o="stop";break}let s=n.message.tool_calls?.map(a=>({id:a.id,type:"function",function:{name:a.function.name,params:a.function.arguments}}));return{index:r,id:e.id,content:n.message.content||"",functionCalls:s,finishReason:o}}),remoteId:e.id});createChatStreamResp=(e,t)=>{let n=t,r=e.choices[0];if(!r)throw new Error("No choice in WebLLM stream response");if(r.delta.content&&(n.content=(n.content||"")+r.delta.content),r.delta.tool_calls){n.toolCalls||(n.toolCalls=[]);for(let l of r.delta.tool_calls){let u=n.toolCalls[l.index];u?l.function?.arguments&&(u.function.arguments=(u.function?.arguments||"")+l.function.arguments):n.toolCalls[l.index]={id:l.id,type:l.type,function:{name:l.function?.name,arguments:l.function?.arguments||""}}}}e.usage&&(this.tokensUsed={promptTokens:e.usage.prompt_tokens,completionTokens:e.usage.completion_tokens,totalTokens:e.usage.total_tokens});let o;if(r.finish_reason)switch(r.finish_reason){case"stop":o="stop";break;case"length":o="length";break;case"tool_calls":o="function_call";break;case"content_filter":o="content_filter";break;default:o="stop";break}let s=n.toolCalls?.map(l=>({id:l.id||"",type:"function",function:{name:l.function?.name||"",params:l.function?.arguments||""}}));return{results:[{index:0,id:e.id,content:n.content||"",functionCalls:s,finishReason:o}],remoteId:e.id}};createEmbedResp(e){throw new Error("WebLLM does not support embeddings")}},gn=class extends Ee{constructor({engine:e,config:t,options:n,models:r}){if(!e)throw new Error("WebLLM engine instance is required");let o={...Js(),...t},s=new Vs(o,e);super(s,{name:"WebLLM",apiURL:void 0,headers:async()=>({}),modelInfo:xo,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 pr=(r=>(r.Grok3="grok-3",r.Grok3Mini="grok-3-mini",r.Grok3Fast="grok-3-fast",r.Grok3MiniFast="grok-3-mini-fast",r))(pr||{}),Qs=(e=>(e.GrokEmbedSmall="grok-embed-small",e))(Qs||{});var Ao=[{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 yo=()=>structuredClone({model:"grok-3-mini",...be()}),Ya=()=>structuredClone({...yo(),model:"grok-3"}),hn=class extends ke{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Grok API key not set");let s={...yo(),...t};o=[...Ao,...o??[]];let a=u=>{let c=rt({model:u,modelInfo:o,models:r});return{functions:!0,streaming:!0,hasThinkingBudget:c?.supported?.thinkingBudget??!1,hasShowThoughts:c?.supported?.showThoughts??!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}},l=u=>{if(n?.searchParameters){let c=n.searchParameters;return{...u,search_parameters:{mode:c.mode,return_citations:c.returnCitations,from_date:c.fromDate,to_date:c.toDate,max_search_results:c.maxSearchResults,sources:c.sources?.map(p=>({type:p.type,country:p.country,excluded_websites:p.excludedWebsites,allowed_websites:p.allowedWebsites,safe_search:p.safeSearch,x_handles:p.xHandles,links:p.links}))}}}return u};super({apiKey:e,config:s,options:n,apiURL:"https://api.x.ai/v1",modelInfo:o,models:r,supportFor:a,chatReqUpdater:l}),super.setName("Grok")}};function Xa(i){return dr.create(i)}var dr=class i{ai;static create(e){return new i(e)}constructor(e){switch(e.name){case"openai":this.ai=new Xt(e);break;case"openai-responses":this.ai=new cn(e);break;case"azure-openai":this.ai=new Zt(e);break;case"grok":this.ai=new hn(e);break;case"huggingface":this.ai=new sn(e);break;case"groq":this.ai=new on(e);break;case"together":this.ai=new mn(e);break;case"openrouter":this.ai=new pn(e);break;case"cohere":this.ai=new en(e);break;case"google-gemini":this.ai=new nn(e);break;case"anthropic":this.ai=new Vt(e);break;case"mistral":this.ai=new an(e);break;case"deepseek":this.ai=new tn(e);break;case"ollama":this.ai=new ln(e);break;case"reka":this.ai=new dn(e);break;case"webllm":this.ai=new gn(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 bo=require("@opentelemetry/api");var it=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:bo.SpanKind.SERVER,attributes:{[me.DB_SYSTEM]:this.name,[me.DB_OPERATION_NAME]:"upsert",[me.DB_TABLE]:e.table,[me.DB_NAMESPACE]:e.namespace,[me.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:bo.SpanKind.SERVER,attributes:{[me.DB_SYSTEM]:this.name,[me.DB_OPERATION_NAME]:"upsert",[me.DB_TABLE]:e[0].table,[me.DB_NAMESPACE]:e[0].namespace,[me.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:bo.SpanKind.SERVER,attributes:{[me.DB_SYSTEM]:this.name,[me.DB_OPERATION_NAME]:"upsert",[me.DB_TABLE]:e.table,[me.DB_NAMESPACE]:e.namespace,[me.DB_OPERATION_NAME]:"query"}},async t=>{try{return await this._query(e,{span:t})}finally{t.end()}}):await this._query(e)}};var Ys="https://api.cloudflare.com/client/v4/accounts/",fn=class extends it{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 Ke({url:new URL(`${this.accountId}/vectorize/indexes/${e.table}/upsert`,Ys),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 Ke({url:new URL(`${this.accountId}/vectorize/indexes/${r}/upsert`,Ys),headers:{"X-Auth-Key":this.apiKey},fetch:this.fetch,span:n?.span},e.map(s=>({id:s.id,values:s.values,namespace:s.namespace,metadata:s.metadata})));if(o.errors)throw new Error(`Cloudflare batch upsert failed: ${o.errors.map(({message:s})=>s).join(", ")}`);return{ids:o.result.ids}};query=async(e,t)=>{let n=await Ke({url:new URL(`${this.accountId}/vectorize/indexes/${e.table}/query`,Ys),headers:{"X-Auth-Key":this.apiKey},fetch:this.fetch,span:t?.span},{vector:e.values,topK:e.limit||10,returnValues:!0});if(n.errors)throw new Error(`Cloudflare query failed: ${n.errors.map(({message:o})=>o).join(", ")}`);return{matches:n.result.matches.map(({id:o,score:s,values:a,metadata:l})=>({id:o,score:s,values:a,metadata:l}))}}};var Ot=class extends it{state;constructor({tracer:e}={}){super({name:"Memory",tracer:e}),this.state={}}_upsert=async(e,t,n)=>{if(!this.state[e.table])this.state[e.table]={[e.id]:e};else{let r=this.state[e.table];if(!r)throw new Error(`Table not found: ${e.table}`);r[e.id]=e}return{ids:[e.id]}};_batchUpsert=async(e,t,n)=>{let r=[];for(let o of e){let s=await this.upsert(o,t);r.push(...s.ids)}return{ids:r}};_query=async(e,t)=>{let n=this.state[e.table];if(!n)return{matches:[]};let r=[];return Object.entries(n).forEach(([o,s])=>{if(e.values&&s.values){let a=Ac(e.values,s.values);r.push({id:o,score:a,metadata:s.metadata})}}),r.sort((o,s)=>o.score-s.score),e.limit&&(r.length=e.limit),{matches:r}};getDB=()=>structuredClone(this.state);setDB=e=>{this.state=structuredClone(e)};clearDB=()=>{this.state={}}},Ac=(i,e)=>{if(i.length!==e.length)throw new Error("Vectors must be of the same length.");let t=0,n=0,r=0,o=!0,s=!0,a=new Float64Array(i),l=new Float64Array(e);for(let d=0;d<a.length;d++)t+=a[d]*l[d],n+=a[d]*a[d],r+=l[d]*l[d],a[d]!==0&&(o=!1),l[d]!==0&&(s=!1);if(o||s)return 1;let u=Math.sqrt(n),c=Math.sqrt(r);return 1-t/(u*c)};var yc=i=>({namespace:i.namespace,topK:i.limit||10,filter:{},includeValues:!0,includeMetadata:!0,vector:i.values??[],id:i.id}),xn=class extends it{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 Ke({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:"/vectors/upsert",fetch:this.fetch,span:n?.span},e.map(({id:r,values:o=[],metadata:s})=>({id:r,values:o,metadata:s}))),{ids:e.map(({id:r})=>r)}};query=async(e,t)=>{if(e.text)throw new Error("Pinecone does not support text");return{matches:(await Ke({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:"/query",fetch:this.fetch,span:t?.span},yc(e))).matches.map(({id:o,score:s,values:a,metadata:l})=>({id:o,score:s,metadata:l,values:a}))}}};var An=class extends it{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 Ke({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:`/v1/objects/${e.table}/${e.id}`,put:!!t,fetch:this.fetch,span:n?.span},{id:e.id,class:e.table,tenant:e.namespace,vector:e.values,properties:e.metadata??{}});if(r?.result?.errors)throw new Error(`Weaviate upsert failed: ${r.result.errors.error.map(({message:o})=>o).join(", ")}`);return{ids:[r.id]}};_batchUpsert=async(e,t,n)=>{if(t)throw new Error("Weaviate does not support batch update");if(e.length===0)throw new Error("Batch request is empty");let r=e.map(s=>({id:s.id,class:s.table,tenant:s.namespace,vector:s.values,properties:s.metadata??{}})),o=await Ke({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:"/v1/batch/objects",fetch:this.fetch,span:n?.span},{objects:r});if(o?.some(({result:s})=>s?.errors))throw new Error(`Weaviate batch upsert failed: ${o.map(({result:s})=>s?.errors?.error.map(({message:a})=>a).join(", ")).join(", ")}`);return{ids:o.map(({id:s})=>s)}};_query=async(e,t)=>{let n="";if(e.columns&&e.columns.length===0)throw new Error("Weaviate requires at least one column");if(e.values)n=`nearVector: {
177
+ `);throw new Re(n,void 0,e)}return i.filter(n=>n.type==="output_text").map(n=>n.text).join(`
178
+ `)};function po(i){let e=[];for(let t of i??[])if(t?.type==="output_text"&&Array.isArray(t.annotations))for(let n of t.annotations)n&&n.type==="url_citation"&&typeof n.url=="string"&&e.push({url:n.url,title:n.title,description:n.description});return e.length?e:void 0}var sr=()=>({model:"gpt-4o",embedModel:"text-embedding-ada-002",temperature:.7,topP:1,stream:!0}),qa=()=>({...sr(),model:"gpt-4o",temperature:.5}),za=()=>({...sr(),model:"gpt-4o",temperature:.9}),or=class extends Me{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o=[],models:s,responsesReqUpdater:a,supportFor:l={functions:!0,streaming:!0,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}){if(!e||e==="")throw new Error("OpenAI API key not set");let u=new ln(t,n?.streamingUsage??!0,a),c=s?.map(p=>{let d=p,m=d?.config;if(!m)return p;let g={};m.maxTokens!==void 0&&(g.maxTokens=m.maxTokens),m.temperature!==void 0&&(g.temperature=m.temperature),m.topP!==void 0&&(g.topP=m.topP),m.presencePenalty!==void 0&&(g.presencePenalty=m.presencePenalty),m.frequencyPenalty!==void 0&&(g.frequencyPenalty=m.frequencyPenalty);let h=m.stopSequences??m.stop;h!==void 0&&(g.stopSequences=h),m.n!==void 0&&(g.n=m.n),m.stream!==void 0&&(g.stream=m.stream);let f={...d};Object.keys(g).length>0&&(f.modelConfig={...d.modelConfig??{},...g});let I=m?.thinking?.thinkingTokenBudget;if(typeof I=="number"){let x=[["minimal",200],["low",800],["medium",5e3],["high",1e4],["highest",24500]],A="minimal",w=Number.POSITIVE_INFINITY;for(let[b,T]of x){let v=Math.abs(I-T);v<w&&(w=v,A=b)}f.thinkingTokenBudget=A}return m?.thinking?.includeThoughts!==void 0&&(f.showThoughts=!!m.thinking.includeThoughts),f});super(u,{name:"OpenAI",apiURL:r||"https://api.openai.com/v1",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:o,defaults:{model:t.model??"gpt-4o",embedModel:t.embedModel??"text-embedding-ada-002"},options:n,supportFor:l,models:c??s})}},un=class extends or{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("OpenAI API key not set");o=[...zr,...o??[]];let s=a=>{let l=nt({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:{...sr(),...t},options:n,modelInfo:o,models:r,supportFor:s})}};var Hs=()=>structuredClone({model:"openrouter/auto",...be()}),cn=class extends ke{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o,referer:s,title:a}){if(!e||e==="")throw new Error("OpenRouter API key not set");let l={...Hs(),...t},u={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},c=o??[];super({apiKey:e,config:l,options:n,apiURL:"https://openrouter.ai/api/v1",modelInfo:c,models:r,supportFor:u}),super.setName("OpenRouter"),super.setHeaders(async()=>{let p={Authorization:`Bearer ${e}`};return s&&(p["HTTP-Referer"]=s),a&&(p["X-Title"]=a),p})}};async function mo(i,e,t={}){if(typeof i=="string")return[{type:"text",text:i}];if(!Array.isArray(i))return[{type:"text",text:String(i)}];let n=e.getFeatures(),r=[];for(let o of i)try{switch(o.type){case"text":r.push({type:"text",text:o.text});break;case"image":if(n.media.images.supported)o.altText?r.push({type:"text",text:`[Image: ${o.altText}]`}):r.push({type:"text",text:"[Image content]"});else if(o.altText)r.push({type:"text",text:o.altText});else if(t.imageToText)try{let s=await t.imageToText(o.image);r.push({type:"text",text:s})}catch(s){throw new dt(s,"image","vision analysis")}else switch(t.fallbackBehavior){case"error":throw new ot("Images",e.getName(),!1);case"skip":continue;default:r.push({type:"text",text:"[Image content not supported by this provider]"})}break;case"audio":if(n.media.audio.supported)o.transcription?r.push({type:"text",text:o.transcription}):r.push({type:"text",text:"[Audio content]"});else if(o.transcription)r.push({type:"text",text:o.transcription});else if(t.audioToText)try{let s=await t.audioToText(o.data,o.format);r.push({type:"text",text:s})}catch(s){throw new dt(s,"audio","transcription")}else switch(t.fallbackBehavior){case"error":throw new ot("Audio",e.getName(),!1);case"skip":continue;case"degrade":r.push({type:"text",text:"[Audio content not supported by this provider]"})}break;case"file":if(n.media.files.supported)o.extractedText?r.push({type:"text",text:o.extractedText}):r.push({type:"text",text:`[File: ${o.filename}]`});else if(o.extractedText)r.push({type:"text",text:o.extractedText});else if(t.fileToText)try{let s=await t.fileToText(o.data,o.mimeType);r.push({type:"text",text:s})}catch(s){throw new dt(s,"file","text extraction")}else switch(t.fallbackBehavior){case"error":throw new ot("Files",e.getName(),!1);case"skip":continue;default:r.push({type:"text",text:`[File: ${o.filename} - content not accessible by this provider]`})}break;case"url":if(n.media.urls.supported)o.cachedContent?r.push({type:"text",text:o.cachedContent}):r.push({type:"text",text:`[Link: ${o.url}${o.title?` - ${o.title}`:""}]`});else if(o.cachedContent)r.push({type:"text",text:o.cachedContent});else if(t.urlToText)try{let s=await t.urlToText(o.url);r.push({type:"text",text:s})}catch(s){throw new dt(s,"url","content fetching")}else switch(t.fallbackBehavior){case"error":throw new ot("URLs",e.getName(),!1);case"skip":continue;case"degrade":r.push({type:"text",text:`[Link: ${o.url}${o.title?` - ${o.title}`:""}]`})}break;default:typeof o=="object"&&o.text?r.push({type:"text",text:o.text}):r.push({type:"text",text:String(o)})}}catch(s){throw s instanceof ot||s instanceof dt?s:new dt(s,o.type||"unknown","content processing")}return r}function Ha(i){let e=!1,t=!1,n=!1,r=!1;for(let o of i)if(o.role==="user"&&Array.isArray(o.content))for(let s of o.content)switch(s.type){case"image":e=!0;break;case"audio":t=!0;break;case"file":n=!0;break;case"url":r=!0;break}return{hasImages:e,hasAudio:t,hasFiles:n,hasUrls:r}}var ir=(n=>(n.RekaCore="reka-core",n.RekaFlash="reka-flash",n.RekaEdge="reka-edge",n))(ir||{});var go=[{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 ar=()=>structuredClone({model:"reka-core",...be()}),Wa=()=>structuredClone({...ar(),model:"reka-core"}),Va=()=>structuredClone({model:"reka-core",...Ne()}),Ja=()=>({...ar(),model:"reka-flash"}),Ks=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=yc(e),o=e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,s=e.modelConfig?.stream??this.config.stream,a={model:t,messages:r,max_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},top_k:e.modelConfig?.n??this.config.n,...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},stop:e.modelConfig?.stopSequences??this.config.stop,presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,...o?{frequency_penalty:o}:{},...s?{stream:!0}:{}};return[n,a]};createChatResp=e=>{let{id:t,usage:n,responses:r}=e;return this.tokensUsed=n?{promptTokens:n.input_tokens,completionTokens:n.output_tokens,totalTokens:n.input_tokens+n.output_tokens}:void 0,{results:r.map((s,a)=>{let l=Ka(s.finish_reason),u;return typeof s.message.content=="string"?u=s.message.content:u=s.message.content.text,{index:a,id:`${t}`,content:u,finishReason:l}}),remoteId:t}};createChatStreamResp=e=>{let{id:t,usage:n,responses:r}=e;return this.tokensUsed=n?{promptTokens:n.input_tokens,completionTokens:n.output_tokens,totalTokens:n.input_tokens+n.output_tokens}:void 0,{results:r.map((s,a)=>{let l=Ka(s.finish_reason),u;return typeof s.chunk.content=="string"?u=s.chunk.content:u=s.chunk.content.text,{index:a,id:`${t}`,content:u,finishReason:l}})}}},Ka=i=>{switch(i){case"stop":return"stop";case"context":return"length";case"length":return"length"}};function yc(i){return i.chatPrompt.map(e=>{switch(e.role){case"system":return{role:"user",content:e.content};case"user":return Array.isArray(e.content)?{role:"user",content:e.content.map(t=>{switch(t.type){case"text":return{type:"text",text:t.text};case"image":throw new Error("Image type not supported");default:throw new Error("Invalid content type")}})}:{role:"user",content:e.content};case"assistant":if(Array.isArray(e.content))return{role:"assistant",content:e.content.map(t=>{switch(t.type){case"text":return{type:"text",text:t.text};case"image":throw new Error("Image type not supported");default:throw new Error("Invalid content type")}})};if(!e.content)throw new Error("Assistant content is empty");return{role:"user",content:e.content};default:throw new Error("Invalid role")}})}var pn=class extends Me{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o=go,models:s}){if(!e||e==="")throw new Error("Reka API key not set");let a={...ar(),...t},l=new Ks(a);super(l,{name:"Reka",apiURL:r||"https://api.reka.ai/v1/chat",headers:async()=>({"X-Api-Key":e}),modelInfo:o,defaults:{model:a.model},options:n,supportFor:{functions:!0,streaming:!0,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},models:s})}};var ho=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 ot&&t.fallbackProviders?.length)return await this.tryFallbackProviders(e,t.fallbackProviders,t);throw o}}async preprocessRequest(e,t,n){let r={...n,fallbackBehavior:n?.fallbackBehavior||"degrade",imageToText:n?.imageToText||this.processingServices.imageToText,audioToText:n?.audioToText||this.processingServices.audioToText,fileToText:n?.fileToText||this.processingServices.fileToText,urlToText:n?.urlToText||this.processingServices.urlToText},o=[];for(let s of e.chatPrompt)if(s.role==="user"&&Array.isArray(s.content)){let a=await mo(s.content,t,r);a.every(u=>u.type==="text")&&a.length===1?o.push({...s,content:a[0].text}):o.push({...s,content:a.map(u=>({type:"text",text:u.text}))})}else o.push(s);return{...e,chatPrompt:o}}async selectProviderWithDegradation(e,t){let n=Nt(e),r=[],o=[],s=[];try{let a=Qr(e,this.providers,{requireExactMatch:t.requireExactMatch??this.config.capability.requireExactMatch,allowDegradation:t.allowDegradation??this.config.capability.allowDegradation}),l=a.getFeatures();return n.hasImages&&!l.media.images.supported&&(o.push("Images will be converted to text descriptions"),r.push("Image-to-text conversion")),n.hasAudio&&!l.media.audio.supported&&(o.push("Audio will be transcribed to text"),r.push("Audio-to-text transcription")),n.hasFiles&&!l.media.files.supported&&(o.push("File content will be extracted to text"),r.push("File-to-text extraction")),n.hasUrls&&!l.media.urls.supported&&(o.push("URL content will be pre-fetched"),r.push("URL content fetching")),n.requiresStreaming&&!l.streaming&&s.push("Streaming not supported - will use non-streaming mode"),n.requiresCaching&&!l.caching.supported&&s.push("Content caching not supported"),{provider:a,processingApplied:r,degradations:o,warnings:s}}catch(a){throw new Error(`Provider selection failed: ${a instanceof Error?a.message:"Unknown error"}`)}}async tryFallbackProviders(e,t,n){for(let r of t)try{let o={provider:r,processingApplied:["Fallback provider selection"],degradations:["Using fallback provider due to primary provider failure"],warnings:[]},s=await this.preprocessRequest(e,r,{fallbackBehavior:"degrade"});return{response:await r.chat(s,n),routing:o}}catch{}throw new Error("All fallback providers failed")}async getRoutingRecommendation(e){return await this.selectProviderWithDegradation(e,{})}async validateRequest(e){let t=Nt(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 lr=(x=>(x.KimiK25="moonshotai/Kimi-K2.5",x.KimiK2Instruct0905="moonshotai/Kimi-K2-Instruct-0905",x.KimiK2Thinking="moonshotai/Kimi-K2-Thinking",x.DeepSeekV31="deepseek-ai/DeepSeek-V3.1",x.DeepSeekR1="deepseek-ai/DeepSeek-R1",x.GPTOSS120B="openai/gpt-oss-120b",x.GPTOSS20B="openai/gpt-oss-20b",x.Qwen35_397B="Qwen/Qwen3.5-397B-A17B",x.Qwen3CoderNext="Qwen/Qwen3-Coder-Next-FP8",x.Qwen3Coder480B="Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8",x.Qwen3_235BInstruct2507="Qwen/Qwen3-235B-A22B-Instruct-2507-tput",x.Qwen3_235BThinking2507="Qwen/Qwen3-235B-A22B-Thinking-2507",x.Qwen3Next80BInstruct="Qwen/Qwen3-Next-80B-A3B-Instruct",x.Qwen3Next80BThinking="Qwen/Qwen3-Next-80B-A3B-Thinking",x.GLM5="zai-org/GLM-5",x.GLM47="zai-org/GLM-4.7",x.Llama4Maverick="meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8",x.Llama33_70B="meta-llama/Llama-3.3-70B-Instruct-Turbo",x))(lr||{});var fo=[{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 Ws=()=>structuredClone({model:"meta-llama/Llama-3.3-70B-Instruct-Turbo",...be()}),dn=class extends ke{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Together API key not set");let s={...Ws(),...t};o=[...fo,...o??[]];let a={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0};super({apiKey:e,config:s,options:n,apiURL:"https://api.together.xyz/v1",modelInfo:o,models:r,supportFor:a}),super.setName("Together")}};var ur=(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))(ur||{});var xo=[{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 Js=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...be()}),Qa=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...Ne()}),Vs=class{constructor(e,t){this.config=e;this.engine=t}tokensUsed;engine;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,endSequences:e.endSequences,stream:e.stream,n:e.n}}createChatReq(e){let t=e.model,n=e.chatPrompt.map(a=>{if(a.role==="function")return{role:"function",name:a.functionId,content:typeof a.result=="string"?a.result:JSON.stringify(a.result)};let l="";typeof a.content=="string"?l=a.content:Array.isArray(a.content)&&(l=a.content.filter(c=>c.type==="text").map(c=>c.text).join(`
179
+ `));let u={role:a.role,content:l};return a.role==="assistant"&&a.functionCalls?.length?{...u,tool_calls:a.functionCalls.map(c=>({id:c.id,type:"function",function:{name:c.function.name,arguments:typeof c.function.params=="string"?c.function.params:JSON.stringify(c.function.params||{})}}))}:u}),r=e.functions?.map(a=>({type:"function",function:{name:a.name,description:a.description,parameters:a.parameters||{type:"object",properties:{}}}})),o={name:"/chat/completions",localCall:async(a,l)=>{try{let u=await this.engine.chat.completions.create({...a,stream:l||!1});return l?new ReadableStream({async start(c){try{for await(let p of u)c.enqueue(p);c.close()}catch(p){c.error(p)}}}):u}catch(u){throw new Error(`WebLLM API error: ${u}`)}}},s={model:t,messages:n,...r?.length?{tools:r}:{},max_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,frequency_penalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,stop:e.modelConfig?.stopSequences??this.config.stopSequences,stream:e.modelConfig?.stream??this.config.stream,n:e.modelConfig?.n??this.config.n};return[o,s]}createEmbedReq=e=>{throw new Error("WebLLM does not support embeddings")};createChatResp=e=>(this.tokensUsed={promptTokens:e.usage?.prompt_tokens??0,completionTokens:e.usage?.completion_tokens??0,totalTokens:e.usage?.total_tokens??0},{results:e.choices.map((n,r)=>{let o="stop";switch(n.finish_reason){case"stop":o="stop";break;case"length":o="length";break;case"tool_calls":o="function_call";break;case"content_filter":o="content_filter";break;default:o="stop";break}let s=n.message.tool_calls?.map(a=>({id:a.id,type:"function",function:{name:a.function.name,params:a.function.arguments}}));return{index:r,id:e.id,content:n.message.content||"",functionCalls:s,finishReason:o}}),remoteId:e.id});createChatStreamResp=(e,t)=>{let n=t,r=e.choices[0];if(!r)throw new Error("No choice in WebLLM stream response");if(r.delta.content&&(n.content=(n.content||"")+r.delta.content),r.delta.tool_calls){n.toolCalls||(n.toolCalls=[]);for(let l of r.delta.tool_calls){let u=n.toolCalls[l.index];u?l.function?.arguments&&(u.function.arguments=(u.function?.arguments||"")+l.function.arguments):n.toolCalls[l.index]={id:l.id,type:l.type,function:{name:l.function?.name,arguments:l.function?.arguments||""}}}}e.usage&&(this.tokensUsed={promptTokens:e.usage.prompt_tokens,completionTokens:e.usage.completion_tokens,totalTokens:e.usage.total_tokens});let o;if(r.finish_reason)switch(r.finish_reason){case"stop":o="stop";break;case"length":o="length";break;case"tool_calls":o="function_call";break;case"content_filter":o="content_filter";break;default:o="stop";break}let s=n.toolCalls?.map(l=>({id:l.id||"",type:"function",function:{name:l.function?.name||"",params:l.function?.arguments||""}}));return{results:[{index:0,id:e.id,content:n.content||"",functionCalls:s,finishReason:o}],remoteId:e.id}};createEmbedResp(e){throw new Error("WebLLM does not support embeddings")}},mn=class extends Me{constructor({engine:e,config:t,options:n,models:r}){if(!e)throw new Error("WebLLM engine instance is required");let o={...Js(),...t},s=new Vs(o,e);super(s,{name:"WebLLM",apiURL:void 0,headers:async()=>({}),modelInfo:xo,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 cr=(r=>(r.Grok3="grok-3",r.Grok3Mini="grok-3-mini",r.Grok3Fast="grok-3-fast",r.Grok3MiniFast="grok-3-mini-fast",r))(cr||{}),Qs=(e=>(e.GrokEmbedSmall="grok-embed-small",e))(Qs||{});var Ao=[{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 yo=()=>structuredClone({model:"grok-3-mini",...be()}),Ya=()=>structuredClone({...yo(),model:"grok-3"}),gn=class extends ke{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Grok API key not set");let s={...yo(),...t};o=[...Ao,...o??[]];let a=u=>{let c=nt({model:u,modelInfo:o,models:r});return{functions:!0,streaming:!0,hasThinkingBudget:c?.supported?.thinkingBudget??!1,hasShowThoughts:c?.supported?.showThoughts??!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}},l=u=>{if(n?.searchParameters){let c=n.searchParameters;return{...u,search_parameters:{mode:c.mode,return_citations:c.returnCitations,from_date:c.fromDate,to_date:c.toDate,max_search_results:c.maxSearchResults,sources:c.sources?.map(p=>({type:p.type,country:p.country,excluded_websites:p.excludedWebsites,allowed_websites:p.allowedWebsites,safe_search:p.safeSearch,x_handles:p.xHandles,links:p.links}))}}}return u};super({apiKey:e,config:s,options:n,apiURL:"https://api.x.ai/v1",modelInfo:o,models:r,supportFor:a,chatReqUpdater:l}),super.setName("Grok")}};function Xa(i){return pr.create(i)}var pr=class i{ai;static create(e){return new i(e)}constructor(e){switch(e.name){case"openai":this.ai=new Yt(e);break;case"openai-responses":this.ai=new un(e);break;case"azure-openai":this.ai=new Xt(e);break;case"grok":this.ai=new gn(e);break;case"huggingface":this.ai=new on(e);break;case"groq":this.ai=new rn(e);break;case"together":this.ai=new dn(e);break;case"openrouter":this.ai=new cn(e);break;case"cohere":this.ai=new Zt(e);break;case"google-gemini":this.ai=new tn(e);break;case"anthropic":this.ai=new Wt(e);break;case"mistral":this.ai=new sn(e);break;case"deepseek":this.ai=new en(e);break;case"ollama":this.ai=new an(e);break;case"reka":this.ai=new pn(e);break;case"webllm":this.ai=new mn(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 bo=require("@opentelemetry/api");var st=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:bo.SpanKind.SERVER,attributes:{[ge.DB_SYSTEM]:this.name,[ge.DB_OPERATION_NAME]:"upsert",[ge.DB_TABLE]:e.table,[ge.DB_NAMESPACE]:e.namespace,[ge.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:bo.SpanKind.SERVER,attributes:{[ge.DB_SYSTEM]:this.name,[ge.DB_OPERATION_NAME]:"upsert",[ge.DB_TABLE]:e[0].table,[ge.DB_NAMESPACE]:e[0].namespace,[ge.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:bo.SpanKind.SERVER,attributes:{[ge.DB_SYSTEM]:this.name,[ge.DB_OPERATION_NAME]:"upsert",[ge.DB_TABLE]:e.table,[ge.DB_NAMESPACE]:e.namespace,[ge.DB_OPERATION_NAME]:"query"}},async t=>{try{return await this._query(e,{span:t})}finally{t.end()}}):await this._query(e)}};var Ys="https://api.cloudflare.com/client/v4/accounts/",hn=class extends st{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`,Ys),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`,Ys),headers:{"X-Auth-Key":this.apiKey},fetch:this.fetch,span:n?.span},e.map(s=>({id:s.id,values:s.values,namespace:s.namespace,metadata:s.metadata})));if(o.errors)throw new Error(`Cloudflare batch upsert failed: ${o.errors.map(({message:s})=>s).join(", ")}`);return{ids:o.result.ids}};query=async(e,t)=>{let n=await qe({url:new URL(`${this.accountId}/vectorize/indexes/${e.table}/query`,Ys),headers:{"X-Auth-Key":this.apiKey},fetch:this.fetch,span:t?.span},{vector:e.values,topK:e.limit||10,returnValues:!0});if(n.errors)throw new Error(`Cloudflare query failed: ${n.errors.map(({message:o})=>o).join(", ")}`);return{matches:n.result.matches.map(({id:o,score:s,values:a,metadata:l})=>({id:o,score:s,values:a,metadata:l}))}}};var Ot=class extends st{state;constructor({tracer:e}={}){super({name:"Memory",tracer:e}),this.state={}}_upsert=async(e,t,n)=>{if(!this.state[e.table])this.state[e.table]={[e.id]:e};else{let r=this.state[e.table];if(!r)throw new Error(`Table not found: ${e.table}`);r[e.id]=e}return{ids:[e.id]}};_batchUpsert=async(e,t,n)=>{let r=[];for(let o of e){let s=await this.upsert(o,t);r.push(...s.ids)}return{ids:r}};_query=async(e,t)=>{let n=this.state[e.table];if(!n)return{matches:[]};let r=[];return Object.entries(n).forEach(([o,s])=>{if(e.values&&s.values){let a=bc(e.values,s.values);r.push({id:o,score:a,metadata:s.metadata})}}),r.sort((o,s)=>o.score-s.score),e.limit&&(r.length=e.limit),{matches:r}};getDB=()=>structuredClone(this.state);setDB=e=>{this.state=structuredClone(e)};clearDB=()=>{this.state={}}},bc=(i,e)=>{if(i.length!==e.length)throw new Error("Vectors must be of the same length.");let t=0,n=0,r=0,o=!0,s=!0,a=new Float64Array(i),l=new Float64Array(e);for(let d=0;d<a.length;d++)t+=a[d]*l[d],n+=a[d]*a[d],r+=l[d]*l[d],a[d]!==0&&(o=!1),l[d]!==0&&(s=!1);if(o||s)return 1;let u=Math.sqrt(n),c=Math.sqrt(r);return 1-t/(u*c)};var Ic=i=>({namespace:i.namespace,topK:i.limit||10,filter:{},includeValues:!0,includeMetadata:!0,vector:i.values??[],id:i.id}),fn=class extends st{apiKey;apiURL;constructor({apiKey:e,host:t,fetch:n,tracer:r}){if(!e||e==="")throw new Error("Pinecone API key not set");super({name:"Pinecone",fetch:n,tracer:r}),this.apiKey=e,this.apiURL=t}_upsert=async(e,t,n)=>(await this._batchUpsert([e],t,n),{ids:[e.id]});_batchUpsert=async(e,t,n)=>{if(e.length===0)throw new Error("Batch request is empty");return await qe({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:"/vectors/upsert",fetch:this.fetch,span:n?.span},e.map(({id:r,values:o=[],metadata:s})=>({id:r,values:o,metadata:s}))),{ids:e.map(({id:r})=>r)}};query=async(e,t)=>{if(e.text)throw new Error("Pinecone does not support text");return{matches:(await qe({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:"/query",fetch:this.fetch,span:t?.span},Ic(e))).matches.map(({id:o,score:s,values:a,metadata:l})=>({id:o,score:s,metadata:l,values:a}))}}};var xn=class extends st{apiKey;apiURL;constructor({apiKey:e,host:t,fetch:n,tracer:r}){if(!e||e==="")throw new Error("Weaviate API key not set");super({name:"Weaviate",fetch:n,tracer:r}),this.apiKey=e,this.apiURL=t}_upsert=async(e,t,n)=>{let r=await qe({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:`/v1/objects/${e.table}/${e.id}`,put:!!t,fetch:this.fetch,span:n?.span},{id:e.id,class:e.table,tenant:e.namespace,vector:e.values,properties:e.metadata??{}});if(r?.result?.errors)throw new Error(`Weaviate upsert failed: ${r.result.errors.error.map(({message:o})=>o).join(", ")}`);return{ids:[r.id]}};_batchUpsert=async(e,t,n)=>{if(t)throw new Error("Weaviate does not support batch update");if(e.length===0)throw new Error("Batch request is empty");let r=e.map(s=>({id:s.id,class:s.table,tenant:s.namespace,vector:s.values,properties:s.metadata??{}})),o=await qe({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:"/v1/batch/objects",fetch:this.fetch,span:n?.span},{objects:r});if(o?.some(({result:s})=>s?.errors))throw new Error(`Weaviate batch upsert failed: ${o.map(({result:s})=>s?.errors?.error.map(({message:a})=>a).join(", ")).join(", ")}`);return{ids:o.map(({id:s})=>s)}};_query=async(e,t)=>{let n="";if(e.columns&&e.columns.length===0)throw new Error("Weaviate requires at least one column");if(e.values)n=`nearVector: {
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 Ke({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 qe({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 Io=class{db;constructor(e){switch(e.name){case"weaviate":this.db=new An(e);break;case"pinecone":this.db=new xn(e);break;case"cloudflare":this.db=new fn(e);break;case"memory":this.db=new Ot(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 Xs="_internal",Co=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 Io=class{db;constructor(e){switch(e.name){case"weaviate":this.db=new xn(e);break;case"pinecone":this.db=new fn(e);break;case"cloudflare":this.db=new hn(e);break;case"memory":this.db=new Ot(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 Xs="_internal",Co=class{ai;db;chunker;rewriter;reranker;constructor({ai:e,db:t,config:n}){this.ai=e,this.db=t,this.chunker=n?.chunker??this.defaultChunker,this.reranker=n?.reranker,this.rewriter=n?.rewriter}defaultChunker=e=>e.split(/\n\n+/);insert=async(e,t)=>{try{let n=Array.isArray(e)?e.join(`
194
194
 
195
- `):e,r=this.chunker(n).filter(u=>u.length>0),o=t?.maxWordsPerChunk,s=t?.minWordsPerChunk,a=bc({initialChunks:r,minWordsPerChunk:s,maxWordsPerChunk:o}),l=t?.batchSize??10;for(let u=0;u<a.length;u+=l){let c=a.slice(u,u+l),d=(await this.ai.embed({texts:c},{abortSignal:t?.abortSignal})).embeddings.map((m,g)=>({id:`chunk_${Date.now()+g}`,table:Xs,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:Xs,values:u})):o=r.map(l=>this.db.query({table:Xs,values:l}));let s=await Promise.all(o),a=[];for(let{matches:l}of s){let u=l.filter(d=>d.metadata?.text&&d.metadata?.text.length>0).map(({score:d,metadata:m})=>({score:d,text:m?.text??""})),c=t&&t>1?t/100:t,p=c?Ic(u,c):u;if(this.reranker){let{rankedItems:d}=await this.reranker.forward(this.ai,{query:r[0],items:p.map(g=>g.text)}),m=d.map(g=>p.find(h=>h.text===g)).filter(g=>g!==void 0);a.push(m)}else a.push(p)}return a}},bc=({initialChunks:i,maxWordsPerChunk:e=350,minWordsPerChunk:t=250})=>{let n=[],r="",o=0;return i.forEach(s=>{let a=s.split(/\s+/),l=a.length;if(o+l<=e)r+=`${s}
195
+ `):e,r=this.chunker(n).filter(u=>u.length>0),o=t?.maxWordsPerChunk,s=t?.minWordsPerChunk,a=Cc({initialChunks:r,minWordsPerChunk:s,maxWordsPerChunk:o}),l=t?.batchSize??10;for(let u=0;u<a.length;u+=l){let c=a.slice(u,u+l),d=(await this.ai.embed({texts:c},{abortSignal:t?.abortSignal})).embeddings.map((m,g)=>({id:`chunk_${Date.now()+g}`,table:Xs,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:Xs,values:u})):o=r.map(l=>this.db.query({table:Xs,values:l}));let s=await Promise.all(o),a=[];for(let{matches:l}of s){let u=l.filter(d=>d.metadata?.text&&d.metadata?.text.length>0).map(({score:d,metadata:m})=>({score:d,text:m?.text??""})),c=t&&t>1?t/100:t,p=c?Tc(u,c):u;if(this.reranker){let{rankedItems:d}=await this.reranker.forward(this.ai,{query:r[0],items:p.map(g=>g.text)}),m=d.map(g=>p.find(h=>h.text===g)).filter(g=>g!==void 0);a.push(m)}else a.push(p)}return a}},Cc=({initialChunks:i,maxWordsPerChunk:e=350,minWordsPerChunk:t=250})=>{let n=[],r="",o=0;return i.forEach(s=>{let a=s.split(/\s+/),l=a.length;if(o+l<=e)r+=`${s}
196
196
 
197
197
  `,o+=l;else if(o>0&&o+l<=e*1.5)r+=`${s}
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=`${s}
202
202
 
203
- `,o=l}),(o>t||n.length===0)&&n.push(r.trim()),n},Ic=(i,e=.1)=>{let t=[...i].sort((r,o)=>r.score-o.score),n=Math.ceil(t.length*e);return t.slice(0,n)};var En=require("@opentelemetry/api");var mr=class{data=[];seenTags=new Set;addRequest(e,t){this.data.push(...e.map(n=>{let r=structuredClone(n);return{role:n.role,chat:[{index:t,value:r}]}}))}addFunctionResults(e){let t=e.map(({index:r,...o})=>({index:r,value:structuredClone(o)})),n=this.getLast();n?.role==="function"?n.chat.push(...t):this.data.push({role:"function",chat:t})}addResponse(e){let t=e.map(({index:n,...r})=>({index:n,value:structuredClone(r)}));this.data.push({role:"assistant",chat:t})}updateResult({content:e,name:t,functionCalls:n,thought:r,thoughtBlocks:o,index:s}){let a=this.data.at(-1);if(!a||a.role!=="assistant"||a.role==="assistant"&&!a.updatable){this.data.push({role:"assistant",updatable:!0,chat:[{index:s,value:structuredClone({content:e,name:t,functionCalls:n,thought:r,thoughtBlocks:o})}]});return}let l=a.chat.find(u=>u.index===s);if(!l){a.chat.push({index:s,value:structuredClone({content:e,name:t,functionCalls:n,thought:r,thoughtBlocks:o})});return}if(typeof e=="string"&&e.trim()!==""&&(l.value.content=e),typeof t=="string"&&t.trim()!==""&&(l.value.name=t),Array.isArray(n)&&n.length>0&&(l.value.functionCalls=n),typeof r=="string"&&r.trim()!==""){let u=l.value.thought;l.value.thought=typeof u=="string"?u+r:r}if(Array.isArray(o)&&o.length>0){let u=l.value.thoughtBlocks??[];for(let c of o){let p=u.length>0?u[u.length-1]:void 0;!c.signature&&c.data?p&&!p.signature?(p.data=(p.data??"")+c.data,c.encrypted&&(p.encrypted=!0)):u.push(structuredClone(c)):c.signature?p&&!p.signature?(p.data=(p.data??"")+c.data,p.signature=c.signature,c.encrypted&&(p.encrypted=!0)):u.push(structuredClone(c)):u.length===0&&u.push(structuredClone(c))}l.value.thoughtBlocks=u,u.length>0&&(l.value.thought=u.map(c=>c.data).join(""))}}addTag(e){let t=this.data.at(-1);t&&(t.tags||(t.tags=[]),t.tags.includes(e)||t.tags.push(e),this.seenTags.add(e))}rewindToTag(e){let t=this.data.findIndex(n=>n.tags?.includes(e));if(t===-1){if(!this.seenTags.has(e))throw new Error(`Tag "${e}" not found`);return[]}return this.data.splice(t)}removeByTag(e){let t=this.data.reduce((n,r,o)=>(r.tags?.includes(e)&&n.push(o),n),[]);return t.length===0?[]:t.reverse().map(n=>this.data.splice(n,1).at(0)).filter(n=>n!==void 0).reverse()}history(e){let t=[];for(let{role:n,chat:r}of this.data){let o;n==="function"?o=r.filter(s=>s.index===e).map(s=>s.value):o=r.find(s=>s.index===e)?.value,Array.isArray(o)&&o.length>0?t.push(...o.map(s=>({...s,role:n}))):typeof o=="object"&&o!==null&&t.push({...o,role:n})}return t}getLast(){return this.data.at(-1)}reset(){this.data=[],this.seenTags=new Set}},yn=class{memories=new Map;defaultMemory;constructor(){this.defaultMemory=new mr}getMemory(e){return e?(this.memories.has(e)||this.memories.set(e,new mr),this.memories.get(e)):this.defaultMemory}addRequest(e,t){for(let n of e)Kt(n);this.getMemory(t).addRequest(e,0)}addResponse(e,t){qr(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 mr):this.defaultMemory.reset()}};function vt(i,e){if(!i&&!e)return;if(!i)return e;if(!e||i===e||i.aborted)return i;if(e.aborted)return e;if(typeof AbortSignal.any=="function")return AbortSignal.any([i,e]);let t=new AbortController,n=()=>{t.abort(i.aborted?i.reason:e.reason),r()},r=()=>{i.removeEventListener("abort",n),e.removeEventListener("abort",n)};return i.addEventListener("abort",n,{once:!0}),e.addEventListener("abort",n,{once:!0}),t.signal}var At=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()}},bn=async(i,e)=>{for(let t of i){let{fn:n,message:r}=t,o=await n(e);if(o!==void 0){if(typeof o=="string")throw new At({message:o});if(!o)throw r?new At({message:r}):new Error("Assertion Failed: No message provided for assertion")}}},Zs=async(i,e,t,n=!1)=>{if(!e.currField||e.s===-1||!i||i.length===0)return;let r=i.filter(s=>s.fieldName===e.currField?.name);if(r.length===0)return;let o=t.substring(e.s);for(let s of r){let{message:a,fn:l}=s,u=await l(o,n);if(u!==void 0){if(typeof u=="string")throw new At({message:u});if(!u&&a)throw new At({message:a})}}};var ei={enabled:!0,enabledCategories:["generation","streaming","functions","errors","performance"],maxLabelLength:100,samplingRate:1},gr,ti=i=>{if(gr)return gr;let e=i??ce.meter;if(e)return gr=Cc(e),gr};var Za=()=>{let i=[];return ce.meter||i.push("Global meter not initialized"),!gr&&ce.meter&&i.push("Metrics instruments not created despite available meter"),{healthy:i.length===0,issues:i}},Cc=i=>({generationLatencyHistogram:i.createHistogram("ax_gen_generation_duration_ms",{description:"End-to-end duration of AxGen generation requests",unit:"ms"}),generationRequestsCounter:i.createCounter("ax_gen_generation_requests_total",{description:"Total number of AxGen generation requests"}),generationErrorsCounter:i.createCounter("ax_gen_generation_errors_total",{description:"Total number of failed AxGen generations"}),multiStepGenerationsCounter:i.createCounter("ax_gen_multistep_generations_total",{description:"Total number of generations that required multiple steps"}),stepsPerGenerationHistogram:i.createHistogram("ax_gen_steps_per_generation",{description:"Number of steps taken per generation"}),maxStepsReachedCounter:i.createCounter("ax_gen_max_steps_reached_total",{description:"Total number of generations that hit max steps limit"}),validationErrorsCounter:i.createCounter("ax_gen_validation_errors_total",{description:"Total number of validation errors encountered"}),assertionErrorsCounter:i.createCounter("ax_gen_assertion_errors_total",{description:"Total number of assertion errors encountered"}),errorCorrectionAttemptsHistogram:i.createHistogram("ax_gen_error_correction_attempts",{description:"Number of error correction attempts per generation"}),errorCorrectionSuccessCounter:i.createCounter("ax_gen_error_correction_success_total",{description:"Total number of successful error corrections"}),errorCorrectionFailureCounter:i.createCounter("ax_gen_error_correction_failure_total",{description:"Total number of failed error corrections"}),maxRetriesReachedCounter:i.createCounter("ax_gen_max_retries_reached_total",{description:"Total number of generations that hit max retries limit"}),functionsEnabledGenerationsCounter:i.createCounter("ax_gen_functions_enabled_generations_total",{description:"Total number of generations with functions enabled"}),functionCallStepsCounter:i.createCounter("ax_gen_function_call_steps_total",{description:"Total number of steps that included function calls"}),functionsExecutedPerGenerationHistogram:i.createHistogram("ax_gen_functions_executed_per_generation",{description:"Number of unique functions executed per generation"}),functionErrorCorrectionCounter:i.createCounter("ax_gen_function_error_correction_total",{description:"Total number of function-related error corrections"}),fieldProcessorsExecutedCounter:i.createCounter("ax_gen_field_processors_executed_total",{description:"Total number of field processors executed"}),streamingFieldProcessorsExecutedCounter:i.createCounter("ax_gen_streaming_field_processors_executed_total",{description:"Total number of streaming field processors executed"}),streamingGenerationsCounter:i.createCounter("ax_gen_streaming_generations_total",{description:"Total number of streaming generations"}),streamingDeltasEmittedCounter:i.createCounter("ax_gen_streaming_deltas_emitted_total",{description:"Total number of streaming deltas emitted"}),streamingFinalizationLatencyHistogram:i.createHistogram("ax_gen_streaming_finalization_duration_ms",{description:"Duration of streaming response finalization",unit:"ms"}),samplesGeneratedHistogram:i.createHistogram("ax_gen_samples_generated",{description:"Number of samples generated per request"}),resultPickerUsageCounter:i.createCounter("ax_gen_result_picker_usage_total",{description:"Total number of times result picker was used"}),resultPickerLatencyHistogram:i.createHistogram("ax_gen_result_picker_duration_ms",{description:"Duration of result picker execution",unit:"ms"}),inputFieldsGauge:i.createGauge("ax_gen_input_fields",{description:"Number of input fields in signature"}),outputFieldsGauge:i.createGauge("ax_gen_output_fields",{description:"Number of output fields in signature"}),examplesUsedGauge:i.createGauge("ax_gen_examples_used",{description:"Number of examples used in generation"}),demosUsedGauge:i.createGauge("ax_gen_demos_used",{description:"Number of demos used in generation"}),promptRenderLatencyHistogram:i.createHistogram("ax_gen_prompt_render_duration_ms",{description:"Duration of prompt template rendering",unit:"ms"}),extractionLatencyHistogram:i.createHistogram("ax_gen_extraction_duration_ms",{description:"Duration of value extraction from responses",unit:"ms"}),assertionLatencyHistogram:i.createHistogram("ax_gen_assertion_duration_ms",{description:"Duration of assertion checking",unit:"ms"}),stateCreationLatencyHistogram:i.createHistogram("ax_gen_state_creation_duration_ms",{description:"Duration of state creation for multiple samples",unit:"ms"}),memoryUpdateLatencyHistogram:i.createHistogram("ax_gen_memory_update_duration_ms",{description:"Duration of memory updates during generation",unit:"ms"})}),To=ei,el=i=>{To={...To,...i}},tl=()=>({...To}),ht=i=>{let e={};for(let[t,n]of Object.entries(i))if(n!=null){let r=String(n),o=To.maxLabelLength;e[t]=r.length>o?r.substring(0,o):r}return e};var nl=(i,e,t,n,r,o,s)=>{try{let a=ht({success:t.toString(),...n?{signature:n}:{},...r?{ai_service:r}:{},...o?{model:o}:{},...s});i.generationLatencyHistogram&&i.generationLatencyHistogram.record(e,a),i.generationRequestsCounter&&i.generationRequestsCounter.add(1,a),!t&&i.generationErrorsCounter&&i.generationErrorsCounter.add(1,a)}catch(a){console.warn("Failed to record generation metric:",a)}},wo=(i,e,t,n,r)=>{try{let o=ht({...n?{signature:n}:{},...r});e>1&&i.multiStepGenerationsCounter&&i.multiStepGenerationsCounter.add(1,o),i.stepsPerGenerationHistogram&&i.stepsPerGenerationHistogram.record(e,o),e>=t&&i.maxStepsReachedCounter&&i.maxStepsReachedCounter.add(1,o)}catch(o){console.warn("Failed to record multi-step metric:",o)}},ni=(i,e,t,n)=>{try{let r=ht({error_type:e,...t?{signature:t}:{},...n});e==="validation"&&i.validationErrorsCounter&&i.validationErrorsCounter.add(1,r),e==="assertion"&&i.assertionErrorsCounter&&i.assertionErrorsCounter.add(1,r)}catch(r){console.warn("Failed to record validation error metric:",r)}},rl=(i,e,t)=>{try{let n=ht({error_type:"refusal",...e?{signature:e}:{},...t});i.validationErrorsCounter&&i.validationErrorsCounter.add(1,n)}catch(n){console.warn("Failed to record refusal error metric:",n)}},ri=(i,e,t,n,r,o)=>{try{let s=ht({success:t.toString(),...r?{signature:r}:{},...o});i.errorCorrectionAttemptsHistogram&&i.errorCorrectionAttemptsHistogram.record(e,s),t&&i.errorCorrectionSuccessCounter&&i.errorCorrectionSuccessCounter.add(1,s),t||(i.errorCorrectionFailureCounter&&i.errorCorrectionFailureCounter.add(1,s),e>=n&&i.maxRetriesReachedCounter&&i.maxRetriesReachedCounter.add(1,s))}catch(s){console.warn("Failed to record error correction metric:",s)}},ol=(i,e,t,n,r=!1,o,s)=>{try{let a=ht({functions_enabled:e.toString(),had_function_calls:n.toString(),...o?{signature:o}:{},...s});e&&i.functionsEnabledGenerationsCounter&&i.functionsEnabledGenerationsCounter.add(1,a),n&&i.functionCallStepsCounter&&i.functionCallStepsCounter.add(1,a),t>0&&i.functionsExecutedPerGenerationHistogram&&i.functionsExecutedPerGenerationHistogram.record(t,a),r&&i.functionErrorCorrectionCounter&&i.functionErrorCorrectionCounter.add(1,a)}catch(a){console.warn("Failed to record function calling metric:",a)}},sl=(i,e,t,n,r)=>{try{let o=ht({...n?{signature:n}:{},...r});e>0&&i.fieldProcessorsExecutedCounter&&i.fieldProcessorsExecutedCounter.add(e,o),t>0&&i.streamingFieldProcessorsExecutedCounter&&i.streamingFieldProcessorsExecutedCounter.add(t,o)}catch(o){console.warn("Failed to record field processing metric:",o)}},il=(i,e,t,n,r,o)=>{try{let s=ht({is_streaming:e.toString(),...r?{signature:r}:{},...o});e&&i.streamingGenerationsCounter&&i.streamingGenerationsCounter.add(1,s),t>0&&i.streamingDeltasEmittedCounter&&i.streamingDeltasEmittedCounter.add(t,s),n&&i.streamingFinalizationLatencyHistogram&&i.streamingFinalizationLatencyHistogram.record(n,s)}catch(s){console.warn("Failed to record streaming metric:",s)}},al=(i,e,t,n,r,o)=>{try{let s=ht({result_picker_used:t.toString(),...r?{signature:r}:{},...o});i.samplesGeneratedHistogram&&i.samplesGeneratedHistogram.record(e,s),t&&i.resultPickerUsageCounter&&i.resultPickerUsageCounter.add(1,s),n&&i.resultPickerLatencyHistogram&&i.resultPickerLatencyHistogram.record(n,s)}catch(s){console.warn("Failed to record samples metric:",s)}},ll=(i,e,t,n,r,o,s)=>{try{let a=ht({...o?{signature:o}:{},...s});i.inputFieldsGauge&&i.inputFieldsGauge.record(e,a),i.outputFieldsGauge&&i.outputFieldsGauge.record(t,a),i.examplesUsedGauge&&i.examplesUsedGauge.record(n,a),i.demosUsedGauge&&i.demosUsedGauge.record(r,a)}catch(a){console.warn("Failed to record signature complexity metrics:",a)}},Ro=(i,e,t,n,r)=>{try{let o=ht({metric_type:e,...n?{signature:n}:{},...r});switch(e){case"prompt_render":i.promptRenderLatencyHistogram&&i.promptRenderLatencyHistogram.record(t,o);break;case"extraction":i.extractionLatencyHistogram&&i.extractionLatencyHistogram.record(t,o);break;case"assertion":i.assertionLatencyHistogram&&i.assertionLatencyHistogram.record(t,o);break;case"state_creation":i.stateCreationLatencyHistogram&&i.stateCreationLatencyHistogram.record(t,o);break;case"memory_update":i.memoryUpdateLatencyHistogram&&i.memoryUpdateLatencyHistogram.record(t,o);break}}catch(o){console.warn("Failed to record performance metric:",o)}};var In=i=>{let e=(()=>{switch(i?.name){case"string":return"string";case"number":return"number";case"boolean":return"boolean";case"date":return'date ("YYYY-MM-DD" format)';case"datetime":return'date time ("YYYY-MM-DD HH:mm Timezone" format)';case"json":return"JSON object";case"class":return"classification class";case"code":return"code";case"object":return"object";default:return"string"}})();return i?.isArray?`array of ${e}s`:e},Ue=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()}},ul=i=>{let t=i.map(n=>`'${n.title}' (${In(n.type)})`).join(", ");return new Ue(`Required field not found: ${t}. Add a line starting with the exact label followed by a colon (e.g., "${i[0]?.title}:") and then provide a valid ${In(i[0]?.type)} value. Keep the output concise and avoid unrelated text.`)},So=i=>new Ue(`Expected (Required) field not found: '${i.title}'. Begin a new section with "${i.title}:" and then provide a valid ${In(i.type)} value directly after.`);var vo=i=>new Ue(`Required field is missing: '${i.title}'. After the "${i.title}:" label, provide a non-empty ${In(i.type)}. Do not use null, undefined, or leave it blank.`),cl=(i,e)=>new Ue(`Invalid JSON: ${e} in field '${i.title}'. Return only valid JSON. Prefer a fenced code block containing a single JSON object or array with no trailing text.`),pl=(i,e)=>new Ue(`Invalid Array: ${e} for '${i.title}'. Provide a JSON array of ${In(i.type)} items (e.g., [ ... ]). Markdown lists are also accepted if each item is on its own line starting with a hyphen.`),dl=(i,e,t)=>new Ue(`Field '${i.title}' has an invalid value '${e}': ${t}. Provide a ${In(i.type)}. Ensure formatting exactly matches the expected type.`),ml=(i,e,t)=>new Ue(`Invalid date for '${i.title}': ${t}. Use the exact format YYYY-MM-DD (e.g., 2024-05-09). You provided: ${e}.`),gl=(i,e,t)=>new Ue(`Invalid date/time for '${i.title}': ${t}. Use the format YYYY-MM-DD HH:mm or YYYY-MM-DD HH:mm:ss followed by a valid timezone (e.g., America/New_York). You provided: ${e}.`),oi=(i,e,t)=>new Ue(`Invalid URL for '${i.title}': ${t}. Use a valid URL format (e.g., https://example.com). You provided: ${e}.`),Cn=(i,e,t,n)=>{let r=`Field '${i.title}' failed validation: `;return t==="minLength"?r+=`String must be at least ${n} characters long. You provided: "${e}" (${e.length} characters).`:t==="maxLength"?r+=`String must be at most ${n} characters long. You provided: "${e}" (${e.length} characters).`:t==="pattern"?r+=`String must match pattern /${n}/. You provided: "${e}".`:t==="format"&&(r+=`String must be a ${n}. You provided: "${e}".`),new Ue(r)},si=(i,e,t,n)=>{let r=`Field '${i.title}' failed validation: `;return t==="minimum"?r+=`Number must be at least ${n}. You provided: ${e}.`:t==="maximum"&&(r+=`Number must be at most ${n}. You provided: ${e}.`),new Ue(r)};var hl=({error:i,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:s,customLabels:a})=>{let l=i.getFixingInstructions();if(t&&n){let u=l?.map(c=>c.title).join(", ")??"";Zi(i,e,u,n)}return r&&ni(r,"validation",o,a),s&&s.addEvent("validation.error",{message:i.toString(),fixing_instructions:l?.map(u=>u.title).join(", ")??""}),l},fl=({error:i,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:s,customLabels:a})=>{let l=i.getFixingInstructions();if(t&&n){let u=l?.map(c=>c.title).join(", ")??"";ea(i,e,u,n)}return r&&ni(r,"assertion",o,a),s&&s.addEvent("assertion.error",{message:i.toString(),fixing_instructions:l?.map(u=>u.title).join(", ")??""}),l},xl=({error:i,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:s,customLabels:a})=>{t&&n&&ta(i,e,n),r&&rl(r,o,a),s&&s.addEvent("refusal.error",{message:i.toString()})};var Mt=Kn(require("dayjs"),1),Al=Kn(require("dayjs/plugin/customParseFormat.js"),1),yl=Kn(require("dayjs/plugin/timezone.js"),1),bl=Kn(require("dayjs/plugin/utc.js"),1);Mt.default.extend(bl.default);Mt.default.extend(yl.default);Mt.default.extend(Al.default);function Il(i,e,t=!1){try{return Tc(e)}catch(n){if(i.isOptional&&!t)return;let r=n.message;throw ml(i,e,r)}}function Tc(i){if(!(0,Mt.default)(i,"YYYY-MM-DD",!0).isValid())throw new Error('Invalid date format. Please provide the date in "YYYY-MM-DD" format.');return Mt.default.utc(i,"YYYY-MM-DD").startOf("day").toDate()}function Cl(i,e,t=!1){try{return wc(e)}catch(n){if(i.isOptional&&!t)return;let r=n.message;throw gl(i,e,r)}}function wc(i){let e=/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}(?::\d{2})?) (.+)$/,t=i.match(e);if(!t)throw new Error('Invalid date and time format. Please provide the date and time in "YYYY-MM-DD HH:mm" or "YYYY-MM-DD HH:mm:ss" format, followed by the timezone.');let[,n,r]=t;if(!n||!r)throw new Error('Invalid date and time format. Please provide the date and time in "YYYY-MM-DD HH:mm" or "YYYY-MM-DD HH:mm:ss" format, followed by the timezone.');try{let o=n.includes(":")&&n.split(":").length===3?"YYYY-MM-DD HH:mm:ss":"YYYY-MM-DD HH:mm",s=Mt.default.tz(n,o,r);if(!s.isValid())throw new Error("Invalid date and time values. Please ensure all components are correct.");return s.utc().toDate()}catch{throw new Error(`Unrecognized time zone ${r}. Please provide a valid time zone name, abbreviation, or offset. For example, "America/New_York", or "EST".`)}}var Tl=i=>(0,Mt.default)(i).utc().format("YYYY-MM-DD HH:mm:ss [UTC]");var cf=new Ye,hr=(i,e)=>{let t=i.type??{name:"string",isArray:!1},n=(u,c)=>{switch(u){case"class":return typeof c=="string";case"code":return typeof c=="string";case"string":return typeof c=="string";case"number":return typeof c=="number";case"boolean":return typeof c=="boolean";case"date":return c instanceof Date||typeof c=="string";case"datetime":return c instanceof Date||typeof c=="string";case"json":return typeof c=="object"||typeof c=="string";case"object":return typeof c=="object";default:return!1}},r=u=>!(!u||typeof u!="object"||!("mimeType"in u)||!("data"in u));if(i.type?.name==="image"){let u;if(Array.isArray(e)){for(let c of e)if(!r(c)){u="object ({ mimeType: string; data: string })";break}}else r(e)||(u="object ({ mimeType: string; data: string })");if(u)throw new Error(`Validation failed: Expected '${i.name}' to be type '${u}' instead got '${e}'`);return}let o=u=>!(!u||typeof u!="object"||!("data"in u));if(i.type?.name==="audio"){let u;if(Array.isArray(e)){for(let c of e)if(!o(c)){u="object ({ data: string; format?: string })";break}}else o(e)||(u="object ({ data: string; format?: string })");if(u)throw new Error(`Validation failed: Expected '${i.name}' to be type '${u}' instead got '${e}'`);return}let s=u=>{if(!u||typeof u!="object"||!("mimeType"in u))return!1;let c="data"in u,p="fileUri"in u;return!(!c&&!p||c&&p)};if(i.type?.name==="file"){let u;if(Array.isArray(e)){for(let c of e)if(!s(c)){u="object ({ mimeType: string; data: string } | { mimeType: string; fileUri: string })";break}}else s(e)||(u="object ({ mimeType: string; data: string } | { mimeType: string; fileUri: string })");if(u)throw new Error(`Validation failed: Expected '${i.name}' to be type '${u}' instead got '${e}'`);return}let a=u=>typeof u=="string"?!0:!(!u||typeof u!="object"||!("url"in u));if(i.type?.name==="url"){let u;if(Array.isArray(e)){for(let c of e)if(!a(c)){u="string or object ({ url: string; title?: string; description?: string })";break}}else a(e)||(u="string or object ({ url: string; title?: string; description?: string })");if(u)throw new Error(`Validation failed: Expected '${i.name}' to be type '${u}' instead got '${e}'`);return}let l=!0;if(t.isArray){if(!Array.isArray(e))l=!1;else for(let u of e)if(!n(t.name,u)){l=!1;break}}else l=n(t.name,e);if(!l){let u=Array.isArray(e)?"array":typeof e;throw new Error(`Validation failed: Expected '${i.name}' to be a ${i.type?.isArray?"an array of ":""}${t.name} instead got '${u}' (${JSON.stringify(e)})`)}};function fr(i){let e={};for(let t of i){let n=`${t.ai}:${t.model}`;if(!e[n]){e[n]={...t};continue}let r=e[n];if(r){let o=r.tokens??{promptTokens:0,completionTokens:0,totalTokens:0};o.promptTokens+=t?.tokens?.promptTokens??0,o.completionTokens+=t?.tokens?.completionTokens??0,o.totalTokens+=t?.tokens?.totalTokens??0,r.tokens=o;let s=r.citations??[],a=t.citations??[];if(a.length){let l=new Set(s.map(u=>u.url));for(let u of a)u?.url&&!l.has(u.url)&&(s.push(u),l.add(u.url));r.citations=s}}}return Object.values(e)}var wl=i=>{if(!i.trim())return[];let e=new Set(["-","*","+"]),t=/^\d+[\s]*[.)\]]\s*/,n=i.split(`
204
- `),r=[];for(let o of n){let s=o.trim();if(s){if(s[0]&&e.has(s[0]))r.push(s.slice(1).trim());else if(t.test(s))r.push(s.replace(t,"").trim());else if(r.length!==0)throw new Error("Could not parse markdown list: mixed content detected")}}if(r.length===0)throw new Error("Could not parse markdown list: no valid list items found");return r};function ai(i,e){let{index:t,delta:n,version:r}=e,o=i.find(a=>a.index===t)?.delta;if(!o)return i.push({index:t,delta:n,version:r}),i;for(let a of Object.keys(n)){let l=o[a],u=n[a];l===void 0&&Array.isArray(u)?o[a]=[...u]:Array.isArray(l)&&Array.isArray(u)?o[a]=[...l,...u]:(l===void 0||typeof l=="string")&&typeof u=="string"?o[a]=`${l??""}${u}`:o[a]=u}let s=i.find(a=>a.index===t);return s&&(s.version=r),i}var ii=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)}},Rc=new ii(500);function Rl(i,e,t=0,n=Rc){if(/^```[a-zA-Z]*\s*$/.test(i))return-4;if(/^[\s`]*$/.test(i))return-3;let r=i.indexOf(e,t);if(r!==-1)return r;let o=n.get(e)??Array.from({length:e.length},(a,l)=>e.slice(0,l+1));n.get(e)||n.set(e,o);let s=-1;for(let a=o.length-1;a>=0;a--){let l=o[a];if(i.endsWith(l)){s=a;break}}return s>=0?-2:-1}function Et(i,e){if(typeof i!="string")throw oi(e,String(i),"URL must be a string");try{new URL(i)}catch{throw oi(e,i,"Invalid URL format. Expected a valid URL like https://example.com")}}function at(i,e){if(typeof i!="string")return;let t=e.type;if(t){if(t.minLength!==void 0&&i.length<t.minLength)throw Cn(e,i,"minLength",t.minLength);if(t.maxLength!==void 0&&i.length>t.maxLength)throw Cn(e,i,"maxLength",t.maxLength);if(t.pattern!==void 0&&!new RegExp(t.pattern).test(i))throw Cn(e,i,"pattern",t.pattern);if(t.format==="email"&&!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(i))throw Cn(e,i,"format","valid email address");if(t.format==="uri"||t.format==="url")try{new URL(i)}catch{throw Cn(e,i,"format","valid URL")}}}function lt(i,e){if(typeof i!="number")return;let t=e.type;if(t){if(t.minimum!==void 0&&i<t.minimum)throw si(e,i,"minimum",t.minimum);if(t.maximum!==void 0&&i>t.maximum)throw si(e,i,"maximum",t.maximum)}}var Mo=(i,e,t,n)=>{let r=n?.strictMode??!1,o=n?.treatAllFieldsOptional??!1,s=n?.treatAllFieldsOptional??!1,a={extractedFields:[],streamedIndex:{},s:-1};ci(i,e,a,t,{strictMode:r,skipEarlyFail:s,treatAllFieldsOptional:o}),pi(i,e,a,t,{strictMode:r,treatAllFieldsOptional:o,forceFinalize:!0});for(let l of i.getOutputFields())l.isInternal&&delete e[l.name]},li=(i,e,t)=>{let n=[];for(let r of t)r&&!r.isOptional&&e[r.name]===void 0&&n.push(r);if(n.length>0)throw ul(n)},ci=(i,e,t,n,{strictMode:r,skipEarlyFail:o}={})=>{let s=i.getOutputFields(),a;for(;;){let l=new Set;t.currFieldIndex!==void 0&&!t.inAssumedField&&l.add(t.currFieldIndex);let u=s.map((g,h)=>({field:g,index:h})).filter(({index:g})=>!l.has(g)),c,p,d=-1,m=0;for(let{index:g,field:h}of u){let b=`${(t.extractedFields.length===0?"":`
205
- `)+h.title}:`,x=Rl(n,b,t.s);if(x===-2||x===-3)return!0;if(x===-4)return t.inBlock=!0,!0;x>=0&&(d===-1||x<d)&&(d=x,m=b.length,c=g,p=h)}if(d===-1){if(o)return;if(!r&&t.currField===void 0&&t.extractedFields.length===0&&s.length===1){t.inAssumedField=!0,t.currField=s[0],t.currFieldIndex=0,t.s=0,t.extractedFields.includes(s[0])||t.extractedFields.push(s[0]),t.streamedIndex[s[0].name]===void 0&&(t.streamedIndex[s[0].name]=0);return}if(r&&t.currField===void 0&&t.extractedFields.length===0){let g=s.find(h=>!h.isOptional);if(g)throw So(g)}break}if(a&&p&&a.name!==p.name)throw So(a);if(t.currField!==void 0&&t.inAssumedField&&(t.inAssumedField=!1,t.streamedIndex[t.currField.name]=0,t.currField=void 0),t.currField){let g=n.substring(t.s,d).trim(),h=ko(t.currField,g);h!==void 0&&(e[t.currField.name]=h),t.prevFields?t.prevFields?.push({field:t.currField,s:t.s,e:d}):t.prevFields=[{field:t.currField,s:t.s,e:d}]}t.s=d+m,p!==void 0&&c!==void 0&&(t.currField=p,t.currFieldIndex=c),p&&!t.extractedFields.includes(p)&&t.extractedFields.push(p),p&&t.streamedIndex[p.name]===void 0&&(t.streamedIndex[p.name]=0)}},pi=(i,e,t,n,r)=>{let o=r?.strictMode??!1,s=r?.treatAllFieldsOptional??!1,a=r?.deferRequiredCheckForStreaming??!1,l=r?.forceFinalize??!1;if(t.currField){let u=n.length,c=i.getOutputFields();for(let m of c){if(m.name===t.currField.name)continue;let g=`
206
- ${m.title}:`,h=n.indexOf(g,t.s);h!==-1&&h<u&&(u=h)}let p=n.substring(t.s,u).trim(),d=ko(t.currField,p);d!==void 0&&(e[t.currField.name]=d)}if(o&&!t.currField&&t.extractedFields.length===0&&n.trim()){let p=i.getOutputFields().find(d=>!d.isOptional);if(p)throw So(p)}if(Sc(i,e,n,t),!s){let u=t.currField!==void 0||(t.extractedFields?.length??0)>0;o||l?li(t,e,i.getOutputFields()):u||li(t,e,i.getOutputFields())}},Sc=(i,e,t,n)=>{let r=i.getOutputFields();if(r.length===1){let s=r[0];if(s){let a=`${s.title}:`,l=t.indexOf(a);if(l!==-1){let u=l+a.length,c=`
203
+ `,o=l}),(o>t||n.length===0)&&n.push(r.trim()),n},Tc=(i,e=.1)=>{let t=[...i].sort((r,o)=>r.score-o.score),n=Math.ceil(t.length*e);return t.slice(0,n)};var Mn=require("@opentelemetry/api");var dr=class{data=[];seenTags=new Set;addRequest(e,t){this.data.push(...e.map(n=>{let r=structuredClone(n);return{role:n.role,chat:[{index:t,value:r}]}}))}addFunctionResults(e){let t=e.map(({index:r,...o})=>({index:r,value:structuredClone(o)})),n=this.getLast();n?.role==="function"?n.chat.push(...t):this.data.push({role:"function",chat:t})}addResponse(e){let t=e.map(({index:n,...r})=>({index:n,value:structuredClone(r)}));this.data.push({role:"assistant",chat:t})}updateResult({content:e,name:t,functionCalls:n,thought:r,thoughtBlocks:o,index:s}){let a=this.data.at(-1);if(!a||a.role!=="assistant"||a.role==="assistant"&&!a.updatable){this.data.push({role:"assistant",updatable:!0,chat:[{index:s,value:structuredClone({content:e,name:t,functionCalls:n,thought:r,thoughtBlocks:o})}]});return}let l=a.chat.find(u=>u.index===s);if(!l){a.chat.push({index:s,value:structuredClone({content:e,name:t,functionCalls:n,thought:r,thoughtBlocks:o})});return}if(typeof e=="string"&&e.trim()!==""&&(l.value.content=e),typeof t=="string"&&t.trim()!==""&&(l.value.name=t),Array.isArray(n)&&n.length>0&&(l.value.functionCalls=n),typeof r=="string"&&r.trim()!==""){let u=l.value.thought;l.value.thought=typeof u=="string"?u+r:r}if(Array.isArray(o)&&o.length>0){let u=l.value.thoughtBlocks??[];for(let c of o){let p=u.length>0?u[u.length-1]:void 0;!c.signature&&c.data?p&&!p.signature?(p.data=(p.data??"")+c.data,c.encrypted&&(p.encrypted=!0)):u.push(structuredClone(c)):c.signature?p&&!p.signature?(p.data=(p.data??"")+c.data,p.signature=c.signature,c.encrypted&&(p.encrypted=!0)):u.push(structuredClone(c)):u.length===0&&u.push(structuredClone(c))}l.value.thoughtBlocks=u,u.length>0&&(l.value.thought=u.map(c=>c.data).join(""))}}addTag(e){let t=this.data.at(-1);t&&(t.tags||(t.tags=[]),t.tags.includes(e)||t.tags.push(e),this.seenTags.add(e))}rewindToTag(e){let t=this.data.findIndex(n=>n.tags?.includes(e));if(t===-1){if(!this.seenTags.has(e))throw new Error(`Tag "${e}" not found`);return[]}return this.data.splice(t)}removeByTag(e){let t=this.data.reduce((n,r,o)=>(r.tags?.includes(e)&&n.push(o),n),[]);return t.length===0?[]:t.reverse().map(n=>this.data.splice(n,1).at(0)).filter(n=>n!==void 0).reverse()}history(e){let t=[];for(let{role:n,chat:r}of this.data){let o;n==="function"?o=r.filter(s=>s.index===e).map(s=>s.value):o=r.find(s=>s.index===e)?.value,Array.isArray(o)&&o.length>0?t.push(...o.map(s=>({...s,role:n}))):typeof o=="object"&&o!==null&&t.push({...o,role:n})}return t}getLast(){return this.data.at(-1)}reset(){this.data=[],this.seenTags=new Set}},An=class{memories=new Map;defaultMemory;constructor(){this.defaultMemory=new dr}getMemory(e){return e?(this.memories.has(e)||this.memories.set(e,new dr),this.memories.get(e)):this.defaultMemory}addRequest(e,t){for(let n of e)Ht(n);this.getMemory(t).addRequest(e,0)}addResponse(e,t){qr(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 dr):this.defaultMemory.reset()}};function vt(i,e){if(!i&&!e)return;if(!i)return e;if(!e||i===e||i.aborted)return i;if(e.aborted)return e;if(typeof AbortSignal.any=="function")return AbortSignal.any([i,e]);let t=new AbortController,n=()=>{t.abort(i.aborted?i.reason:e.reason),r()},r=()=>{i.removeEventListener("abort",n),e.removeEventListener("abort",n)};return i.addEventListener("abort",n,{once:!0}),e.addEventListener("abort",n,{once:!0}),t.signal}var yt=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()}},yn=async(i,e)=>{for(let t of i){let{fn:n,message:r}=t,o=await n(e);if(o!==void 0){if(typeof o=="string")throw new yt({message:o});if(!o)throw r?new yt({message:r}):new Error("Assertion Failed: No message provided for assertion")}}},Zs=async(i,e,t,n=!1)=>{if(!e.currField||e.s===-1||!i||i.length===0)return;let r=i.filter(s=>s.fieldName===e.currField?.name);if(r.length===0)return;let o=t.substring(e.s);for(let s of r){let{message:a,fn:l}=s,u=await l(o,n);if(u!==void 0){if(typeof u=="string")throw new yt({message:u});if(!u&&a)throw new yt({message:a})}}};var ei={enabled:!0,enabledCategories:["generation","streaming","functions","errors","performance"],maxLabelLength:100,samplingRate:1},mr,ti=i=>{if(mr)return mr;let e=i??de.meter;if(e)return mr=wc(e),mr};var Za=()=>{let i=[];return de.meter||i.push("Global meter not initialized"),!mr&&de.meter&&i.push("Metrics instruments not created despite available meter"),{healthy:i.length===0,issues:i}},wc=i=>({generationLatencyHistogram:i.createHistogram("ax_gen_generation_duration_ms",{description:"End-to-end duration of AxGen generation requests",unit:"ms"}),generationRequestsCounter:i.createCounter("ax_gen_generation_requests_total",{description:"Total number of AxGen generation requests"}),generationErrorsCounter:i.createCounter("ax_gen_generation_errors_total",{description:"Total number of failed AxGen generations"}),multiStepGenerationsCounter:i.createCounter("ax_gen_multistep_generations_total",{description:"Total number of generations that required multiple steps"}),stepsPerGenerationHistogram:i.createHistogram("ax_gen_steps_per_generation",{description:"Number of steps taken per generation"}),maxStepsReachedCounter:i.createCounter("ax_gen_max_steps_reached_total",{description:"Total number of generations that hit max steps limit"}),validationErrorsCounter:i.createCounter("ax_gen_validation_errors_total",{description:"Total number of validation errors encountered"}),assertionErrorsCounter:i.createCounter("ax_gen_assertion_errors_total",{description:"Total number of assertion errors encountered"}),errorCorrectionAttemptsHistogram:i.createHistogram("ax_gen_error_correction_attempts",{description:"Number of error correction attempts per generation"}),errorCorrectionSuccessCounter:i.createCounter("ax_gen_error_correction_success_total",{description:"Total number of successful error corrections"}),errorCorrectionFailureCounter:i.createCounter("ax_gen_error_correction_failure_total",{description:"Total number of failed error corrections"}),maxRetriesReachedCounter:i.createCounter("ax_gen_max_retries_reached_total",{description:"Total number of generations that hit max retries limit"}),functionsEnabledGenerationsCounter:i.createCounter("ax_gen_functions_enabled_generations_total",{description:"Total number of generations with functions enabled"}),functionCallStepsCounter:i.createCounter("ax_gen_function_call_steps_total",{description:"Total number of steps that included function calls"}),functionsExecutedPerGenerationHistogram:i.createHistogram("ax_gen_functions_executed_per_generation",{description:"Number of unique functions executed per generation"}),functionErrorCorrectionCounter:i.createCounter("ax_gen_function_error_correction_total",{description:"Total number of function-related error corrections"}),fieldProcessorsExecutedCounter:i.createCounter("ax_gen_field_processors_executed_total",{description:"Total number of field processors executed"}),streamingFieldProcessorsExecutedCounter:i.createCounter("ax_gen_streaming_field_processors_executed_total",{description:"Total number of streaming field processors executed"}),streamingGenerationsCounter:i.createCounter("ax_gen_streaming_generations_total",{description:"Total number of streaming generations"}),streamingDeltasEmittedCounter:i.createCounter("ax_gen_streaming_deltas_emitted_total",{description:"Total number of streaming deltas emitted"}),streamingFinalizationLatencyHistogram:i.createHistogram("ax_gen_streaming_finalization_duration_ms",{description:"Duration of streaming response finalization",unit:"ms"}),samplesGeneratedHistogram:i.createHistogram("ax_gen_samples_generated",{description:"Number of samples generated per request"}),resultPickerUsageCounter:i.createCounter("ax_gen_result_picker_usage_total",{description:"Total number of times result picker was used"}),resultPickerLatencyHistogram:i.createHistogram("ax_gen_result_picker_duration_ms",{description:"Duration of result picker execution",unit:"ms"}),inputFieldsGauge:i.createGauge("ax_gen_input_fields",{description:"Number of input fields in signature"}),outputFieldsGauge:i.createGauge("ax_gen_output_fields",{description:"Number of output fields in signature"}),examplesUsedGauge:i.createGauge("ax_gen_examples_used",{description:"Number of examples used in generation"}),demosUsedGauge:i.createGauge("ax_gen_demos_used",{description:"Number of demos used in generation"}),promptRenderLatencyHistogram:i.createHistogram("ax_gen_prompt_render_duration_ms",{description:"Duration of prompt template rendering",unit:"ms"}),extractionLatencyHistogram:i.createHistogram("ax_gen_extraction_duration_ms",{description:"Duration of value extraction from responses",unit:"ms"}),assertionLatencyHistogram:i.createHistogram("ax_gen_assertion_duration_ms",{description:"Duration of assertion checking",unit:"ms"}),stateCreationLatencyHistogram:i.createHistogram("ax_gen_state_creation_duration_ms",{description:"Duration of state creation for multiple samples",unit:"ms"}),memoryUpdateLatencyHistogram:i.createHistogram("ax_gen_memory_update_duration_ms",{description:"Duration of memory updates during generation",unit:"ms"})}),To=ei,el=i=>{To={...To,...i}},tl=()=>({...To}),ht=i=>{let e={};for(let[t,n]of Object.entries(i))if(n!=null){let r=String(n),o=To.maxLabelLength;e[t]=r.length>o?r.substring(0,o):r}return e};var nl=(i,e,t,n,r,o,s)=>{try{let a=ht({success:t.toString(),...n?{signature:n}:{},...r?{ai_service:r}:{},...o?{model:o}:{},...s});i.generationLatencyHistogram&&i.generationLatencyHistogram.record(e,a),i.generationRequestsCounter&&i.generationRequestsCounter.add(1,a),!t&&i.generationErrorsCounter&&i.generationErrorsCounter.add(1,a)}catch(a){console.warn("Failed to record generation metric:",a)}},wo=(i,e,t,n,r)=>{try{let o=ht({...n?{signature:n}:{},...r});e>1&&i.multiStepGenerationsCounter&&i.multiStepGenerationsCounter.add(1,o),i.stepsPerGenerationHistogram&&i.stepsPerGenerationHistogram.record(e,o),e>=t&&i.maxStepsReachedCounter&&i.maxStepsReachedCounter.add(1,o)}catch(o){console.warn("Failed to record multi-step metric:",o)}},ni=(i,e,t,n)=>{try{let r=ht({error_type:e,...t?{signature:t}:{},...n});e==="validation"&&i.validationErrorsCounter&&i.validationErrorsCounter.add(1,r),e==="assertion"&&i.assertionErrorsCounter&&i.assertionErrorsCounter.add(1,r)}catch(r){console.warn("Failed to record validation error metric:",r)}},rl=(i,e,t)=>{try{let n=ht({error_type:"refusal",...e?{signature:e}:{},...t});i.validationErrorsCounter&&i.validationErrorsCounter.add(1,n)}catch(n){console.warn("Failed to record refusal error metric:",n)}},ri=(i,e,t,n,r,o)=>{try{let s=ht({success:t.toString(),...r?{signature:r}:{},...o});i.errorCorrectionAttemptsHistogram&&i.errorCorrectionAttemptsHistogram.record(e,s),t&&i.errorCorrectionSuccessCounter&&i.errorCorrectionSuccessCounter.add(1,s),t||(i.errorCorrectionFailureCounter&&i.errorCorrectionFailureCounter.add(1,s),e>=n&&i.maxRetriesReachedCounter&&i.maxRetriesReachedCounter.add(1,s))}catch(s){console.warn("Failed to record error correction metric:",s)}},ol=(i,e,t,n,r=!1,o,s)=>{try{let a=ht({functions_enabled:e.toString(),had_function_calls:n.toString(),...o?{signature:o}:{},...s});e&&i.functionsEnabledGenerationsCounter&&i.functionsEnabledGenerationsCounter.add(1,a),n&&i.functionCallStepsCounter&&i.functionCallStepsCounter.add(1,a),t>0&&i.functionsExecutedPerGenerationHistogram&&i.functionsExecutedPerGenerationHistogram.record(t,a),r&&i.functionErrorCorrectionCounter&&i.functionErrorCorrectionCounter.add(1,a)}catch(a){console.warn("Failed to record function calling metric:",a)}},sl=(i,e,t,n,r)=>{try{let o=ht({...n?{signature:n}:{},...r});e>0&&i.fieldProcessorsExecutedCounter&&i.fieldProcessorsExecutedCounter.add(e,o),t>0&&i.streamingFieldProcessorsExecutedCounter&&i.streamingFieldProcessorsExecutedCounter.add(t,o)}catch(o){console.warn("Failed to record field processing metric:",o)}},il=(i,e,t,n,r,o)=>{try{let s=ht({is_streaming:e.toString(),...r?{signature:r}:{},...o});e&&i.streamingGenerationsCounter&&i.streamingGenerationsCounter.add(1,s),t>0&&i.streamingDeltasEmittedCounter&&i.streamingDeltasEmittedCounter.add(t,s),n&&i.streamingFinalizationLatencyHistogram&&i.streamingFinalizationLatencyHistogram.record(n,s)}catch(s){console.warn("Failed to record streaming metric:",s)}},al=(i,e,t,n,r,o)=>{try{let s=ht({result_picker_used:t.toString(),...r?{signature:r}:{},...o});i.samplesGeneratedHistogram&&i.samplesGeneratedHistogram.record(e,s),t&&i.resultPickerUsageCounter&&i.resultPickerUsageCounter.add(1,s),n&&i.resultPickerLatencyHistogram&&i.resultPickerLatencyHistogram.record(n,s)}catch(s){console.warn("Failed to record samples metric:",s)}},ll=(i,e,t,n,r,o,s)=>{try{let a=ht({...o?{signature:o}:{},...s});i.inputFieldsGauge&&i.inputFieldsGauge.record(e,a),i.outputFieldsGauge&&i.outputFieldsGauge.record(t,a),i.examplesUsedGauge&&i.examplesUsedGauge.record(n,a),i.demosUsedGauge&&i.demosUsedGauge.record(r,a)}catch(a){console.warn("Failed to record signature complexity metrics:",a)}},Ro=(i,e,t,n,r)=>{try{let o=ht({metric_type:e,...n?{signature:n}:{},...r});switch(e){case"prompt_render":i.promptRenderLatencyHistogram&&i.promptRenderLatencyHistogram.record(t,o);break;case"extraction":i.extractionLatencyHistogram&&i.extractionLatencyHistogram.record(t,o);break;case"assertion":i.assertionLatencyHistogram&&i.assertionLatencyHistogram.record(t,o);break;case"state_creation":i.stateCreationLatencyHistogram&&i.stateCreationLatencyHistogram.record(t,o);break;case"memory_update":i.memoryUpdateLatencyHistogram&&i.memoryUpdateLatencyHistogram.record(t,o);break}}catch(o){console.warn("Failed to record performance metric:",o)}};var bn=i=>{let e=(()=>{switch(i?.name){case"string":return"string";case"number":return"number";case"boolean":return"boolean";case"date":return'date ("YYYY-MM-DD" format)';case"datetime":return'date time ("YYYY-MM-DD HH:mm Timezone" format)';case"json":return"JSON object";case"class":return"classification class";case"code":return"code";case"object":return"object";default:return"string"}})();return i?.isArray?`array of ${e}s`:e},Ue=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()}},ul=i=>{let t=i.map(n=>`'${n.title}' (${bn(n.type)})`).join(", ");return new Ue(`Required field not found: ${t}. Add a line starting with the exact label followed by a colon (e.g., "${i[0]?.title}:") and then provide a valid ${bn(i[0]?.type)} value. Keep the output concise and avoid unrelated text.`)},So=i=>new Ue(`Expected (Required) field not found: '${i.title}'. Begin a new section with "${i.title}:" and then provide a valid ${bn(i.type)} value directly after.`);var vo=i=>new Ue(`Required field is missing: '${i.title}'. After the "${i.title}:" label, provide a non-empty ${bn(i.type)}. Do not use null, undefined, or leave it blank.`),cl=(i,e)=>new Ue(`Invalid JSON: ${e} in field '${i.title}'. Return only valid JSON. Prefer a fenced code block containing a single JSON object or array with no trailing text.`),pl=(i,e)=>new Ue(`Invalid Array: ${e} for '${i.title}'. Provide a JSON array of ${bn(i.type)} items (e.g., [ ... ]). Markdown lists are also accepted if each item is on its own line starting with a hyphen.`),dl=(i,e,t)=>new Ue(`Field '${i.title}' has an invalid value '${e}': ${t}. Provide a ${bn(i.type)}. Ensure formatting exactly matches the expected type.`),ml=(i,e,t)=>new Ue(`Invalid date for '${i.title}': ${t}. Use the exact format YYYY-MM-DD (e.g., 2024-05-09). You provided: ${e}.`),gl=(i,e,t)=>new Ue(`Invalid date/time for '${i.title}': ${t}. Use the format YYYY-MM-DD HH:mm or YYYY-MM-DD HH:mm:ss followed by a valid timezone (e.g., America/New_York). You provided: ${e}.`),oi=(i,e,t)=>new Ue(`Invalid URL for '${i.title}': ${t}. Use a valid URL format (e.g., https://example.com). You provided: ${e}.`),In=(i,e,t,n)=>{let r=`Field '${i.title}' failed validation: `;return t==="minLength"?r+=`String must be at least ${n} characters long. You provided: "${e}" (${e.length} characters).`:t==="maxLength"?r+=`String must be at most ${n} characters long. You provided: "${e}" (${e.length} characters).`:t==="pattern"?r+=`String must match pattern /${n}/. You provided: "${e}".`:t==="format"&&(r+=`String must be a ${n}. You provided: "${e}".`),new Ue(r)},si=(i,e,t,n)=>{let r=`Field '${i.title}' failed validation: `;return t==="minimum"?r+=`Number must be at least ${n}. You provided: ${e}.`:t==="maximum"&&(r+=`Number must be at most ${n}. You provided: ${e}.`),new Ue(r)};var hl=({error:i,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:s,customLabels:a})=>{let l=i.getFixingInstructions();if(t&&n){let u=l?.map(c=>c.title).join(", ")??"";Zi(i,e,u,n)}return r&&ni(r,"validation",o,a),s&&s.addEvent("validation.error",{message:i.toString(),fixing_instructions:l?.map(u=>u.title).join(", ")??""}),l},fl=({error:i,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:s,customLabels:a})=>{let l=i.getFixingInstructions();if(t&&n){let u=l?.map(c=>c.title).join(", ")??"";ea(i,e,u,n)}return r&&ni(r,"assertion",o,a),s&&s.addEvent("assertion.error",{message:i.toString(),fixing_instructions:l?.map(u=>u.title).join(", ")??""}),l},xl=({error:i,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:s,customLabels:a})=>{t&&n&&ta(i,e,n),r&&rl(r,o,a),s&&s.addEvent("refusal.error",{message:i.toString()})};var Mt=Hn(require("dayjs"),1),Al=Hn(require("dayjs/plugin/customParseFormat.js"),1),yl=Hn(require("dayjs/plugin/timezone.js"),1),bl=Hn(require("dayjs/plugin/utc.js"),1);Mt.default.extend(bl.default);Mt.default.extend(yl.default);Mt.default.extend(Al.default);function Il(i,e,t=!1){try{return Rc(e)}catch(n){if(i.isOptional&&!t)return;let r=n.message;throw ml(i,e,r)}}function Rc(i){if(!(0,Mt.default)(i,"YYYY-MM-DD",!0).isValid())throw new Error('Invalid date format. Please provide the date in "YYYY-MM-DD" format.');return Mt.default.utc(i,"YYYY-MM-DD").startOf("day").toDate()}function Cl(i,e,t=!1){try{return Sc(e)}catch(n){if(i.isOptional&&!t)return;let r=n.message;throw gl(i,e,r)}}function Sc(i){let e=/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}(?::\d{2})?) (.+)$/,t=i.match(e);if(!t)throw new Error('Invalid date and time format. Please provide the date and time in "YYYY-MM-DD HH:mm" or "YYYY-MM-DD HH:mm:ss" format, followed by the timezone.');let[,n,r]=t;if(!n||!r)throw new Error('Invalid date and time format. Please provide the date and time in "YYYY-MM-DD HH:mm" or "YYYY-MM-DD HH:mm:ss" format, followed by the timezone.');try{let o=n.includes(":")&&n.split(":").length===3?"YYYY-MM-DD HH:mm:ss":"YYYY-MM-DD HH:mm",s=Mt.default.tz(n,o,r);if(!s.isValid())throw new Error("Invalid date and time values. Please ensure all components are correct.");return s.utc().toDate()}catch{throw new Error(`Unrecognized time zone ${r}. Please provide a valid time zone name, abbreviation, or offset. For example, "America/New_York", or "EST".`)}}var Tl=i=>(0,Mt.default)(i).utc().format("YYYY-MM-DD HH:mm:ss [UTC]");var cf=new Je,gr=(i,e)=>{let t=i.type??{name:"string",isArray:!1},n=(u,c)=>{switch(u){case"class":return typeof c=="string";case"code":return typeof c=="string";case"string":return typeof c=="string";case"number":return typeof c=="number";case"boolean":return typeof c=="boolean";case"date":return c instanceof Date||typeof c=="string";case"datetime":return c instanceof Date||typeof c=="string";case"json":return typeof c=="object"||typeof c=="string";case"object":return typeof c=="object";default:return!1}},r=u=>!(!u||typeof u!="object"||!("mimeType"in u)||!("data"in u));if(i.type?.name==="image"){let u;if(Array.isArray(e)){for(let c of e)if(!r(c)){u="object ({ mimeType: string; data: string })";break}}else r(e)||(u="object ({ mimeType: string; data: string })");if(u)throw new Error(`Validation failed: Expected '${i.name}' to be type '${u}' instead got '${e}'`);return}let o=u=>!(!u||typeof u!="object"||!("data"in u));if(i.type?.name==="audio"){let u;if(Array.isArray(e)){for(let c of e)if(!o(c)){u="object ({ data: string; format?: string })";break}}else o(e)||(u="object ({ data: string; format?: string })");if(u)throw new Error(`Validation failed: Expected '${i.name}' to be type '${u}' instead got '${e}'`);return}let s=u=>{if(!u||typeof u!="object"||!("mimeType"in u))return!1;let c="data"in u,p="fileUri"in u;return!(!c&&!p||c&&p)};if(i.type?.name==="file"){let u;if(Array.isArray(e)){for(let c of e)if(!s(c)){u="object ({ mimeType: string; data: string } | { mimeType: string; fileUri: string })";break}}else s(e)||(u="object ({ mimeType: string; data: string } | { mimeType: string; fileUri: string })");if(u)throw new Error(`Validation failed: Expected '${i.name}' to be type '${u}' instead got '${e}'`);return}let a=u=>typeof u=="string"?!0:!(!u||typeof u!="object"||!("url"in u));if(i.type?.name==="url"){let u;if(Array.isArray(e)){for(let c of e)if(!a(c)){u="string or object ({ url: string; title?: string; description?: string })";break}}else a(e)||(u="string or object ({ url: string; title?: string; description?: string })");if(u)throw new Error(`Validation failed: Expected '${i.name}' to be type '${u}' instead got '${e}'`);return}let l=!0;if(t.isArray){if(!Array.isArray(e))l=!1;else for(let u of e)if(!n(t.name,u)){l=!1;break}}else l=n(t.name,e);if(!l){let u=Array.isArray(e)?"array":typeof e;throw new Error(`Validation failed: Expected '${i.name}' to be a ${i.type?.isArray?"an array of ":""}${t.name} instead got '${u}' (${JSON.stringify(e)})`)}};function hr(i){let e={};for(let t of i){let n=`${t.ai}:${t.model}`;if(!e[n]){e[n]={...t};continue}let r=e[n];if(r){let o=r.tokens??{promptTokens:0,completionTokens:0,totalTokens:0};o.promptTokens+=t?.tokens?.promptTokens??0,o.completionTokens+=t?.tokens?.completionTokens??0,o.totalTokens+=t?.tokens?.totalTokens??0,r.tokens=o;let s=r.citations??[],a=t.citations??[];if(a.length){let l=new Set(s.map(u=>u.url));for(let u of a)u?.url&&!l.has(u.url)&&(s.push(u),l.add(u.url));r.citations=s}}}return Object.values(e)}var wl=i=>{if(!i.trim())return[];let e=new Set(["-","*","+"]),t=/^\d+[\s]*[.)\]]\s*/,n=i.split(`
204
+ `),r=[];for(let o of n){let s=o.trim();if(s){if(s[0]&&e.has(s[0]))r.push(s.slice(1).trim());else if(t.test(s))r.push(s.replace(t,"").trim());else if(r.length!==0)throw new Error("Could not parse markdown list: mixed content detected")}}if(r.length===0)throw new Error("Could not parse markdown list: no valid list items found");return r};function ai(i,e){let{index:t,delta:n,version:r}=e,o=i.find(a=>a.index===t)?.delta;if(!o)return i.push({index:t,delta:n,version:r}),i;for(let a of Object.keys(n)){let l=o[a],u=n[a];l===void 0&&Array.isArray(u)?o[a]=[...u]:Array.isArray(l)&&Array.isArray(u)?o[a]=[...l,...u]:(l===void 0||typeof l=="string")&&typeof u=="string"?o[a]=`${l??""}${u}`:o[a]=u}let s=i.find(a=>a.index===t);return s&&(s.version=r),i}var ii=class{cache=new Map;maxSize;constructor(e){this.maxSize=e}get(e){let t=this.cache.get(e);return t&&(this.cache.delete(e),this.cache.set(e,t)),t}set(e,t){if(this.cache.has(e))this.cache.delete(e);else if(this.cache.size>=this.maxSize){let n=this.cache.keys().next().value;n&&this.cache.delete(n)}this.cache.set(e,t)}},vc=new ii(500);function Rl(i,e,t=0,n=vc){if(/^```[a-zA-Z]*\s*$/.test(i))return-4;if(/^[\s`]*$/.test(i))return-3;let r=i.indexOf(e,t);if(r!==-1)return r;let o=n.get(e)??Array.from({length:e.length},(a,l)=>e.slice(0,l+1));n.get(e)||n.set(e,o);let s=-1;for(let a=o.length-1;a>=0;a--){let l=o[a];if(i.endsWith(l)){s=a;break}}return s>=0?-2:-1}function Et(i,e){if(typeof i!="string")throw oi(e,String(i),"URL must be a string");try{new URL(i)}catch{throw oi(e,i,"Invalid URL format. Expected a valid URL like https://example.com")}}function it(i,e){if(typeof i!="string")return;let t=e.type;if(t){if(t.minLength!==void 0&&i.length<t.minLength)throw In(e,i,"minLength",t.minLength);if(t.maxLength!==void 0&&i.length>t.maxLength)throw In(e,i,"maxLength",t.maxLength);if(t.pattern!==void 0&&!new RegExp(t.pattern).test(i))throw In(e,i,"pattern",t.pattern);if(t.format==="email"&&!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(i))throw In(e,i,"format","valid email address");if(t.format==="uri"||t.format==="url")try{new URL(i)}catch{throw In(e,i,"format","valid URL")}}}function at(i,e){if(typeof i!="number")return;let t=e.type;if(t){if(t.minimum!==void 0&&i<t.minimum)throw si(e,i,"minimum",t.minimum);if(t.maximum!==void 0&&i>t.maximum)throw si(e,i,"maximum",t.maximum)}}var Mo=(i,e,t,n)=>{let r=n?.strictMode??!1,o=n?.treatAllFieldsOptional??!1,s=n?.treatAllFieldsOptional??!1,a={extractedFields:[],streamedIndex:{},s:-1};ci(i,e,a,t,{strictMode:r,skipEarlyFail:s,treatAllFieldsOptional:o}),pi(i,e,a,t,{strictMode:r,treatAllFieldsOptional:o,forceFinalize:!0});for(let l of i.getOutputFields())l.isInternal&&delete e[l.name]},li=(i,e,t)=>{let n=[];for(let r of t)r&&!r.isOptional&&e[r.name]===void 0&&n.push(r);if(n.length>0)throw ul(n)},ci=(i,e,t,n,{strictMode:r,skipEarlyFail:o}={})=>{let s=i.getOutputFields(),a;for(;;){let l=new Set;t.currFieldIndex!==void 0&&!t.inAssumedField&&l.add(t.currFieldIndex);let u=s.map((g,h)=>({field:g,index:h})).filter(({index:g})=>!l.has(g)),c,p,d=-1,m=0;for(let{index:g,field:h}of u){let I=`${(t.extractedFields.length===0?"":`
205
+ `)+h.title}:`,x=Rl(n,I,t.s);if(x===-2||x===-3)return!0;if(x===-4)return t.inBlock=!0,!0;x>=0&&(d===-1||x<d)&&(d=x,m=I.length,c=g,p=h)}if(d===-1){if(o)return;if(!r&&t.currField===void 0&&t.extractedFields.length===0&&s.length===1){t.inAssumedField=!0,t.currField=s[0],t.currFieldIndex=0,t.s=0,t.extractedFields.includes(s[0])||t.extractedFields.push(s[0]),t.streamedIndex[s[0].name]===void 0&&(t.streamedIndex[s[0].name]=0);return}if(r&&t.currField===void 0&&t.extractedFields.length===0){let g=s.find(h=>!h.isOptional);if(g)throw So(g)}break}if(a&&p&&a.name!==p.name)throw So(a);if(t.currField!==void 0&&t.inAssumedField&&(t.inAssumedField=!1,t.streamedIndex[t.currField.name]=0,t.currField=void 0),t.currField){let g=n.substring(t.s,d).trim(),h=ko(t.currField,g);h!==void 0&&(e[t.currField.name]=h),t.prevFields?t.prevFields?.push({field:t.currField,s:t.s,e:d}):t.prevFields=[{field:t.currField,s:t.s,e:d}]}t.s=d+m,p!==void 0&&c!==void 0&&(t.currField=p,t.currFieldIndex=c),p&&!t.extractedFields.includes(p)&&t.extractedFields.push(p),p&&t.streamedIndex[p.name]===void 0&&(t.streamedIndex[p.name]=0)}},pi=(i,e,t,n,r)=>{let o=r?.strictMode??!1,s=r?.treatAllFieldsOptional??!1,a=r?.deferRequiredCheckForStreaming??!1,l=r?.forceFinalize??!1;if(t.currField){let u=n.length,c=i.getOutputFields();for(let m of c){if(m.name===t.currField.name)continue;let g=`
206
+ ${m.title}:`,h=n.indexOf(g,t.s);h!==-1&&h<u&&(u=h)}let p=n.substring(t.s,u).trim(),d=ko(t.currField,p);d!==void 0&&(e[t.currField.name]=d)}if(o&&!t.currField&&t.extractedFields.length===0&&n.trim()){let p=i.getOutputFields().find(d=>!d.isOptional);if(p)throw So(p)}if(kc(i,e,n,t),!s){let u=t.currField!==void 0||(t.extractedFields?.length??0)>0;o||l?li(t,e,i.getOutputFields()):u||li(t,e,i.getOutputFields())}},kc=(i,e,t,n)=>{let r=i.getOutputFields();if(r.length===1){let s=r[0];if(s){let a=`${s.title}:`,l=t.indexOf(a);if(l!==-1){let u=l+a.length,c=`
207
207
  ${s.title}:`,p=t.indexOf(c,u),d=t.substring(u,p===-1?t.length:p).trim();if(d)try{let m=ko(s,d);if(m!==void 0){e[s.name]=m;return}}catch{}}}}let o=t.split(`
208
- `);for(let s of r){if(s.name in e)continue;let a=`${s.title}:`;for(let l of o){let u=l.trim();if(u.startsWith(a)){let c=u.substring(a.length).trim();if(c)try{let p=ko(s,c);if(p!==void 0){e[s.name]=p;break}}catch(p){if(!s.isOptional)throw p}break}}}},Sl=(i,e,t=!1)=>{switch(i.type?.name){case"code":return ui(e);case"string":return e;case"number":{let n=Number(e);if(Number.isNaN(n)){if(i.isOptional&&!t)return;throw new Error("Invalid number")}return n}case"boolean":{if(typeof e=="boolean")return e;let n=e.toLowerCase();if(n==="true")return!0;if(n==="false")return!1;if(i.isOptional&&!t)return;throw new Error("Invalid boolean")}case"date":return Il(i,e,t);case"datetime":return Cl(i,e,t);case"class":{let n=e;if(i.type.options&&!i.type.options.includes(n)){if(i.isOptional)return;throw new Error(`Invalid class '${e}', expected one of the following: ${i.type.options.join(", ")}`)}return n}default:return e}};function*vl(i,e,t,n,r,o){let{name:s,isInternal:a}=e,{isArray:l,name:u}=e.type??{};if(a||l||u&&u!=="string"&&u!=="code")return;let c=r.streamedIndex[s]??0,p=c===0,d=(t<0?0:t)+c,m=i.substring(d,n);if(m.length===0)return;let g=m.replace(/\s+$/,"");r.currField?.type?.name==="code"&&(g=g.replace(/\s*```\s*$/,""));let h=p?g.trimStart():g;r.currField?.type?.name==="code"&&(h=h.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/,"")),h.length>0&&(yield{index:o,delta:{[s]:h}},r.streamedIndex[s]=c+g.length)}function*di(i,e,t,n,r){for(let s of n.prevFields??[]){let{field:a,s:l,e:u}=s;yield*vl(e,a,l,u,n,r)}if(n.prevFields=void 0,n.inAssumedField&&!(i.getOutputFields().filter(l=>!l.isInternal).length===1)||!n.currField||n.currField.isInternal)return;yield*vl(e,n.currField,n.s,e.length,n,r);let o=i.getOutputFields();for(let s of Object.keys(t)){let a=o.find(c=>c.name===s);if(!a||a.isInternal)continue;let l=t[s];if(Array.isArray(l)){let c=n.streamedIndex?.[s]??0,p=l.slice(c);p&&p.length>0&&(yield{index:r,delta:{[s]:p}},n.streamedIndex[s]=c+p.length);continue}let u=typeof l=="string"?l:void 0;if(!n.streamedIndex[s])yield{index:r,delta:{[s]:l}},n.streamedIndex[s]=u?u.length:1;else if(u){let c=n.streamedIndex[s];if(u.length>c){let p=u.substring(c);yield{index:r,delta:{[s]:p}},n.streamedIndex[s]=u.length}}}}function ko(i,e){if(!e||e===""||/^(null|undefined)\s*$/i.test(e)){if(i.isOptional)return;throw vo(i)}let t;if(i.type?.name==="json"&&!i.type?.isArray)try{let r=ui(e);return t=JSON.parse(r),t}catch(r){throw cl(i,r.message)}if(i.type?.isArray)try{try{t=JSON.parse(e)}catch{t=wl(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 pl(i,o)}try{if(Array.isArray(t)){for(let[r,o]of t.entries())if(o!==void 0){let s=typeof o=="string"?o.trim():o;if(typeof s=="string"&&(i.type?.name==="object"||i.type?.name==="json"))try{let a=ui(s);s=JSON.parse(a)}catch{}t[r]=Sl(i,s,!0)}}else t=Sl(i,e)}catch(r){throw dl(i,e,r.message)}if(typeof t=="string"&&t==="")return;let n=i.type;if(n&&t!==void 0&&(n.name==="url"&&Et(t,i),(n.name==="string"||n.name==="code")&&at(t,i),n.name==="number"&&lt(t,i),n.isArray&&Array.isArray(t)))for(let r of t)r!==void 0&&(n.name==="string"||n.name==="code"?at(r,i):n.name==="number"&&lt(r,i));return t}function Tn(i,e,t){let n=i.getOutputFields();for(let r of n){let o=e[r.name];if(o==null){if(!r.isOptional&&!t?.allowMissingRequired)throw vo(r);continue}let s=r.type;if(s){if(s.name==="url"&&Et(o,r),(s.name==="string"||s.name==="code")&&at(o,r),s.name==="number"&&lt(o,r),s.isArray&&Array.isArray(o))for(let a of o)a!=null&&(s.name==="url"?Et(a,r):s.name==="string"||s.name==="code"?at(a,r):s.name==="number"&&lt(a,r));if(s.name==="object"&&s.fields&&typeof o=="object"&&!Array.isArray(o)&&Oo(r,o,t),s.isArray&&s.fields&&Array.isArray(o)&&s.name==="object")for(let a of o)a&&typeof a=="object"&&Oo(r,a,t)}}}function Oo(i,e,t){let n=i.type?.fields;if(!(!n||typeof n!="object"))for(let[r,o]of Object.entries(n)){let s={name:r,title:r,description:o.description,type:{name:o.type,isArray:o.isArray,options:o.options,fields:o.fields,minLength:o.minLength,maxLength:o.maxLength,minimum:o.minimum,maximum:o.maximum,pattern:o.pattern,patternDescription:o.patternDescription,format:o.format},isOptional:o.isOptional??!1,isInternal:o.isInternal??!1},a=e[s.name];if(a==null){if(!s.isOptional&&!t?.allowMissingRequired)throw vo(s);continue}let l=s.type;if(l){if(l.name==="url"&&Et(a,s),(l.name==="string"||l.name==="code")&&at(a,s),l.name==="number"&&lt(a,s),l.isArray&&Array.isArray(a))for(let u of a)u!=null&&(l.name==="url"?Et(u,s):l.name==="string"||l.name==="code"?at(u,s):l.name==="number"&&lt(u,s));if(l.name==="object"&&l.fields&&typeof a=="object"&&!Array.isArray(a)&&Oo(s,a,t),l.isArray&&l.fields&&Array.isArray(a)&&l.name==="object")for(let u of a)u&&typeof u=="object"&&Oo(s,u,t)}}}var ui=i=>{let t=/```([A-Za-z]*)\s*([\s\S]*?)\s*```/g.exec(i);return t?t.length===3?t[2]:t.length===2?t[1]:i:i};async function xr(i,e,t,n){for(let r of i){if(e[r.field.name]===void 0)continue;let o=r.process,s=await o(e[r.field.name],{sessionId:n,values:e,done:!0});kl(r.field,t,s,n)}}async function mi(i,e,t,n,r,o,s=!1){for(let a of i){if(t.currField?.name!==a.field.name)continue;let l=e.substring(t.s);t.currField?.type?.name==="code"&&(l=l.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/,""),l=l.replace(/\s*```\s*$/,""));let u=a.process,c=await u(l,{sessionId:o,values:r,done:s});kl(t.currField,n,c,o)}}var kl=(i,e,t,n)=>{if(t===void 0||typeof t=="string"&&(t===""||/^(null|undefined)\s*$/i.test(t)))return;let r=String(t),o=r;e.addRequest([{role:"user",content:[{type:"text",text:o}]}],n),e.addTag("processor",n)};function Ol(i,e){if(!e)return i;let t=[];if(e.format==="email"&&t.push("Must be a valid email address format"),(e.format==="uri"||e.format==="url"||e.name==="url")&&t.push("Must be a valid URL format"),(e.name==="string"||e.name==="code"||e.name==="url"||e.name==="date"||e.name==="datetime")&&(e.minLength!==void 0&&e.maxLength!==void 0?t.push(`Minimum length: ${e.minLength} characters, maximum length: ${e.maxLength} characters`):e.minLength!==void 0?t.push(`Minimum length: ${e.minLength} characters`):e.maxLength!==void 0&&t.push(`Maximum length: ${e.maxLength} characters`)),e.name==="number"&&(e.minimum!==void 0&&e.maximum!==void 0?t.push(`Minimum value: ${e.minimum}, maximum value: ${e.maximum}`):e.minimum!==void 0?t.push(`Minimum value: ${e.minimum}`):e.maximum!==void 0&&t.push(`Maximum value: ${e.maximum}`)),e.pattern!==void 0){if(!e.patternDescription)throw new Error(`Field with pattern '${e.pattern}' must include a patternDescription to explain the pattern to the LLM`);t.push(e.patternDescription)}if(e.name==="date"&&t.push("Format: YYYY-MM-DD"),e.name==="datetime"&&t.push("Format: ISO 8601 date-time"),t.length===0)return i;let n=t.join(". ");return!i||i.trim().length===0?n:`${i.trim().endsWith(".")?i.trim():`${i.trim()}.`} ${n}`}function wn(i,e="Schema"){if("name"in i&&"type"in i)return Eo(i);let t={},n=[];for(let r of i){if(r.isInternal)continue;let o=Eo(r);t[r.name]=o,r.isOptional||n.push(r.name)}return{type:"object",title:e,properties:t,required:n,additionalProperties:!1}}function Eo(i,e=!1){let t=i.type,n=Ol(i.description,t);if(e&&t?.name&&(t.name==="image"||t.name==="audio"||t.name==="file"))throw new Error(`Media type '${t.name}' is not allowed in nested object fields. Media types (image, audio, file) can only be used as top-level input fields. Field: ${i.name}`);let r={};if(n&&(r.description=n),t?.isArray)if(r.type="array",t.fields){r.items={type:"object",properties:{},required:[],additionalProperties:!1},t.description&&(r.items.description=t.description);for(let[o,s]of Object.entries(t.fields)){let a={name:o,description:s.description,type:{name:s.type,isArray:s.isArray,options:s.options?[...s.options]:void 0,fields:s.fields,minLength:s.minLength,maxLength:s.maxLength,minimum:s.minimum,maximum:s.maximum,pattern:s.pattern,patternDescription:s.patternDescription,format:s.format},isOptional:s.isOptional,isInternal:s.isInternal};r.items.properties[o]=Eo(a,!0),s.isOptional||r.items.required.push(o)}}else if(t.name==="class"&&t.options)r.items={type:"string",enum:t.options};else{let o=Ol(t.description||i.description,t);r.items={type:Ml(t.name)},o&&(r.items.description=o),t.name==="string"||t.name==="code"||t.name==="url"||t.name==="date"||t.name==="datetime"?(t.minLength!==void 0&&(r.items.minLength=t.minLength),t.maxLength!==void 0&&(r.items.maxLength=t.maxLength),t.pattern!==void 0&&(r.items.pattern=t.pattern),t.format!==void 0&&(r.items.format=t.format)):t.name==="number"&&(t.minimum!==void 0&&(r.items.minimum=t.minimum),t.maximum!==void 0&&(r.items.maximum=t.maximum))}else if(t?.name==="object"&&t.fields){r.type="object",r.properties={},r.required=[],r.additionalProperties=!1;for(let[o,s]of Object.entries(t.fields)){let a={name:o,description:s.description,type:{name:s.type,isArray:s.isArray,options:s.options?[...s.options]:void 0,fields:s.fields,minLength:s.minLength,maxLength:s.maxLength,minimum:s.minimum,maximum:s.maximum,pattern:s.pattern,patternDescription:s.patternDescription,format:s.format},isOptional:s.isOptional,isInternal:s.isInternal};r.properties[o]=Eo(a,!0),s.isOptional||r.required.push(o)}}else t?.name==="class"&&t.options?(r.type="string",r.enum=t.options):(r.type=Ml(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 Ml(i){switch(i){case"string":case"code":case"url":case"date":case"datetime":case"image":case"audio":case"file":return"string";case"number":return"number";case"boolean":return"boolean";case"json":case"object":return["object","array","string","number","boolean","null"];default:return"string"}}function Po(i){if(!i||typeof i!="object")throw new Error("Schema must be an object");if(i.type==="array"){if(!i.items)throw new Error('Array schema is missing an "items" definition (required by JSON Schema and all LLM providers for function tools)');Po(i.items)}else if(i.type==="object"&&i.properties)for(let e of Object.values(i.properties))Po(e)}var $t=class extends Error{calls;constructor(e){super(`Stop function executed: ${e.map(t=>t.func.name).join(", ")}`),this.name="AxStopFunctionCallException",this.calls=e}},Ar=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()}},yr=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}'
208
+ `);for(let s of r){if(s.name in e)continue;let a=`${s.title}:`;for(let l of o){let u=l.trim();if(u.startsWith(a)){let c=u.substring(a.length).trim();if(c)try{let p=ko(s,c);if(p!==void 0){e[s.name]=p;break}}catch(p){if(!s.isOptional)throw p}break}}}},Sl=(i,e,t=!1)=>{switch(i.type?.name){case"code":return ui(e);case"string":return e;case"number":{let n=Number(e);if(Number.isNaN(n)){if(i.isOptional&&!t)return;throw new Error("Invalid number")}return n}case"boolean":{if(typeof e=="boolean")return e;let n=e.toLowerCase();if(n==="true")return!0;if(n==="false")return!1;if(i.isOptional&&!t)return;throw new Error("Invalid boolean")}case"date":return Il(i,e,t);case"datetime":return Cl(i,e,t);case"class":{let n=e;if(i.type.options&&!i.type.options.includes(n)){if(i.isOptional)return;throw new Error(`Invalid class '${e}', expected one of the following: ${i.type.options.join(", ")}`)}return n}default:return e}};function*vl(i,e,t,n,r,o){let{name:s,isInternal:a}=e,{isArray:l,name:u}=e.type??{};if(a||l||u&&u!=="string"&&u!=="code")return;let c=r.streamedIndex[s]??0,p=c===0,d=(t<0?0:t)+c,m=i.substring(d,n);if(m.length===0)return;let g=m.replace(/\s+$/,"");r.currField?.type?.name==="code"&&(g=g.replace(/\s*```\s*$/,""));let h=p?g.trimStart():g;r.currField?.type?.name==="code"&&(h=h.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/,"")),h.length>0&&(yield{index:o,delta:{[s]:h}},r.streamedIndex[s]=c+g.length)}function*di(i,e,t,n,r){for(let s of n.prevFields??[]){let{field:a,s:l,e:u}=s;yield*vl(e,a,l,u,n,r)}if(n.prevFields=void 0,n.inAssumedField&&!(i.getOutputFields().filter(l=>!l.isInternal).length===1)||!n.currField||n.currField.isInternal)return;yield*vl(e,n.currField,n.s,e.length,n,r);let o=i.getOutputFields();for(let s of Object.keys(t)){let a=o.find(c=>c.name===s);if(!a||a.isInternal)continue;let l=t[s];if(Array.isArray(l)){let c=n.streamedIndex?.[s]??0,p=l.slice(c);p&&p.length>0&&(yield{index:r,delta:{[s]:p}},n.streamedIndex[s]=c+p.length);continue}let u=typeof l=="string"?l:void 0;if(!n.streamedIndex[s])yield{index:r,delta:{[s]:l}},n.streamedIndex[s]=u?u.length:1;else if(u){let c=n.streamedIndex[s];if(u.length>c){let p=u.substring(c);yield{index:r,delta:{[s]:p}},n.streamedIndex[s]=u.length}}}}function ko(i,e){if(!e||e===""||/^(null|undefined)\s*$/i.test(e)){if(i.isOptional)return;throw vo(i)}let t;if(i.type?.name==="json"&&!i.type?.isArray)try{let r=ui(e);return t=JSON.parse(r),t}catch(r){throw cl(i,r.message)}if(i.type?.isArray)try{try{t=JSON.parse(e)}catch{t=wl(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 pl(i,o)}try{if(Array.isArray(t)){for(let[r,o]of t.entries())if(o!==void 0){let s=typeof o=="string"?o.trim():o;if(typeof s=="string"&&(i.type?.name==="object"||i.type?.name==="json"))try{let a=ui(s);s=JSON.parse(a)}catch{}t[r]=Sl(i,s,!0)}}else t=Sl(i,e)}catch(r){throw dl(i,e,r.message)}if(typeof t=="string"&&t==="")return;let n=i.type;if(n&&t!==void 0&&(n.name==="url"&&Et(t,i),(n.name==="string"||n.name==="code")&&it(t,i),n.name==="number"&&at(t,i),n.isArray&&Array.isArray(t)))for(let r of t)r!==void 0&&(n.name==="string"||n.name==="code"?it(r,i):n.name==="number"&&at(r,i));return t}function Cn(i,e,t){let n=i.getOutputFields();for(let r of n){let o=e[r.name];if(o==null){if(!r.isOptional&&!t?.allowMissingRequired)throw vo(r);continue}let s=r.type;if(s){if(s.name==="url"&&Et(o,r),(s.name==="string"||s.name==="code")&&it(o,r),s.name==="number"&&at(o,r),s.isArray&&Array.isArray(o))for(let a of o)a!=null&&(s.name==="url"?Et(a,r):s.name==="string"||s.name==="code"?it(a,r):s.name==="number"&&at(a,r));if(s.name==="object"&&s.fields&&typeof o=="object"&&!Array.isArray(o)&&Oo(r,o,t),s.isArray&&s.fields&&Array.isArray(o)&&s.name==="object")for(let a of o)a&&typeof a=="object"&&Oo(r,a,t)}}}function Oo(i,e,t){let n=i.type?.fields;if(!(!n||typeof n!="object"))for(let[r,o]of Object.entries(n)){let s={name:r,title:r,description:o.description,type:{name:o.type,isArray:o.isArray,options:o.options,fields:o.fields,minLength:o.minLength,maxLength:o.maxLength,minimum:o.minimum,maximum:o.maximum,pattern:o.pattern,patternDescription:o.patternDescription,format:o.format},isOptional:o.isOptional??!1,isInternal:o.isInternal??!1},a=e[s.name];if(a==null){if(!s.isOptional&&!t?.allowMissingRequired)throw vo(s);continue}let l=s.type;if(l){if(l.name==="url"&&Et(a,s),(l.name==="string"||l.name==="code")&&it(a,s),l.name==="number"&&at(a,s),l.isArray&&Array.isArray(a))for(let u of a)u!=null&&(l.name==="url"?Et(u,s):l.name==="string"||l.name==="code"?it(u,s):l.name==="number"&&at(u,s));if(l.name==="object"&&l.fields&&typeof a=="object"&&!Array.isArray(a)&&Oo(s,a,t),l.isArray&&l.fields&&Array.isArray(a)&&l.name==="object")for(let u of a)u&&typeof u=="object"&&Oo(s,u,t)}}}var ui=i=>{let t=/```([A-Za-z]*)\s*([\s\S]*?)\s*```/g.exec(i);return t?t.length===3?t[2]:t.length===2?t[1]:i:i};async function fr(i,e,t,n){for(let r of i){if(e[r.field.name]===void 0)continue;let o=r.process,s=await o(e[r.field.name],{sessionId:n,values:e,done:!0});kl(r.field,t,s,n)}}async function mi(i,e,t,n,r,o,s=!1){for(let a of i){if(t.currField?.name!==a.field.name)continue;let l=e.substring(t.s);t.currField?.type?.name==="code"&&(l=l.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/,""),l=l.replace(/\s*```\s*$/,""));let u=a.process,c=await u(l,{sessionId:o,values:r,done:s});kl(t.currField,n,c,o)}}var kl=(i,e,t,n)=>{if(t===void 0||typeof t=="string"&&(t===""||/^(null|undefined)\s*$/i.test(t)))return;let r=String(t),o=r;e.addRequest([{role:"user",content:[{type:"text",text:o}]}],n),e.addTag("processor",n)};function Ol(i,e){if(!e)return i;let t=[];if(e.format==="email"&&t.push("Must be a valid email address format"),(e.format==="uri"||e.format==="url"||e.name==="url")&&t.push("Must be a valid URL format"),(e.name==="string"||e.name==="code"||e.name==="url"||e.name==="date"||e.name==="datetime")&&(e.minLength!==void 0&&e.maxLength!==void 0?t.push(`Minimum length: ${e.minLength} characters, maximum length: ${e.maxLength} characters`):e.minLength!==void 0?t.push(`Minimum length: ${e.minLength} characters`):e.maxLength!==void 0&&t.push(`Maximum length: ${e.maxLength} characters`)),e.name==="number"&&(e.minimum!==void 0&&e.maximum!==void 0?t.push(`Minimum value: ${e.minimum}, maximum value: ${e.maximum}`):e.minimum!==void 0?t.push(`Minimum value: ${e.minimum}`):e.maximum!==void 0&&t.push(`Maximum value: ${e.maximum}`)),e.pattern!==void 0){if(!e.patternDescription)throw new Error(`Field with pattern '${e.pattern}' must include a patternDescription to explain the pattern to the LLM`);t.push(e.patternDescription)}if(e.name==="date"&&t.push("Format: YYYY-MM-DD"),e.name==="datetime"&&t.push("Format: ISO 8601 date-time"),t.length===0)return i;let n=t.join(". ");return!i||i.trim().length===0?n:`${i.trim().endsWith(".")?i.trim():`${i.trim()}.`} ${n}`}function Tn(i,e="Schema"){if("name"in i&&"type"in i)return Eo(i);let t={},n=[];for(let r of i){if(r.isInternal)continue;let o=Eo(r);t[r.name]=o,r.isOptional||n.push(r.name)}return{type:"object",title:e,properties:t,required:n,additionalProperties:!1}}function Eo(i,e=!1){let t=i.type,n=Ol(i.description,t);if(e&&t?.name&&(t.name==="image"||t.name==="audio"||t.name==="file"))throw new Error(`Media type '${t.name}' is not allowed in nested object fields. Media types (image, audio, file) can only be used as top-level input fields. Field: ${i.name}`);let r={};if(n&&(r.description=n),t?.isArray)if(r.type="array",t.fields){r.items={type:"object",properties:{},required:[],additionalProperties:!1},t.description&&(r.items.description=t.description);for(let[o,s]of Object.entries(t.fields)){let a={name:o,description:s.description,type:{name:s.type,isArray:s.isArray,options:s.options?[...s.options]:void 0,fields:s.fields,minLength:s.minLength,maxLength:s.maxLength,minimum:s.minimum,maximum:s.maximum,pattern:s.pattern,patternDescription:s.patternDescription,format:s.format},isOptional:s.isOptional,isInternal:s.isInternal};r.items.properties[o]=Eo(a,!0),s.isOptional||r.items.required.push(o)}}else if(t.name==="class"&&t.options)r.items={type:"string",enum:t.options};else{let o=Ol(t.description||i.description,t);r.items={type:Ml(t.name)},o&&(r.items.description=o),t.name==="string"||t.name==="code"||t.name==="url"||t.name==="date"||t.name==="datetime"?(t.minLength!==void 0&&(r.items.minLength=t.minLength),t.maxLength!==void 0&&(r.items.maxLength=t.maxLength),t.pattern!==void 0&&(r.items.pattern=t.pattern),t.format!==void 0&&(r.items.format=t.format)):t.name==="number"&&(t.minimum!==void 0&&(r.items.minimum=t.minimum),t.maximum!==void 0&&(r.items.maximum=t.maximum))}else if(t?.name==="object"&&t.fields){r.type="object",r.properties={},r.required=[],r.additionalProperties=!1;for(let[o,s]of Object.entries(t.fields)){let a={name:o,description:s.description,type:{name:s.type,isArray:s.isArray,options:s.options?[...s.options]:void 0,fields:s.fields,minLength:s.minLength,maxLength:s.maxLength,minimum:s.minimum,maximum:s.maximum,pattern:s.pattern,patternDescription:s.patternDescription,format:s.format},isOptional:s.isOptional,isInternal:s.isInternal};r.properties[o]=Eo(a,!0),s.isOptional||r.required.push(o)}}else t?.name==="class"&&t.options?(r.type="string",r.enum=t.options):(r.type=Ml(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 Ml(i){switch(i){case"string":case"code":case"url":case"date":case"datetime":case"image":case"audio":case"file":return"string";case"number":return"number";case"boolean":return"boolean";case"json":case"object":return["object","array","string","number","boolean","null"];default:return"string"}}function Po(i){if(!i||typeof i!="object")throw new Error("Schema must be an object");if(i.type==="array"){if(!i.items)throw new Error('Array schema is missing an "items" definition (required by JSON Schema and all LLM providers for function tools)');Po(i.items)}else if(i.type==="object"&&i.properties)for(let e of Object.values(i.properties))Po(e)}var $t=class extends Error{calls;constructor(e){super(`Stop function executed: ${e.map(t=>t.func.name).join(", ")}`),this.name="AxStopFunctionCallException",this.calls=e}},xr=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()}},Ar=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()}},br=class{funcList=[];constructor(e){this.funcList=e}executeFunction=async(e,t,n)=>{let r;if(typeof t.args=="string"&&t.args.length>0)try{r=JSON.parse(t.args)}catch(u){throw new Error(`Invalid function arguments: ${t.args}`,{cause:u})}else r=t.args;let o=n?{sessionId:n.sessionId,traceId:n.traceId,ai:n.ai,step:n.step,abortSignal:n.abortSignal}:void 0,s;e.parameters?s=e.func.length===2?await e.func(r,o):await e.func(r):s=e.func.length===1?await e.func(o):await e.func();let l=(n?.functionResultFormatter??ce.functionResultFormatter)(s);return{formatted:String(l),rawResult:s,parsedArgs:r}};executeWithDetails=async(e,t)=>{let n=s=>s.replace(/[^a-zA-Z0-9]/g,"").toLowerCase(),r=n(e.name),o=this.funcList.find(s=>s.name===e.name);if(o||(o=this.funcList.find(s=>n(s.name)===r)),!o)throw new Error(`Function not found: ${e.name}`);if(!o.func)throw new Error(`No handler for function: ${e.name}`);try{return await this.executeFunction(o,e,t)}catch(s){throw s instanceof Ar?new yr(s.getFields(),o,e.id):s}};execute=async(e,t)=>(await this.executeWithDetails(e,t)).formatted},Gt=(i,e)=>{if(i.length===0)return[...e??[]];let t=i.map(n=>"toFunction"in n?n.toFunction():n).flat();for(let n of t.filter(r=>r.parameters))if(n.parameters)try{Po(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()}},yr=class{funcList=[];constructor(e){this.funcList=e}executeFunction=async(e,t,n)=>{let r;if(typeof t.args=="string"&&t.args.length>0)try{r=JSON.parse(t.args)}catch(u){throw new Error(`Invalid function arguments: ${t.args}`,{cause:u})}else r=t.args;let o=n?{sessionId:n.sessionId,traceId:n.traceId,ai:n.ai,step:n.step,abortSignal:n.abortSignal}:void 0,s;e.parameters?s=e.func.length===2?await e.func(r,o):await e.func(r):s=e.func.length===1?await e.func(o):await e.func();let l=(n?.functionResultFormatter??de.functionResultFormatter)(s);return{formatted:String(l),rawResult:s,parsedArgs:r}};executeWithDetails=async(e,t)=>{let n=s=>s.replace(/[^a-zA-Z0-9]/g,"").toLowerCase(),r=n(e.name),o=this.funcList.find(s=>s.name===e.name);if(o||(o=this.funcList.find(s=>n(s.name)===r)),!o)throw new Error(`Function not found: ${e.name}`);if(!o.func)throw new Error(`No handler for function: ${e.name}`);try{return await this.executeFunction(o,e,t)}catch(s){throw s instanceof xr?new Ar(s.getFields(),o,e.id):s}};execute=async(e,t)=>(await this.executeWithDetails(e,t)).formatted},Gt=(i,e)=>{if(i.length===0)return[...e??[]];let t=i.map(n=>"toFunction"in n?n.toFunction():n).flat();for(let n of t.filter(r=>r.parameters))if(n.parameters)try{Po(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]},Fo=async({ai:i,functionList:e,functionCalls:t,mem:n,sessionId:r,traceId:o,span:s,excludeContentFromTrace:a,index:l,functionResultFormatter:u,logger:c,debug:p,stopFunctionNames:d,step:m,abortSignal:g})=>{let h=new br(e),f=new Set,b=[],x=T=>{let v=D=>D.replace(/[^a-zA-Z0-9]/g,"").toLowerCase(),F=v(T),E=e.find(D=>D.name===T);return E||(E=e.find(D=>v(D.name)===F)),E},A=t.map(T=>{if(!T.id)throw new Error(`Function ${T.name} did not return an ID`);let v=i.getOptions().tracer??ce.tracer;return v?v.startActiveSpan(`Tool: ${T.name}`,async F=>{try{F?.setAttributes?.({"tool.name":T.name,"tool.mode":"native","function.id":T.id,"session.id":r??""});let{formatted:E,rawResult:D,parsedArgs:U}=await h.executeWithDetails(T,{sessionId:r,ai:i,functionResultFormatter:u,traceId:F?.spanContext?.().traceId??o,stopFunctionNames:d,step:m,abortSignal:g});if(f.add(T.name.toLowerCase()),m?._recordFunctionCall(T.name,U,D),d?.includes(T.name.toLowerCase())){let V=x(T.name);V&&b.push({func:V,args:U,result:D})}if(a?F.addEvent("gen_ai.tool.message",{name:T.name}):F.addEvent("gen_ai.tool.message",{name:T.name,args:T.args,result:E??""}),s){let V={name:T.name};a||(V.args=T.args,V.result=E??""),s.addEvent("function.call",V)}return{result:E??"",role:"function",functionId:T.id,index:l}}catch(E){if(F?.recordException?.(E),E instanceof yr){let D=E.getFixingInstructions(),U={name:T.name,message:E.toString()};return a||(U.args=T.args,U.fixing_instructions=D),F?.addEvent?.("function.error",U),p&&Ts(E,l,D,c),{functionId:T.id,isError:!0,index:l,result:D,role:"function"}}throw E}finally{F?.end?.()}}):h.executeWithDetails(T,{sessionId:r,ai:i,functionResultFormatter:u,traceId:o,stopFunctionNames:d,step:m,abortSignal:g}).then(({formatted:F,rawResult:E,parsedArgs:D})=>{if(f.add(T.name.toLowerCase()),m?._recordFunctionCall(T.name,D,E),d?.includes(T.name.toLowerCase())){let U=x(T.name);U&&b.push({func:U,args:D,result:E})}if(s){let U={name:T.name};a||(U.args=T.args,U.result=F??""),s.addEvent("function.call",U)}return{result:F??"",role:"function",functionId:T.id,index:l}}).catch(F=>{if(!(F instanceof yr))throw F;let E=F.getFixingInstructions();if(s){let D={name:T.name,message:F.toString()};a||(D.args=T.args,D.fixing_instructions=E),s.addEvent("function.error",D)}return p&&Ts(F,l,E,c),{functionId:T.id,isError:!0,index:l,result:E,role:"function"}})}),I=(await Promise.all(A)).map(T=>T.result===void 0||T.result===""?{...T,result:"done"}:T);if(n.addFunctionResults(I,r),p){let T=I.filter(v=>!v.isError);T.length>0&&Xi(T,c)}if(b.length>0)throw new $t(b);return f};function gi(i,e,t,n){if(!e||e.length===0)return;if(!i.getFeatures(n).functions)throw new Error("Functions are not supported by the AI service");return e.map(o=>({id:o.id,name:o.function.name,args:o.function.params}))}function El(i,e,t,n){let r=e;return!t&&(r==="required"||typeof r=="function")?{functions:[],functionCall:void 0}:i?{functions:i.map(s=>"toFunction"in s?s.toFunction():s).flat(),functionCall:r}:{functions:[],functionCall:r}}function vc(i){let e=0,t=!1,n=!1,r=!1,o=!1,s=[];for(let a=0;a<i.length;a++){let l=i[a];if(n){n=!1;continue}if(l==="\\"){n=!0;continue}if(l==='"'){t=!t;continue}t||(l==="{"?(s.push("{"),e++):l==="["?(s.push("["),e++):l==="}"?s.length>0&&s[s.length-1]==="{"&&(s.pop(),e--):l==="]"&&s.length>0&&s[s.length-1]==="["&&(s.pop(),e--))}if(s.length>0){let a=s[s.length-1];r=a==="[",o=a==="{"}return{nestingLevel:e,inString:t,inArray:r,inObject:o}}function Pl(i){if(!i.trim())return{parsed:null,partialMarker:null};try{return{parsed:JSON.parse(i),partialMarker:null}}catch{}let e=vc(i),t=kc(i);try{return{parsed:JSON.parse(t),partialMarker:e}}catch{return{parsed:null,partialMarker:e}}}function kc(i){let e=i.trim();for(e.endsWith(",")&&(e=e.slice(0,-1)),e.match(/,\s*"[^"]*"\s*:\s*$/)?e=e.replace(/,\s*"[^"]*"\s*:\s*$/,""):e.match(/\{\s*"[^"]*"\s*:\s*$/)&&(e=e.replace(/"[^"]*"\s*:\s*$/,""));e.match(/[0-9][eE.+-]$/)||e.match(/[eE][+-]$/);)e=e.slice(0,-1);e=e.replace(/,(\s*[}\]])/g,"$1"),e.match(/t(r(u(e)?)?)?$/)&&!e.endsWith('"')&&!e.endsWith("true")&&e.match(/[:[,]\s*t(r(u(e)?)?)?$/)&&(e=e.replace(/t(r(u(e)?)?)?$/,"true")),e.match(/f(a(l(s(e)?)?)?)?$/)&&!e.endsWith('"')&&!e.endsWith("false")&&e.match(/[:[,]\s*f(a(l(s(e)?)?)?)?$/)&&(e=e.replace(/f(a(l(s(e)?)?)?)?$/,"false")),e.match(/n(u(l(l)?)?)?$/)&&!e.endsWith('"')&&!e.endsWith("null")&&e.match(/[:[,]\s*n(u(l(l)?)?)?$/)&&(e=e.replace(/n(u(l(l)?)?)?$/,"null"));let t=[],n=!1,r=!1;for(let o=0;o<e.length;o++){let s=e[o];if(r){r=!1;continue}if(s==="\\"){r=!0;continue}if(s==='"'){n=!n;continue}n||(s==="{"?t.push("}"):s==="["?t.push("]"):s==="}"?t.length>0&&t[t.length-1]==="}"&&t.pop():s==="]"&&t.length>0&&t[t.length-1]==="]"&&t.pop())}for(r&&(e=e.slice(0,-1)),n&&(e+='"'),t.length>0&&t[t.length-1]==="}"&&e.match(/,\s*"[^"]*"\s*$/)&&(e=e.replace(/,\s*"[^"]*"\s*$/,""));t.length>0;)e+=t.pop();return e}async function*Fl({res:i,usage:e,states:t,debug:n,stepContext:r,...o}){let s=(o.ai.getFeatures().functionCot??!1)&&o.functions!==void 0&&o.functions.length>0,a,l=[],u=i.getReader();try{for(;;){let{done:c,value:p}=await u.read();if(c)break;let d=p;d.modelUsage&&(a=d.modelUsage);for(let m of d.results){if(Array.isArray(m.citations))for(let h of m.citations)h?.url&&l.push({url:h.url,title:h.title,description:h.description,license:h.license,publicationDate:h.publicationDate,snippet:h.snippet});if((!m.content||m.content==="")&&(!m.thought||m.thought==="")&&(!m.thoughtBlocks||m.thoughtBlocks.length===0)&&(!m.functionCalls||m.functionCalls.length===0))continue;let g=t.find(h=>h.index===m.index);if(!g)throw new Error(`No state found for result (index: ${m.index})`);yield*Oc({...o,result:m,skipEarlyFail:s,state:g,debug:n})}}}finally{u.releaseLock()}for(let c of t)yield*Mc({...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*Oc({result:i,mem:e,sessionId:t,strictMode:n,skipEarlyFail:r,treatAllFieldsOptional:o,state:s,signature:a,streamingFieldProcessors:l,thoughtFieldName:u,streamingAsserts:c,asserts:p}){if(i.functionCalls&&i.functionCalls.length>0)Ur(s.functionCalls,i.functionCalls),e.updateResult({name:i.name,content:i.content,functionCalls:s.functionCalls,thoughtBlocks:i.thoughtBlocks,delta:i.functionCalls?.[0]?.function?.params,index:i.index},t);else if(i.content&&i.content.length>0){i.thought&&i.thought.length>0&&(yield{index:i.index,delta:{[u]:i.thought}}),s.content+=i.content,e.updateResult({name:i.name,content:s.content,thoughtBlocks:i.thoughtBlocks,delta:i.content,index:i.index},t);let d=a.getOutputFields();if(a.hasComplexFields()){let{parsed:h,partialMarker:f}=Pl(s.content);if(h&&typeof h=="object"){let b={},x={};for(let A of Object.keys(h))if(d.some(w=>w.name===A)){let w=h[A],I=s.values[A];if(Array.isArray(w)&&w.length>0&&Ec(w,f)&&(w=w.slice(0,-1)),x[A]=w,typeof w=="string"&&typeof I=="string"&&w.startsWith(I)){let T=w.slice(I.length);T&&(b[A]=T)}else Array.isArray(w)&&Array.isArray(I)?w.length>I.length&&(b[A]=w.slice(I.length)):JSON.stringify(w)!==JSON.stringify(I)&&(Array.isArray(w)||(b[A]=w))}try{Tn(a,x,{allowMissingRequired:!0})}catch{}Object.assign(s.values,x),Object.keys(b).length>0&&(yield{index:i.index,delta:b});return}}if(ci(a,s.values,s.xstate,s.content,{strictMode:n,skipEarlyFail:r,treatAllFieldsOptional:o}))return;c.length!==0&&await Zs(c,s.xstate,s.content),l.length!==0&&await mi(l,s.content,s.xstate,e,s.values,t),yield*di(a,s.content,s.values,s.xstate,i.index),await bn(p,s.values)}else i.thought&&i.thought.length>0?(s.values[u]=(s.values[u]??"")+i.thought,yield{index:i.index,delta:{[u]:i.thought}},e.updateResult({name:i.name,content:s.content,delta:"",index:i.index,thought:i.thought,thoughtBlocks:i.thoughtBlocks},t)):i.thoughtBlocks&&i.thoughtBlocks.length>0&&e.updateResult({name:i.name,content:s.content,delta:"",index:i.index,thoughtBlocks:i.thoughtBlocks},t);if(i.finishReason==="length")throw new Error(`Max tokens reached before completion
215
- Content: ${s.content}`)}async function*Mc({state:i,signature:e,ai:t,model:n,functions:r,mem:o,sessionId:s,traceId:a,span:l,strictMode:u,excludeContentFromTrace:c,streamingAsserts:p,asserts:d,fieldProcessors:m,streamingFieldProcessors:g,functionResultFormatter:h,signatureToolCallingManager:f,logger:b,debug:x,stopFunctionNames:A,stepContext:w,abortSignal:I}){let T=f?void 0:gi(t,i.functionCalls,i.values,n);if(T){if(!r)throw new Error("Functions are not defined");let v=await Fo({ai:t,functionList:r,functionCalls:T,mem:o,sessionId:s,traceId:a,span:l,index:i.index,excludeContentFromTrace:c,functionResultFormatter:h,logger:b,debug:x,stopFunctionNames:A,step:w,abortSignal:I});i.functionsExecuted=new Set([...i.functionsExecuted,...v]),i.functionCalls=[]}else{let v=e.getOutputFields(),F=e.hasComplexFields(),E=!1;if(F)try{let D=JSON.parse(i.content),U={};for(let V of Object.keys(D))if(v.some(k=>k.name===V)){let k=D[V],$=i.values[V];if(typeof k=="string"&&typeof $=="string"&&k.startsWith($)){let N=k.slice($.length);N&&(U[V]=N)}else Array.isArray(k)&&Array.isArray($)?k.length>$.length&&(U[V]=k.slice($.length)):JSON.stringify(k)!==JSON.stringify($)&&(Array.isArray(k)||(U[V]=k))}try{Tn(e,U,{allowMissingRequired:!0})}catch(V){let k=(V.message||"").toLowerCase();if(k.includes("at least")||k.includes("at most")||k.includes("must match pattern")||k.includes("invalid url")||k.includes("required")||k.includes("missing")||k.includes("valid email")||k.includes("number must be"))throw V}for(let V of Object.keys(D))v.some(k=>k.name===V)&&(i.values[V]=D[V]);yield{index:i.index,delta:U},E=!0}catch(D){let U=(D.message||"").toLowerCase();if(U.includes("at least")||U.includes("at most")||U.includes("must match pattern")||U.includes("invalid url")||U.includes("required")||U.includes("missing")||U.includes("valid email")||U.includes("number must be"))throw D}if(!E){let D=f!==void 0;pi(e,i.values,i.xstate,i.content,{strictMode:u,treatAllFieldsOptional:D,deferRequiredCheckForStreaming:!0,forceFinalize:!0})}if(f){let D=await f.processResults(i.values);if(D&&D.length>0){if(!r)throw new Error("Functions are not defined");let U=await Fo({ai:t,functionList:r,functionCalls:D,mem:o,sessionId:s,traceId:a,span:l,index:i.index,excludeContentFromTrace:c,functionResultFormatter:h,logger:b,debug:x,stopFunctionNames:A,step:w,abortSignal:I});i.functionsExecuted=new Set([...i.functionsExecuted,...U]),o.updateResult({name:void 0,content:i.content,functionCalls:D.map(V=>({id:V.id,type:"function",function:{name:V.name,params:V.args}})),index:i.index},s);return}}await Zs(p,i.xstate,i.content,!0),await bn(d,i.values),m.length&&await xr(m,i.values,o,s),g.length!==0&&await mi(g,i.content,i.xstate,o,i.values,s,!0),yield*di(e,i.content,i.values,i.xstate,i.index)}}async function*_l({ai:i,res:e,mem:t,sessionId:n,traceId:r,functions:o,span:s,strictMode:a,states:l,usage:u,excludeContentFromTrace:c,asserts:p,fieldProcessors:d,thoughtFieldName:m,signature:g,functionResultFormatter:h,logger:f,debug:b,signatureToolCallingManager:x,stopFunctionNames:A,disableMemoryCleanup:w,stepContext:I,abortSignal:T}){let v=e.results??[],F=x!==void 0;t.addResponse(v,n);let E=[];for(let k of v)if(Array.isArray(k?.citations))for(let $ of k.citations)$?.url&&E.push({url:$.url,title:$.title,description:$.description,license:$.license,publicationDate:$.publicationDate,snippet:$.snippet});for(let k of v){let $=l[k.index];if(!$)throw new Error(`No state found for result (index: ${k.index})`);if(e.modelUsage){let N=Array.from(new Map(E.filter(M=>M.url).map(M=>[M.url,M])).values()),Q={...e.modelUsage,...N.length?{citations:N}:{}};if(u.push(Q),b&&f){let M=structuredClone(Q);delete M.citations,f({name:"ChatResponseUsage",value:M}),Q.citations&&Q.citations.length>0&&f({name:"ChatResponseCitations",value:Q.citations})}}if(x&&k.content){k.thought&&k.thought.length>0&&($.values[m]=k.thought),Mo(g,$.values,k.content,{strictMode:a,treatAllFieldsOptional:F});let Q=(await x.processResults($.values))?.map(M=>({id:M.id,type:"function",function:{name:M.name,params:M.args}}));Q&&Q.length>0&&t.updateResult({name:k.name,content:k.content,functionCalls:Q,index:k.index},n)}if(k.functionCalls?.length){let N=gi(i,k.functionCalls,$.values);if(N&&N.length>0){if(!o)throw new Error("Functions are not defined");let Q;try{Q=await Fo({ai:i,functionList:o,functionCalls:N,mem:t,sessionId:n,traceId:r,span:s,excludeContentFromTrace:c,index:k.index,functionResultFormatter:h,logger:f,debug:b,stopFunctionNames:A,step:I,abortSignal:T})}catch(M){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),M}$.functionsExecuted=new Set([...$.functionsExecuted,...Q])}}else if(k.content){k.thought&&k.thought.length>0&&($.values[m]=k.thought);let N=g.getOutputFields();if(g.hasComplexFields())try{let M=JSON.parse(k.content),L={};for(let Y of Object.keys(M))N.some(_=>_.name===Y)&&(L[Y]=M[Y]);Tn(g,L),Object.assign($.values,L)}catch(M){let L=M instanceof SyntaxError;if((M.name?.includes("ValidationError")||M.name?.includes("Error"))&&!L){let _=(M.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 M}if(L)Mo(g,$.values,k.content,{strictMode:a,treatAllFieldsOptional:F});else throw M}else Mo(g,$.values,k.content,{strictMode:a,treatAllFieldsOptional:F})}if(await bn(p,$.values),w||(t.removeByTag("correction",n),t.removeByTag("error",n)),d.length&&await xr(d,$.values,t,n),k.finishReason==="length")throw new Error(`Max tokens reached before completion
216
- Content: ${k.content}`)}let D=l.map(k=>k.values);for(let k of D)for(let $ of g.getOutputFields())$.isInternal&&delete k[$.name];let U=g.getOutputFields(),V=D.map((k,$)=>{let N={};for(let Q of U)Q.isInternal||(N[Q.name]=k[Q.name]);return k[m]!==void 0&&(N[m]=k[m]),{index:$,delta:N}});for(let k of V)yield k}function Ec(i,e){return e?!!(e.nestingLevel>0||e.inArray||e.inObject):!1}function Nl(i,e,t,n){let r=i.getLast(n);if(!r)return!0;for(let[o,s]of t.entries()){let a=e?Array.from(e).some(p=>s.functionsExecuted.has(p)):!1;if(!r.chat[o])throw new Error(`No chat message found for result (index: ${o})`);let u=r.role==="function",c=r.tags?r.tags.some(p=>p==="processor"):!1;if(u&&e&&a||!(u||c))return!1}return!0}var Pc=["none","minimal","low","medium","high","highest"];function hi(i,e,t){let n={};if(e.model!==!1){let a=i.getModelList();if(a&&a.length>0){let l=a.filter(u=>"model"in u);if(l.length>0){let u=l.map(p=>p.key),c=l.map(p=>`${p.key} (${p.description})`);n.model={type:"string",enum:u,description:`${t&&u.includes(t)?`Currently using model: ${t}. `:""}Switch model for the next step. Prefer faster/cheaper models for simple tasks; use more capable models for complex reasoning, math, or multi-step analysis. Available: ${c.join(", ")}`}}}}e.thinkingBudget!==!1&&(n.thinkingBudget={type:"string",enum:[...Pc],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=Gt(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 Rn=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 pe=class extends Error{constructor(t,n,r,o){super(t);this.position=n;this.context=r;this.suggestion=o;this.name="SignatureValidationError"}},fi=class{input;position;currentFieldName=null;currentSection="description";constructor(e){if(this.input=e.trim(),this.position=0,!this.input)throw new pe("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 pe("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 pe('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 pe(`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 pe)throw e;let t=e instanceof Error?e.message:"Unknown error";throw new pe(t,this.position,this.getErrorContext())}}validateParsedSignature(e){let t=new Set;for(let r of e.inputs){if(t.has(r.name))throw new pe(`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 pe(`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 pe(`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 pe("Signature must have at least one input field",0,"",'Add an input field before "->". Example: "userInput:string -> ..."');if(e.outputs.length===0)throw new pe("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 pe(`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 pe?r:new pe(`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 pe(`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 pe?r:new pe(`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 pe(`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 pe(`Input field "${e}" cannot use the "class" type`,this.position,this.getErrorContext(),'Class types are only allowed on output fields. Use "string" type for input classifications');try{let o=this.parseTypeNotClass(),s=this.match("[]");n={name:o,isArray:s}}catch(o){throw o instanceof pe?o:new pe(`Input field "${e}": ${o instanceof Error?o.message:"Unknown error"}`,this.position,this.getErrorContext())}}this.skipWhitespace();let r=this.parseParsedString();return{name:e,desc:r?.trim(),type:n,isOptional:t}}parseOutputField(){this.skipWhitespace();let e=this.parseParsedIdentifier();this.currentFieldName=e,this.validateFieldName(e,"output");let t=!1,n=!1;for(;;){if(this.match("?")){t=!0;continue}if(this.match("!")){n=!0;continue}break}let r;if(this.skipWhitespace(),this.match(":"))if(this.skipWhitespace(),this.match("class")){let s=this.match("[]");this.skipWhitespace();let a=this.parseParsedString();if(!a)throw new pe(`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 pe(`Output field "${e}": Empty class list provided`,this.position,this.getErrorContext(),'Provide at least one class option. Example: "positive, negative"');r={name:"class",isArray:s,options:l}}else try{let s=this.parseTypeNotClass(),a=this.match("[]");if(r={name:s,isArray:a},s==="image"&&a)throw new pe(`Output field "${e}": Arrays of images are not supported`,this.position,this.getErrorContext(),'Use a single image type instead: "image"');if(s==="audio"&&a)throw new pe(`Output field "${e}": Arrays of audio are not supported`,this.position,this.getErrorContext(),'Use a single audio type instead: "audio"');if(s==="image")throw new pe(`Output field "${e}": Image type is not supported in output fields`,this.position,this.getErrorContext(),"Image types can only be used in input fields");if(s==="audio")throw new pe(`Output field "${e}": Audio type is not supported in output fields`,this.position,this.getErrorContext(),"Audio types can only be used in input fields")}catch(s){throw s instanceof pe?s:new pe(`Output field "${e}": ${s instanceof Error?s.message:"Unknown error"}`,this.position,this.getErrorContext())}this.skipWhitespace();let o=this.parseParsedString();return{name:e,desc:o?.trim(),type:r,isOptional:t,isInternal:n}}validateFieldName(e,t){if(ce.signatureStrict&&["text","object","image","string","number","boolean","json","array","datetime","date","time","type","class","input","output","data","value","result","response","request","item","element"].includes(e.toLowerCase())){let s=t==="input"?["userInput","questionText","documentContent","messageText"]:["responseText","analysisResult","categoryType","summaryText"];throw new pe(`Field name "${e}" is too generic`,this.position,this.getErrorContext(),`Use a more descriptive name. Examples: ${s.join(", ")}`)}let n=/^[a-z][a-zA-Z0-9]*$/,r=/^[a-z]+(_[a-z0-9]+)*$/;if(!n.test(e)&&!r.test(e))throw new pe(`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 pe(`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 pe(`Field name "${e}" is too long (${e.length} characters)`,this.position,this.getErrorContext(),"Field names should be 50 characters or less")}parseTypeNotClass(){let e=["string","number","boolean","json","image","audio","file","url","datetime","date","code","object"],t=e.find(n=>this.match(n));if(!t){let n=this.input.slice(this.position).match(/^\w+/)?.[0]||"",r=this.suggestType(n),o=`Invalid type "${n||"empty"}"`,s=r?`. Did you mean "${r}"?`:"",a=`${o}${s}`;throw new pe(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 pe("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 pe(`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 pe(`Invalid field name "${n}"`,this.position,this.getErrorContext(),"Field names must start with a letter or underscore and contain only letters, numbers, or underscores")}parseParsedString(){let e=["'",'"'];for(let t of e)if(this.match(t)){let n="",r=!1,o=this.position-1;for(;this.position<this.input.length;){let a=this.input[this.position];if(this.position++,r)n+=a,r=!1;else if(a==="\\")r=!0;else{if(a===t)return n;n+=a}}let s=this.input.slice(o,Math.min(this.position,o+20));throw new pe(`Unterminated string starting at position ${o}`,o,this.getErrorContext(),`Add closing ${t} to complete the string: ${s}${t}`)}}skipWhitespace(){let e=/^[\s\t\r\n]+/.exec(this.input.slice(this.position));e&&(this.position+=e[0].length)}match(e){let t;if(typeof e=="string"){if(this.input.startsWith(e,this.position))return this.position+=e.length,!0}else if(t=e.exec(this.input.slice(this.position)),t)return this.position+=t[0].length,!0;return!1}expectArrow(){if(!this.match("->")){let e=this.input.slice(this.position,this.position+10),t=e.includes(">")?'Use "->" (dash followed by greater-than)':e.includes("-")?'Add ">" after the dash':'Add "->" to separate input and output fields';throw new pe(`Expected "->" but found "${e}..."`,this.position,this.getErrorContext(),t)}}};function Ll(i){return new fi(i).parse()}var Ir=class{inputFields=[];outputFields=[];desc;input(e,t,n=!1){let r={name:e,type:{name:t.type,isArray:t.isArray||void 0,options:t.options?[...t.options]:void 0,minLength:t.minLength,maxLength:t.maxLength,minimum:t.minimum,maximum:t.maximum,pattern:t.pattern,patternDescription:t.patternDescription,format:t.format,description:t.itemDescription,fields:t.fields?Object.fromEntries(Object.entries(t.fields).map(([o,s])=>[o,xi(s)])):void 0},description:t.description,isOptional:t.isOptional||void 0,isInternal:t.isInternal||void 0,isCached:t.isCached||void 0};return n?this.inputFields.unshift(r):this.inputFields.push(r),this}output(e,t,n=!1){let r={name:e,type:{name:t.type,isArray:t.isArray||void 0,options:t.options?[...t.options]:void 0,minLength:t.minLength,maxLength:t.maxLength,minimum:t.minimum,maximum:t.maximum,pattern:t.pattern,patternDescription:t.patternDescription,format:t.format,description:t.itemDescription,fields:t.fields?Object.fromEntries(Object.entries(t.fields).map(([o,s])=>[o,xi(s)])):void 0},description:t.description,isOptional:t.isOptional||void 0,isInternal:t.isInternal||void 0};return n?this.outputFields.unshift(r):this.outputFields.push(r),this}addInputFields(e){for(let t of e)this.inputFields.push(t);return this}addOutputFields(e){for(let t of e)this.outputFields.push(t);return this}description(e){return this.desc=e,this}useStructured(){return this._useStructuredOutputs=!0,this}build(){let e={description:this.desc,inputs:this.inputFields,outputs:this.outputFields},t=new Oe(e);return this._useStructuredOutputs&&(t._forceComplexFields=!0,t._hasComplexFields=void 0),t}},$e=class i{type;isArray;options;description;isOptional;isInternal;isCached;fields;minLength;maxLength;minimum;maximum;pattern;patternDescription;format;itemDescription;constructor(e){this.type=e.type,this.isArray=e.isArray,this.options=e.options,this.description=e.description,this.itemDescription=e.itemDescription,this.isOptional=e.isOptional,this.isInternal=e.isInternal,this.isCached=e.isCached,this.fields=e.fields,this.minLength=e.minLength,this.maxLength=e.maxLength,this.minimum=e.minimum,this.maximum=e.maximum,this.pattern=e.pattern,this.patternDescription=e.patternDescription,this.format=e.format}optional(){return new i({...this,isOptional:!0})}array(e){return new i({...this,isArray:!0,description:e||this.description,itemDescription:e?this.description:void 0})}internal(){return new i({...this,isInternal:!0})}cache(){return new i({...this,isCached:!0})}min(e){return this.type==="string"?new i({...this,minLength:e}):this.type==="number"?new i({...this,minimum:e}):this}max(e){return this.type==="string"?new i({...this,maxLength:e}):this.type==="number"?new i({...this,maximum:e}):this}email(){return this.type==="string"?new i({...this,format:"email"}):this}url(){return this.type==="string"?new i({...this,format:"uri"}):this}regex(e,t){return this.type==="string"?new i({...this,pattern:e,patternDescription:t}):this}date(){return this.type==="string"?new i({...this,format:"date"}):this}datetime(){return this.type==="string"?new i({...this,format:"date-time"}):this}},ge=Object.assign(()=>new Ir,{string:i=>new $e({type:"string",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),number:i=>new $e({type:"number",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),boolean:i=>new $e({type:"boolean",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),json:i=>new $e({type:"json",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),datetime:i=>new $e({type:"datetime",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),date:i=>new $e({type:"date",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),class:(i,e)=>new $e({type:"class",isArray:!1,options:i,description:e,isOptional:!1,isInternal:!1,isCached:!1}),image:i=>new $e({type:"image",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),audio:i=>new $e({type:"audio",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),file:i=>new $e({type:"file",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),url:i=>new $e({type:"url",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),email:i=>new $e({type:"string",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1,format:"email"}),code:(i,e)=>new $e({type:"code",isArray:!1,description:e||i,isOptional:!1,isInternal:!1,isCached:!1}),object:(i,e)=>new $e({type:"object",isArray:!1,fields:i,description:e,isOptional:!1,isInternal:!1,isCached:!1})});function xi(i){return{type:i.type,isArray:i.isArray,options:i.options,description:i.description,isOptional:i.isOptional,isInternal:i.isInternal,minLength:i.minLength,maxLength:i.maxLength,minimum:i.minimum,maximum:i.maximum,pattern:i.pattern,patternDescription:i.patternDescription,format:i.format,fields:i.fields?Object.fromEntries(Object.entries(i.fields).map(([e,t])=>[e,xi(t)])):void 0}}function _o(i){return{type:{name:i.type,isArray:i.isArray,options:i.options?[...i.options]:void 0,fields:i.fields},description:i.description,isOptional:i.isOptional,isInternal:i.isInternal}}var te=class extends Error{constructor(t,n,r){super(t);this.fieldName=n;this.suggestion=r;this.name="AxSignatureValidationError"}},Oe=class i{description;inputFields;outputFields;sigHash;sigString;validatedAtHash;constructor(e){if(!e){this.inputFields=[],this.outputFields=[],this.sigHash="",this.sigString="";return}if(typeof e=="string"){let t;try{t=Ll(e)}catch(n){if(n instanceof Error){let r="suggestion"in n&&typeof n.suggestion=="string"?n.suggestion:'Please check the signature format. Example: "userInput:string -> responseText:string"';throw new te(`Invalid Signature: ${n.message}`,void 0,r)}throw new te(`Invalid Signature: ${e}`,void 0,'Please check the signature format. Example: "userInput:string -> responseText:string"')}this.description=t.desc,this.inputFields=t.inputs.map(n=>this.parseParsedField(n)),this.outputFields=t.outputs.map(n=>this.parseParsedField(n)),[this.sigHash,this.sigString]=this.updateHash()}else if(e instanceof i)this.description=e.getDescription(),this.inputFields=structuredClone(e.getInputFields()),this.outputFields=structuredClone(e.getOutputFields()),this.sigHash=e.hash(),this.sigString=e.toString(),e.validatedAtHash===this.sigHash&&(this.validatedAtHash=this.sigHash),this._forceComplexFields=e._forceComplexFields,this._hasComplexFields=e._hasComplexFields;else if(typeof e=="object"&&e!==null){if(!("inputs"in e)||!("outputs"in e))throw new te("Invalid signature object: missing inputs or outputs",void 0,'Signature object must have "inputs" and "outputs" arrays. Example: { inputs: [...], outputs: [...] }');if(!Array.isArray(e.inputs)||!Array.isArray(e.outputs))throw new te("Invalid signature object: inputs and outputs must be arrays",void 0,'Both "inputs" and "outputs" must be arrays of AxField objects');try{this.description=e.description,this.inputFields=e.inputs.map(t=>this.parseField(t)),this.outputFields=e.outputs.map(t=>this.parseField(t)),[this.sigHash,this.sigString]=this.updateHash()}catch(t){throw t instanceof te?t:new te(`Failed to create signature from object: ${t instanceof Error?t.message:"Unknown error"}`,void 0,"Check that all fields in inputs and outputs arrays are valid AxField objects")}}else throw new te("Invalid signature argument type",void 0,"Signature must be a string, another AxSignature instance, or an object with inputs and outputs arrays")}static create(e){return new i(e)}parseParsedField=e=>{if(!e.name||e.name.length===0)throw new te("Field name is required",e.name,'Every field must have a descriptive name. Example: "userInput", "responseText"');let t=this.toTitle(e.name);return{name:e.name,title:t,description:"desc"in e?e.desc:void 0,type:e.type??{name:"string",isArray:!1},..."isInternal"in e?{isInternal:e.isInternal}:{},..."isOptional"in e?{isOptional:e.isOptional}:{}}};parseField=e=>{let t=!e.title||e.title.length===0?this.toTitle(e.name):e.title;if(e.type&&(!e.type.name||e.type.name.length===0))throw new te("Field type name is required",e.name,"Specify a valid type. Available types: string, number, boolean, json, image, audio, file, url, date, datetime, class, code");return{...e,title:t}};setDescription=e=>{if(typeof e!="string")throw new te("Description must be a string",void 0,"Provide a string description for the signature");this.description=e,this.invalidateValidationCache(),this.updateHashLight()};addInputField=e=>{try{let t=this.parseField(e);yt(t,"input");for(let n of this.inputFields)if(n.name===t.name)throw new te(`Duplicate input field name: "${t.name}"`,t.name,"Each field name must be unique within the signature");for(let n of this.outputFields)if(n.name===t.name)throw new te(`Field name "${t.name}" appears in both inputs and outputs`,t.name,"Use different names for input and output fields to avoid confusion");this.inputFields.push(t),this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof te?t:new te(`Failed to add input field "${e.name}": ${t instanceof Error?t.message:"Unknown error"}`,e.name)}};addOutputField=e=>{try{let t=this.parseField(e);yt(t,"output");for(let n of this.outputFields)if(n.name===t.name)throw new te(`Duplicate output field name: "${t.name}"`,t.name,"Each field name must be unique within the signature");for(let n of this.inputFields)if(n.name===t.name)throw new te(`Field name "${t.name}" appears in both inputs and outputs`,t.name,"Use different names for input and output fields to avoid confusion");this.outputFields.push(t),this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof te?t:new te(`Failed to add output field "${e.name}": ${t instanceof Error?t.message:"Unknown error"}`,e.name)}};setInputFields=e=>{if(!Array.isArray(e))throw new te("Input fields must be an array",void 0,"Provide an array of field objects");try{let t=e.map(n=>{let r=this.parseField(n);return yt(r,"input"),r});this.inputFields=t,this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof te?t:new te(`Failed to set input fields: ${t instanceof Error?t.message:"Unknown error"}`)}};setOutputFields=e=>{if(!Array.isArray(e))throw new te("Output fields must be an array",void 0,"Provide an array of field objects");try{let t=e.map(n=>{let r=this.parseField(n);return yt(r,"output"),r});this.outputFields=t,this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof te?t:new te(`Failed to set output fields: ${t instanceof Error?t.message:"Unknown error"}`)}};getInputFields=()=>this.inputFields;getOutputFields=()=>this.outputFields;getDescription=()=>this.description;appendInputField=(e,t)=>{let n=new i(this);return n.addInputField({name:e,..._o(t)}),n};prependInputField=(e,t)=>{let n=new i(this),r={name:e,..._o(t)},o=n.parseField(r);yt(o,"input");for(let s of n.inputFields)if(s.name===o.name)throw new te(`Duplicate input field name: "${o.name}"`,o.name,"Each field name must be unique within the signature");for(let s of n.outputFields)if(s.name===o.name)throw new te(`Field name "${o.name}" appears in both inputs and outputs`,o.name,"Use different names for input and output fields to avoid confusion");return n.inputFields.unshift(o),n.invalidateValidationCache(),n.updateHashLight(),n};appendOutputField=(e,t)=>{let n=new i(this);return n.addOutputField({name:e,..._o(t)}),n};prependOutputField=(e,t)=>{let n=new i(this),r={name:e,..._o(t)},o=n.parseField(r);yt(o,"output");for(let s of n.outputFields)if(s.name===o.name)throw new te(`Duplicate output field name: "${o.name}"`,o.name,"Each field name must be unique within the signature");for(let s of n.inputFields)if(s.name===o.name)throw new te(`Field name "${o.name}" appears in both inputs and outputs`,o.name,"Use different names for input and output fields to avoid confusion");return n.outputFields.unshift(o),n.invalidateValidationCache(),n.updateHashLight(),n};invalidateValidationCache=()=>{this.validatedAtHash=void 0,this._hasComplexFields=void 0};toTitle=e=>{let t=e.replace(/_/g," ");return t=t.replace(/([A-Z]|[0-9]+)/g," $1").trim(),t.charAt(0).toUpperCase()+t.slice(1)};updateHashLight=()=>{try{return this.getInputFields().forEach(e=>{yt(e,"input")}),this.getOutputFields().forEach(e=>{yt(e,"output")}),this.sigHash=tt("sha256").update(JSON.stringify(this.inputFields)).update(JSON.stringify(this.outputFields)).digest("hex"),this.sigString=Gl(this.description,this.inputFields,this.outputFields),this._hasComplexFields=this.computeHasComplexFields(),[this.sigHash,this.sigString]}catch(e){throw e instanceof te?e:new te(`Signature validation failed: ${e instanceof Error?e.message:"Unknown error"}`)}};updateHash=()=>{try{return this.getInputFields().forEach(e=>{yt(e,"input")}),this.getOutputFields().forEach(e=>{yt(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=Gl(this.description,this.inputFields,this.outputFields),this._hasComplexFields=this.computeHasComplexFields(),[this.sigHash,this.sigString]}catch(e){throw e instanceof te?e:new te(`Signature validation failed: ${e instanceof Error?e.message:"Unknown error"}`)}};validateSignatureConsistency(){let e=new Set;for(let n of this.inputFields){if(e.has(n.name))throw new te(`Duplicate input field name: "${n.name}"`,n.name,"Each field name must be unique within the signature");e.add(n.name)}let t=new Set;for(let n of this.outputFields){if(t.has(n.name))throw new te(`Duplicate output field name: "${n.name}"`,n.name,"Each field name must be unique within the signature");t.add(n.name)}for(let n of this.outputFields)if(e.has(n.name))throw new te(`Field name "${n.name}" appears in both inputs and outputs`,n.name,"Use different names for input and output fields to avoid confusion");if(this.inputFields.length===0)throw new te("Signature must have at least one input field",void 0,'Add an input field. Example: "userInput:string -> ..."');if(this.outputFields.length===0)throw new te("Signature must have at least one output field",void 0,'Add an output field. Example: "... -> responseText:string"')}_forceComplexFields=!1;_hasComplexFields;hasComplexFields=()=>this._hasComplexFields!==void 0?this._hasComplexFields:(this._hasComplexFields=this.computeHasComplexFields(),this._hasComplexFields);computeHasComplexFields=()=>this._forceComplexFields?!0:this.outputFields.some(e=>e.type?.name==="object"||e.type?.isArray&&e.type.fields!==void 0);validate=()=>{if(this.validatedAtHash===this.sigHash)return!0;try{return this.updateHash(),this.validatedAtHash=this.sigHash,!0}catch(e){throw this.validatedAtHash=void 0,e}};hash=()=>this.sigHash;toString=()=>this.sigString;toJSON=()=>({id:this.hash(),description:this.description,inputFields:this.inputFields,outputFields:this.outputFields});toJSONSchema=()=>{let e=[...this.inputFields,...this.outputFields];return wn(e,this.description??"Schema")};toInputJSONSchema=()=>wn(this.inputFields,this.description??"Schema")};function $l(i){let e=i.name;return i.isOptional&&(e+="?"),i.isInternal&&(e+="!"),i.type&&(e+=`:${i.type.name}`,i.type.isArray&&(e+="[]"),i.type.name==="class"&&i.type.options&&(e+=` "${i.type.options.join(" | ")}"`)),i.description&&i.type?.name!=="class"&&(e+=` "${i.description}"`),e}function Gl(i,e,t){let n=i?`"${i}" `:"",r=e.map($l).join(", "),o=t.map($l).join(", ");return`${n}${r} -> ${o}`}function Fc(i){let e=/^[a-z][a-zA-Z0-9]*$/,t=/^[a-z]+(_[a-z0-9]+)*$/;return e.test(i)||t.test(i)}function yt(i,e){if(!i.name||i.name.length===0)throw new te("Field name cannot be blank",i.name,"Every field must have a descriptive name");if(!Fc(i.name))throw new te(`Invalid field name '${i.name}' - must be camelCase or snake_case`,i.name,'Use camelCase (e.g., "userInput") or snake_case (e.g., "user_input")');if(ce.signatureStrict&&["text","object","image","string","number","boolean","json","array","datetime","date","time","type","class","input","output","data","value","result","response","request","item","element"].includes(i.name.toLowerCase())){let n=e==="input"?["userInput","questionText","documentContent","messageText","queryString"]:["responseText","analysisResult","categoryType","summaryText","outputData"];throw new te(`Field name '${i.name}' is too generic`,i.name,`Use a more descriptive name. Examples for ${e} fields: ${n.join(", ")}`)}if(i.name.length<2)throw new te(`Field name '${i.name}' is too short`,i.name,"Field names must be at least 2 characters long");if(i.name.length>50)throw new te(`Field name '${i.name}' is too long (${i.name.length} characters)`,i.name,"Field names should be 50 characters or less");i.type&&_c(i,e)}function _c(i,e){if(!i.type)return;let{type:t}=i;if((t.name==="image"||t.name==="audio"||t.name==="file")&&e==="output")throw new te(`${t.name} type is not supported in output fields`,i.name,`${t.name} types can only be used in input fields`);if(t.name==="class"){if(e==="input")throw new te("Class type is not supported in input fields",i.name,'Class types are only allowed on output fields. Use "string" type for input classifications');if(!t.options||t.options.length===0)throw new te("Class type requires options",i.name,'Provide class options. Example: class "positive, negative, neutral"');for(let r of t.options){if(!r||r.trim().length===0)throw new te("Empty class option found",i.name,"All class options must be non-empty strings");let o=r.trim();if(o.includes(",")||o.includes("|"))throw new te(`Invalid class option "${o}"`,i.name,"Class options cannot contain commas (,) or pipes (|) as they are used to separate options")}if(new Set(t.options.map(r=>r.trim().toLowerCase())).size!==t.options.length)throw new te("Duplicate class options found",i.name,"Each class option must be unique (case-insensitive)")}if(t.name==="code"&&t.isArray)throw new te("Arrays of code are not commonly supported",i.name,"Consider using a single code field or an array of strings instead");if(i.isInternal&&e==="input")throw new te("Internal marker (!) is not allowed on input fields",i.name,"Internal markers are only allowed on output fields");t.name==="object"&&t.fields&&Ai(t.fields,i.name,e)}function Ai(i,e,t,n=1){for(let[r,o]of Object.entries(i)){let s=`${e}.${r}`;if(o.type==="image"||o.type==="audio"||o.type==="file")throw new te(`${o.type} type is not allowed in nested object fields`,s,`Media types (image, audio, file) can only be used as top-level input fields, not within objects. Found at depth ${n}.`);o.type==="object"&&o.fields&&Ai(o.fields,s,t,n+1),o.isArray&&o.fields&&Ai(o.fields,`${s}[]`,t,n+1)}}var Pt=class i{signature;sigHash;examples;examplesOptions;demos;trace;usage=[];traceLabel;key;children;childNames=new Map;childCount=0;constructor(e,t){this.signature=new Oe(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 Rn,this.key={id:"root"}}getSignature(){return new Oe(this.signature)}setSignature(e){this.signature=new Oe(e),e&&this.signature.validate(),this.updateSignatureHash()}setDescription(e){this.signature.setDescription(e),this.updateSignatureHash()}updateSignatureHash(){this.sigHash=this.signature.hash()}getId(){return this.key.id}register(e,t){let n=t??`p${this.childCount}`;this.childCount++,e.setId([this.key.id,n].join(".")),this.childNames.set(e,n),this.children.register(e)}setId(e){this.key={id:e,custom:!0};for(let[t,n]of this.childNames)t.setId([e,n].join("."))}setExamples(e,t){let n=[];if("programId"in e&&e.programId===this.key.id&&(n=e.traces),Array.isArray(e)&&(n=e),n){this.examplesOptions=t;let r=this.signature,o=[...r.getInputFields(),...r.getOutputFields()];this.examples=n.map(s=>{let a={};for(let l of o){let u=s[l.name];u!==void 0&&(hr(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 fr(e)}resetUsage(){this.usage=[];for(let e of Array.from(this.children))e?.resetUsage()}static _propagating=!1;setDemos(e,t){if(!i._propagating&&e.length>0){let u=new Set(this.namedPrograms().map(p=>p.id)),c=[...new Set(e.map(p=>p.programId))].filter(p=>!u.has(p));if(c.length>0){let p=[...u].join(", ");throw new Error(`Unknown program ID(s) in demos: ${c.join(", ")}. Valid IDs: ${p}. Use namedPrograms() to discover available IDs.`)}}let n=e.filter(u=>u.programId===this.key.id).map(u=>u.traces).flat(),r=this.signature,o=[...r.getInputFields(),...r.getOutputFields()],s=new Set(r.getInputFields().map(u=>u.name)),a=new Set(r.getOutputFields().map(u=>u.name));this.demos=n.map((u,c)=>{let p={};for(let g of o){let h=u[g.name];h!==void 0&&(hr(g,h),p[g.name]=h)}let d=Object.keys(p).some(g=>s.has(g));if(!Object.keys(p).some(g=>a.has(g)))throw new Error(`Demo trace[${c}] for '${this.key.id}' has no output field values. Expected at least one of: ${[...a].join(", ")}`);if(!d)throw new Error(`Demo trace[${c}] for '${this.key.id}' has no input field values. Expected at least one of: ${[...s].join(", ")}. Provide input context so the demo renders as a complete few-shot example.`);return p}),t?.modelConfig&&(this._optimizedModelConfig=t.modelConfig);let l=i._propagating;i._propagating=!0;try{for(let u of Array.from(this.children))u?.setDemos(e,t)}finally{i._propagating=l}}namedPrograms(){let e=[];[...this.signature.getInputFields(),...this.signature.getOutputFields()].length>0&&e.push({id:this.key.id,signature:this.signature.toString()});for(let n of Array.from(this.children))n&&"namedPrograms"in n&&typeof n.namedPrograms=="function"?e.push(...n.namedPrograms()):n&&e.push({id:n.getId()});return e}applyOptimization(e){this.setDemos(e.demos??[],{modelConfig:e.modelConfig})}};var Dl={"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]},Fo=async({ai:i,functionList:e,functionCalls:t,mem:n,sessionId:r,traceId:o,span:s,excludeContentFromTrace:a,index:l,functionResultFormatter:u,logger:c,debug:p,stopFunctionNames:d,step:m,abortSignal:g})=>{let h=new yr(e),f=new Set,I=[],x=T=>{let v=D=>D.replace(/[^a-zA-Z0-9]/g,"").toLowerCase(),F=v(T),O=e.find(D=>D.name===T);return O||(O=e.find(D=>v(D.name)===F)),O},A=t.map(T=>{if(!T.id)throw new Error(`Function ${T.name} did not return an ID`);let v=i.getOptions().tracer??de.tracer;return v?v.startActiveSpan(`Tool: ${T.name}`,async F=>{try{F?.setAttributes?.({"tool.name":T.name,"tool.mode":"native","function.id":T.id,"session.id":r??""});let{formatted:O,rawResult:D,parsedArgs:B}=await h.executeWithDetails(T,{sessionId:r,ai:i,functionResultFormatter:u,traceId:F?.spanContext?.().traceId??o,stopFunctionNames:d,step:m,abortSignal:g});if(f.add(T.name.toLowerCase()),m?._recordFunctionCall(T.name,B,D),d?.includes(T.name.toLowerCase())){let Q=x(T.name);Q&&I.push({func:Q,args:B,result:D})}if(a?F.addEvent("gen_ai.tool.message",{name:T.name}):F.addEvent("gen_ai.tool.message",{name:T.name,args:T.args,result:O??""}),s){let Q={name:T.name};a||(Q.args=T.args,Q.result=O??""),s.addEvent("function.call",Q)}return{result:O??"",role:"function",functionId:T.id,index:l}}catch(O){if(F?.recordException?.(O),O instanceof Ar){let D=O.getFixingInstructions(),B={name:T.name,message:O.toString()};return a||(B.args=T.args,B.fixing_instructions=D),F?.addEvent?.("function.error",B),p&&Ts(O,l,D,c),{functionId:T.id,isError:!0,index:l,result:D,role:"function"}}throw O}finally{F?.end?.()}}):h.executeWithDetails(T,{sessionId:r,ai:i,functionResultFormatter:u,traceId:o,stopFunctionNames:d,step:m,abortSignal:g}).then(({formatted:F,rawResult:O,parsedArgs:D})=>{if(f.add(T.name.toLowerCase()),m?._recordFunctionCall(T.name,D,O),d?.includes(T.name.toLowerCase())){let B=x(T.name);B&&I.push({func:B,args:D,result:O})}if(s){let B={name:T.name};a||(B.args=T.args,B.result=F??""),s.addEvent("function.call",B)}return{result:F??"",role:"function",functionId:T.id,index:l}}).catch(F=>{if(!(F instanceof Ar))throw F;let O=F.getFixingInstructions();if(s){let D={name:T.name,message:F.toString()};a||(D.args=T.args,D.fixing_instructions=O),s.addEvent("function.error",D)}return p&&Ts(F,l,O,c),{functionId:T.id,isError:!0,index:l,result:O,role:"function"}})}),b=(await Promise.all(A)).map(T=>T.result===void 0||T.result===""?{...T,result:"done"}:T);if(n.addFunctionResults(b,r),p){let T=b.filter(v=>!v.isError);T.length>0&&Xi(T,c)}if(I.length>0)throw new $t(I);return f};function gi(i,e,t,n){if(!e||e.length===0)return;if(!i.getFeatures(n).functions)throw new Error("Functions are not supported by the AI service");return e.map(o=>({id:o.id,name:o.function.name,args:o.function.params}))}function El(i,e,t,n){let r=e;return!t&&(r==="required"||typeof r=="function")?{functions:[],functionCall:void 0}:i?{functions:i.map(s=>"toFunction"in s?s.toFunction():s).flat(),functionCall:r}:{functions:[],functionCall:r}}function Oc(i){let e=0,t=!1,n=!1,r=!1,o=!1,s=[];for(let a=0;a<i.length;a++){let l=i[a];if(n){n=!1;continue}if(l==="\\"){n=!0;continue}if(l==='"'){t=!t;continue}t||(l==="{"?(s.push("{"),e++):l==="["?(s.push("["),e++):l==="}"?s.length>0&&s[s.length-1]==="{"&&(s.pop(),e--):l==="]"&&s.length>0&&s[s.length-1]==="["&&(s.pop(),e--))}if(s.length>0){let a=s[s.length-1];r=a==="[",o=a==="{"}return{nestingLevel:e,inString:t,inArray:r,inObject:o}}function Pl(i){if(!i.trim())return{parsed:null,partialMarker:null};try{return{parsed:JSON.parse(i),partialMarker:null}}catch{}let e=Oc(i),t=Mc(i);try{return{parsed:JSON.parse(t),partialMarker:e}}catch{return{parsed:null,partialMarker:e}}}function Mc(i){let e=i.trim();for(e.endsWith(",")&&(e=e.slice(0,-1)),e.match(/,\s*"[^"]*"\s*:\s*$/)?e=e.replace(/,\s*"[^"]*"\s*:\s*$/,""):e.match(/\{\s*"[^"]*"\s*:\s*$/)&&(e=e.replace(/"[^"]*"\s*:\s*$/,""));e.match(/[0-9][eE.+-]$/)||e.match(/[eE][+-]$/);)e=e.slice(0,-1);e=e.replace(/,(\s*[}\]])/g,"$1"),e.match(/t(r(u(e)?)?)?$/)&&!e.endsWith('"')&&!e.endsWith("true")&&e.match(/[:[,]\s*t(r(u(e)?)?)?$/)&&(e=e.replace(/t(r(u(e)?)?)?$/,"true")),e.match(/f(a(l(s(e)?)?)?)?$/)&&!e.endsWith('"')&&!e.endsWith("false")&&e.match(/[:[,]\s*f(a(l(s(e)?)?)?)?$/)&&(e=e.replace(/f(a(l(s(e)?)?)?)?$/,"false")),e.match(/n(u(l(l)?)?)?$/)&&!e.endsWith('"')&&!e.endsWith("null")&&e.match(/[:[,]\s*n(u(l(l)?)?)?$/)&&(e=e.replace(/n(u(l(l)?)?)?$/,"null"));let t=[],n=!1,r=!1;for(let o=0;o<e.length;o++){let s=e[o];if(r){r=!1;continue}if(s==="\\"){r=!0;continue}if(s==='"'){n=!n;continue}n||(s==="{"?t.push("}"):s==="["?t.push("]"):s==="}"?t.length>0&&t[t.length-1]==="}"&&t.pop():s==="]"&&t.length>0&&t[t.length-1]==="]"&&t.pop())}for(r&&(e=e.slice(0,-1)),n&&(e+='"'),t.length>0&&t[t.length-1]==="}"&&e.match(/,\s*"[^"]*"\s*$/)&&(e=e.replace(/,\s*"[^"]*"\s*$/,""));t.length>0;)e+=t.pop();return e}async function*Fl({res:i,usage:e,states:t,debug:n,stepContext:r,...o}){let s=(o.ai.getFeatures().functionCot??!1)&&o.functions!==void 0&&o.functions.length>0,a,l=[],u=i.getReader();try{for(;;){let{done:c,value:p}=await u.read();if(c)break;let d=p;d.modelUsage&&(a=d.modelUsage);for(let m of d.results){if(Array.isArray(m.citations))for(let h of m.citations)h?.url&&l.push({url:h.url,title:h.title,description:h.description,license:h.license,publicationDate:h.publicationDate,snippet:h.snippet});if((!m.content||m.content==="")&&(!m.thought||m.thought==="")&&(!m.thoughtBlocks||m.thoughtBlocks.length===0)&&(!m.functionCalls||m.functionCalls.length===0))continue;let g=t.find(h=>h.index===m.index);if(!g)throw new Error(`No state found for result (index: ${m.index})`);yield*Ec({...o,result:m,skipEarlyFail:s,state:g,debug:n})}}}finally{u.releaseLock()}for(let c of t)yield*Pc({...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*Ec({result:i,mem:e,sessionId:t,strictMode:n,skipEarlyFail:r,treatAllFieldsOptional:o,state:s,signature:a,streamingFieldProcessors:l,thoughtFieldName:u,streamingAsserts:c,asserts:p}){if(i.functionCalls&&i.functionCalls.length>0)Ur(s.functionCalls,i.functionCalls),e.updateResult({name:i.name,content:i.content,functionCalls:s.functionCalls,thoughtBlocks:i.thoughtBlocks,delta:i.functionCalls?.[0]?.function?.params,index:i.index},t);else if(i.content&&i.content.length>0){i.thought&&i.thought.length>0&&(yield{index:i.index,delta:{[u]:i.thought}}),s.content+=i.content,e.updateResult({name:i.name,content:s.content,thoughtBlocks:i.thoughtBlocks,delta:i.content,index:i.index},t);let d=a.getOutputFields();if(a.hasComplexFields()){let{parsed:h,partialMarker:f}=Pl(s.content);if(h&&typeof h=="object"){let I={},x={};for(let A of Object.keys(h))if(d.some(w=>w.name===A)){let w=h[A],b=s.values[A];if(Array.isArray(w)&&w.length>0&&Fc(w,f)&&(w=w.slice(0,-1)),x[A]=w,typeof w=="string"&&typeof b=="string"&&w.startsWith(b)){let T=w.slice(b.length);T&&(I[A]=T)}else Array.isArray(w)&&Array.isArray(b)?w.length>b.length&&(I[A]=w.slice(b.length)):JSON.stringify(w)!==JSON.stringify(b)&&(Array.isArray(w)||(I[A]=w))}try{Cn(a,x,{allowMissingRequired:!0})}catch{}Object.assign(s.values,x),Object.keys(I).length>0&&(yield{index:i.index,delta:I});return}}if(ci(a,s.values,s.xstate,s.content,{strictMode:n,skipEarlyFail:r,treatAllFieldsOptional:o}))return;c.length!==0&&await Zs(c,s.xstate,s.content),l.length!==0&&await mi(l,s.content,s.xstate,e,s.values,t),yield*di(a,s.content,s.values,s.xstate,i.index),await yn(p,s.values)}else i.thought&&i.thought.length>0?(s.values[u]=(s.values[u]??"")+i.thought,yield{index:i.index,delta:{[u]:i.thought}},e.updateResult({name:i.name,content:s.content,delta:"",index:i.index,thought:i.thought,thoughtBlocks:i.thoughtBlocks},t)):i.thoughtBlocks&&i.thoughtBlocks.length>0&&e.updateResult({name:i.name,content:s.content,delta:"",index:i.index,thoughtBlocks:i.thoughtBlocks},t);if(i.finishReason==="length")throw new Error(`Max tokens reached before completion
215
+ Content: ${s.content}`)}async function*Pc({state:i,signature:e,ai:t,model:n,functions:r,mem:o,sessionId:s,traceId:a,span:l,strictMode:u,excludeContentFromTrace:c,streamingAsserts:p,asserts:d,fieldProcessors:m,streamingFieldProcessors:g,functionResultFormatter:h,signatureToolCallingManager:f,logger:I,debug:x,stopFunctionNames:A,stepContext:w,abortSignal:b}){let T=f?void 0:gi(t,i.functionCalls,i.values,n);if(T){if(!r)throw new Error("Functions are not defined");let v=await Fo({ai:t,functionList:r,functionCalls:T,mem:o,sessionId:s,traceId:a,span:l,index:i.index,excludeContentFromTrace:c,functionResultFormatter:h,logger:I,debug:x,stopFunctionNames:A,step:w,abortSignal:b});i.functionsExecuted=new Set([...i.functionsExecuted,...v]),i.functionCalls=[]}else{let v=e.getOutputFields(),F=e.hasComplexFields(),O=!1;if(F)try{let D=JSON.parse(i.content),B={};for(let Q of Object.keys(D))if(v.some(S=>S.name===Q)){let S=D[Q],G=i.values[Q];if(typeof S=="string"&&typeof G=="string"&&S.startsWith(G)){let N=S.slice(G.length);N&&(B[Q]=N)}else Array.isArray(S)&&Array.isArray(G)?S.length>G.length&&(B[Q]=S.slice(G.length)):JSON.stringify(S)!==JSON.stringify(G)&&(Array.isArray(S)||(B[Q]=S))}try{Cn(e,B,{allowMissingRequired:!0})}catch(Q){let S=(Q.message||"").toLowerCase();if(S.includes("at least")||S.includes("at most")||S.includes("must match pattern")||S.includes("invalid url")||S.includes("required")||S.includes("missing")||S.includes("valid email")||S.includes("number must be"))throw Q}for(let Q of Object.keys(D))v.some(S=>S.name===Q)&&(i.values[Q]=D[Q]);yield{index:i.index,delta:B},O=!0}catch(D){let B=(D.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 D}if(!O){let D=f!==void 0;pi(e,i.values,i.xstate,i.content,{strictMode:u,treatAllFieldsOptional:D,deferRequiredCheckForStreaming:!0,forceFinalize:!0})}if(f){let D=await f.processResults(i.values);if(D&&D.length>0){if(!r)throw new Error("Functions are not defined");let B=await Fo({ai:t,functionList:r,functionCalls:D,mem:o,sessionId:s,traceId:a,span:l,index:i.index,excludeContentFromTrace:c,functionResultFormatter:h,logger:I,debug:x,stopFunctionNames:A,step:w,abortSignal:b});i.functionsExecuted=new Set([...i.functionsExecuted,...B]),o.updateResult({name:void 0,content:i.content,functionCalls:D.map(Q=>({id:Q.id,type:"function",function:{name:Q.name,params:Q.args}})),index:i.index},s);return}}await Zs(p,i.xstate,i.content,!0),await yn(d,i.values),m.length&&await fr(m,i.values,o,s),g.length!==0&&await mi(g,i.content,i.xstate,o,i.values,s,!0),yield*di(e,i.content,i.values,i.xstate,i.index)}}async function*_l({ai:i,res:e,mem:t,sessionId:n,traceId:r,functions:o,span:s,strictMode:a,states:l,usage:u,excludeContentFromTrace:c,asserts:p,fieldProcessors:d,thoughtFieldName:m,signature:g,functionResultFormatter:h,logger:f,debug:I,signatureToolCallingManager:x,stopFunctionNames:A,disableMemoryCleanup:w,stepContext:b,abortSignal:T}){let v=e.results??[],F=x!==void 0;t.addResponse(v,n);let O=[];for(let S of v)if(Array.isArray(S?.citations))for(let G of S.citations)G?.url&&O.push({url:G.url,title:G.title,description:G.description,license:G.license,publicationDate:G.publicationDate,snippet:G.snippet});for(let S of v){let G=l[S.index];if(!G)throw new Error(`No state found for result (index: ${S.index})`);if(e.modelUsage){let N=Array.from(new Map(O.filter(M=>M.url).map(M=>[M.url,M])).values()),Y={...e.modelUsage,...N.length?{citations:N}:{}};if(u.push(Y),I&&f){let M=structuredClone(Y);delete M.citations,f({name:"ChatResponseUsage",value:M}),Y.citations&&Y.citations.length>0&&f({name:"ChatResponseCitations",value:Y.citations})}}if(x&&S.content){S.thought&&S.thought.length>0&&(G.values[m]=S.thought),Mo(g,G.values,S.content,{strictMode:a,treatAllFieldsOptional:F});let Y=(await x.processResults(G.values))?.map(M=>({id:M.id,type:"function",function:{name:M.name,params:M.args}}));Y&&Y.length>0&&t.updateResult({name:S.name,content:S.content,functionCalls:Y,index:S.index},n)}if(S.functionCalls?.length){let N=gi(i,S.functionCalls,G.values);if(N&&N.length>0){if(!o)throw new Error("Functions are not defined");let Y;try{Y=await Fo({ai:i,functionList:o,functionCalls:N,mem:t,sessionId:n,traceId:r,span:s,excludeContentFromTrace:c,index:S.index,functionResultFormatter:h,logger:f,debug:I,stopFunctionNames:A,step:b,abortSignal:T})}catch(M){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),M}G.functionsExecuted=new Set([...G.functionsExecuted,...Y])}}else if(S.content){S.thought&&S.thought.length>0&&(G.values[m]=S.thought);let N=g.getOutputFields();if(g.hasComplexFields())try{let M=JSON.parse(S.content),L={};for(let X of Object.keys(M))N.some(_=>_.name===X)&&(L[X]=M[X]);Cn(g,L),Object.assign(G.values,L)}catch(M){let L=M instanceof SyntaxError;if((M.name?.includes("ValidationError")||M.name?.includes("Error"))&&!L){let _=(M.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 M}if(L)Mo(g,G.values,S.content,{strictMode:a,treatAllFieldsOptional:F});else throw M}else Mo(g,G.values,S.content,{strictMode:a,treatAllFieldsOptional:F})}if(await yn(p,G.values),w||(t.removeByTag("correction",n),t.removeByTag("error",n)),d.length&&await fr(d,G.values,t,n),S.finishReason==="length")throw new Error(`Max tokens reached before completion
216
+ Content: ${S.content}`)}let D=l.map(S=>S.values);for(let S of D)for(let G of g.getOutputFields())G.isInternal&&delete S[G.name];let B=g.getOutputFields(),Q=D.map((S,G)=>{let N={};for(let Y of B)Y.isInternal||(N[Y.name]=S[Y.name]);return S[m]!==void 0&&(N[m]=S[m]),{index:G,delta:N}});for(let S of Q)yield S}function Fc(i,e){return e?!!(e.nestingLevel>0||e.inArray||e.inObject):!1}function Nl(i,e,t,n){let r=i.getLast(n);if(!r)return!0;for(let[o,s]of t.entries()){let a=e?Array.from(e).some(p=>s.functionsExecuted.has(p)):!1;if(!r.chat[o])throw new Error(`No chat message found for result (index: ${o})`);let u=r.role==="function",c=r.tags?r.tags.some(p=>p==="processor"):!1;if(u&&e&&a||!(u||c))return!1}return!0}var _c=["none","minimal","low","medium","high","highest"];function hi(i,e,t){let n={};if(e.model!==!1){let a=i.getModelList();if(a&&a.length>0){let l=a.filter(u=>"model"in u);if(l.length>0){let u=l.map(p=>p.key),c=l.map(p=>`${p.key} (${p.description})`);n.model={type:"string",enum:u,description:`${t&&u.includes(t)?`Currently using model: ${t}. `:""}Switch model for the next step. Prefer faster/cheaper models for simple tasks; use more capable models for complex reasoning, math, or multi-step analysis. Available: ${c.join(", ")}`}}}}e.thinkingBudget!==!1&&(n.thinkingBudget={type:"string",enum:[..._c],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=Gt(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 wn=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 me=class extends Error{constructor(t,n,r,o){super(t);this.position=n;this.context=r;this.suggestion=o;this.name="SignatureValidationError"}},fi=class{input;position;currentFieldName=null;currentSection="description";constructor(e){if(this.input=e.trim(),this.position=0,!this.input)throw new me("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 me("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 me('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 me(`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 me)throw e;let t=e instanceof Error?e.message:"Unknown error";throw new me(t,this.position,this.getErrorContext())}}validateParsedSignature(e){let t=new Set;for(let r of e.inputs){if(t.has(r.name))throw new me(`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 me(`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 me(`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 me("Signature must have at least one input field",0,"",'Add an input field before "->". Example: "userInput:string -> ..."');if(e.outputs.length===0)throw new me("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 me(`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 me?r:new me(`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 me(`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 me?r:new me(`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 me(`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 me(`Input field "${e}" cannot use the "class" type`,this.position,this.getErrorContext(),'Class types are only allowed on output fields. Use "string" type for input classifications');try{let o=this.parseTypeNotClass(),s=this.match("[]");n={name:o,isArray:s}}catch(o){throw o instanceof me?o:new me(`Input field "${e}": ${o instanceof Error?o.message:"Unknown error"}`,this.position,this.getErrorContext())}}this.skipWhitespace();let r=this.parseParsedString();return{name:e,desc:r?.trim(),type:n,isOptional:t}}parseOutputField(){this.skipWhitespace();let e=this.parseParsedIdentifier();this.currentFieldName=e,this.validateFieldName(e,"output");let t=!1,n=!1;for(;;){if(this.match("?")){t=!0;continue}if(this.match("!")){n=!0;continue}break}let r;if(this.skipWhitespace(),this.match(":"))if(this.skipWhitespace(),this.match("class")){let s=this.match("[]");this.skipWhitespace();let a=this.parseParsedString();if(!a)throw new me(`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 me(`Output field "${e}": Empty class list provided`,this.position,this.getErrorContext(),'Provide at least one class option. Example: "positive, negative"');r={name:"class",isArray:s,options:l}}else try{let s=this.parseTypeNotClass(),a=this.match("[]");if(r={name:s,isArray:a},s==="image"&&a)throw new me(`Output field "${e}": Arrays of images are not supported`,this.position,this.getErrorContext(),'Use a single image type instead: "image"');if(s==="audio"&&a)throw new me(`Output field "${e}": Arrays of audio are not supported`,this.position,this.getErrorContext(),'Use a single audio type instead: "audio"');if(s==="image")throw new me(`Output field "${e}": Image type is not supported in output fields`,this.position,this.getErrorContext(),"Image types can only be used in input fields");if(s==="audio")throw new me(`Output field "${e}": Audio type is not supported in output fields`,this.position,this.getErrorContext(),"Audio types can only be used in input fields")}catch(s){throw s instanceof me?s:new me(`Output field "${e}": ${s instanceof Error?s.message:"Unknown error"}`,this.position,this.getErrorContext())}this.skipWhitespace();let o=this.parseParsedString();return{name:e,desc:o?.trim(),type:r,isOptional:t,isInternal:n}}validateFieldName(e,t){if(de.signatureStrict&&["text","object","image","string","number","boolean","json","array","datetime","date","time","type","class","input","output","data","value","result","response","request","item","element"].includes(e.toLowerCase())){let s=t==="input"?["userInput","questionText","documentContent","messageText"]:["responseText","analysisResult","categoryType","summaryText"];throw new me(`Field name "${e}" is too generic`,this.position,this.getErrorContext(),`Use a more descriptive name. Examples: ${s.join(", ")}`)}let n=/^[a-z][a-zA-Z0-9]*$/,r=/^[a-z]+(_[a-z0-9]+)*$/;if(!n.test(e)&&!r.test(e))throw new me(`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 me(`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 me(`Field name "${e}" is too long (${e.length} characters)`,this.position,this.getErrorContext(),"Field names should be 50 characters or less")}parseTypeNotClass(){let e=["string","number","boolean","json","image","audio","file","url","datetime","date","code","object"],t=e.find(n=>this.match(n));if(!t){let n=this.input.slice(this.position).match(/^\w+/)?.[0]||"",r=this.suggestType(n),o=`Invalid type "${n||"empty"}"`,s=r?`. Did you mean "${r}"?`:"",a=`${o}${s}`;throw new me(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 me("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 me(`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 me(`Invalid field name "${n}"`,this.position,this.getErrorContext(),"Field names must start with a letter or underscore and contain only letters, numbers, or underscores")}parseParsedString(){let e=["'",'"'];for(let t of e)if(this.match(t)){let n="",r=!1,o=this.position-1;for(;this.position<this.input.length;){let a=this.input[this.position];if(this.position++,r)n+=a,r=!1;else if(a==="\\")r=!0;else{if(a===t)return n;n+=a}}let s=this.input.slice(o,Math.min(this.position,o+20));throw new me(`Unterminated string starting at position ${o}`,o,this.getErrorContext(),`Add closing ${t} to complete the string: ${s}${t}`)}}skipWhitespace(){let e=/^[\s\t\r\n]+/.exec(this.input.slice(this.position));e&&(this.position+=e[0].length)}match(e){let t;if(typeof e=="string"){if(this.input.startsWith(e,this.position))return this.position+=e.length,!0}else if(t=e.exec(this.input.slice(this.position)),t)return this.position+=t[0].length,!0;return!1}expectArrow(){if(!this.match("->")){let e=this.input.slice(this.position,this.position+10),t=e.includes(">")?'Use "->" (dash followed by greater-than)':e.includes("-")?'Add ">" after the dash':'Add "->" to separate input and output fields';throw new me(`Expected "->" but found "${e}..."`,this.position,this.getErrorContext(),t)}}};function Ll(i){return new fi(i).parse()}var br=class{inputFields=[];outputFields=[];desc;input(e,t,n=!1){let r={name:e,type:{name:t.type,isArray:t.isArray||void 0,options:t.options?[...t.options]:void 0,minLength:t.minLength,maxLength:t.maxLength,minimum:t.minimum,maximum:t.maximum,pattern:t.pattern,patternDescription:t.patternDescription,format:t.format,description:t.itemDescription,fields:t.fields?Object.fromEntries(Object.entries(t.fields).map(([o,s])=>[o,xi(s)])):void 0},description:t.description,isOptional:t.isOptional||void 0,isInternal:t.isInternal||void 0,isCached:t.isCached||void 0};return n?this.inputFields.unshift(r):this.inputFields.push(r),this}output(e,t,n=!1){let r={name:e,type:{name:t.type,isArray:t.isArray||void 0,options:t.options?[...t.options]:void 0,minLength:t.minLength,maxLength:t.maxLength,minimum:t.minimum,maximum:t.maximum,pattern:t.pattern,patternDescription:t.patternDescription,format:t.format,description:t.itemDescription,fields:t.fields?Object.fromEntries(Object.entries(t.fields).map(([o,s])=>[o,xi(s)])):void 0},description:t.description,isOptional:t.isOptional||void 0,isInternal:t.isInternal||void 0};return n?this.outputFields.unshift(r):this.outputFields.push(r),this}addInputFields(e){for(let t of e)this.inputFields.push(t);return this}addOutputFields(e){for(let t of e)this.outputFields.push(t);return this}description(e){return this.desc=e,this}useStructured(){return this._useStructuredOutputs=!0,this}build(){let e={description:this.desc,inputs:this.inputFields,outputs:this.outputFields},t=new Oe(e);return this._useStructuredOutputs&&(t._forceComplexFields=!0,t._hasComplexFields=void 0),t}},Le=class i{type;isArray;options;description;isOptional;isInternal;isCached;fields;minLength;maxLength;minimum;maximum;pattern;patternDescription;format;itemDescription;constructor(e){this.type=e.type,this.isArray=e.isArray,this.options=e.options,this.description=e.description,this.itemDescription=e.itemDescription,this.isOptional=e.isOptional,this.isInternal=e.isInternal,this.isCached=e.isCached,this.fields=e.fields,this.minLength=e.minLength,this.maxLength=e.maxLength,this.minimum=e.minimum,this.maximum=e.maximum,this.pattern=e.pattern,this.patternDescription=e.patternDescription,this.format=e.format}optional(){return new i({...this,isOptional:!0})}array(e){return new i({...this,isArray:!0,description:e||this.description,itemDescription:e?this.description:void 0})}internal(){return new i({...this,isInternal:!0})}cache(){return new i({...this,isCached:!0})}min(e){return this.type==="string"?new i({...this,minLength:e}):this.type==="number"?new i({...this,minimum:e}):this}max(e){return this.type==="string"?new i({...this,maxLength:e}):this.type==="number"?new i({...this,maximum:e}):this}email(){return this.type==="string"?new i({...this,format:"email"}):this}url(){return this.type==="string"?new i({...this,format:"uri"}):this}regex(e,t){return this.type==="string"?new i({...this,pattern:e,patternDescription:t}):this}date(){return this.type==="string"?new i({...this,format:"date"}):this}datetime(){return this.type==="string"?new i({...this,format:"date-time"}):this}},fe=Object.assign(()=>new br,{string:i=>new Le({type:"string",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),number:i=>new Le({type:"number",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),boolean:i=>new Le({type:"boolean",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),json:i=>new Le({type:"json",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),datetime:i=>new Le({type:"datetime",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),date:i=>new Le({type:"date",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),class:(i,e)=>new Le({type:"class",isArray:!1,options:i,description:e,isOptional:!1,isInternal:!1,isCached:!1}),image:i=>new Le({type:"image",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),audio:i=>new Le({type:"audio",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),file:i=>new Le({type:"file",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),url:i=>new Le({type:"url",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),email:i=>new Le({type:"string",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1,format:"email"}),code:(i,e)=>new Le({type:"code",isArray:!1,description:e||i,isOptional:!1,isInternal:!1,isCached:!1}),object:(i,e)=>new Le({type:"object",isArray:!1,fields:i,description:e,isOptional:!1,isInternal:!1,isCached:!1})});function xi(i){return{type:i.type,isArray:i.isArray,options:i.options,description:i.description,isOptional:i.isOptional,isInternal:i.isInternal,minLength:i.minLength,maxLength:i.maxLength,minimum:i.minimum,maximum:i.maximum,pattern:i.pattern,patternDescription:i.patternDescription,format:i.format,fields:i.fields?Object.fromEntries(Object.entries(i.fields).map(([e,t])=>[e,xi(t)])):void 0}}function _o(i){return{type:{name:i.type,isArray:i.isArray,options:i.options?[...i.options]:void 0,fields:i.fields},description:i.description,isOptional:i.isOptional,isInternal:i.isInternal}}var te=class extends Error{constructor(t,n,r){super(t);this.fieldName=n;this.suggestion=r;this.name="AxSignatureValidationError"}},Oe=class i{description;inputFields;outputFields;sigHash;sigString;validatedAtHash;constructor(e){if(!e){this.inputFields=[],this.outputFields=[],this.sigHash="",this.sigString="";return}if(typeof e=="string"){let t;try{t=Ll(e)}catch(n){if(n instanceof Error){let r="suggestion"in n&&typeof n.suggestion=="string"?n.suggestion:'Please check the signature format. Example: "userInput:string -> responseText:string"';throw new te(`Invalid Signature: ${n.message}`,void 0,r)}throw new te(`Invalid Signature: ${e}`,void 0,'Please check the signature format. Example: "userInput:string -> responseText:string"')}this.description=t.desc,this.inputFields=t.inputs.map(n=>this.parseParsedField(n)),this.outputFields=t.outputs.map(n=>this.parseParsedField(n)),[this.sigHash,this.sigString]=this.updateHash()}else if(e instanceof i)this.description=e.getDescription(),this.inputFields=structuredClone(e.getInputFields()),this.outputFields=structuredClone(e.getOutputFields()),this.sigHash=e.hash(),this.sigString=e.toString(),e.validatedAtHash===this.sigHash&&(this.validatedAtHash=this.sigHash),this._forceComplexFields=e._forceComplexFields,this._hasComplexFields=e._hasComplexFields;else if(typeof e=="object"&&e!==null){if(!("inputs"in e)||!("outputs"in e))throw new te("Invalid signature object: missing inputs or outputs",void 0,'Signature object must have "inputs" and "outputs" arrays. Example: { inputs: [...], outputs: [...] }');if(!Array.isArray(e.inputs)||!Array.isArray(e.outputs))throw new te("Invalid signature object: inputs and outputs must be arrays",void 0,'Both "inputs" and "outputs" must be arrays of AxField objects');try{this.description=e.description,this.inputFields=e.inputs.map(t=>this.parseField(t)),this.outputFields=e.outputs.map(t=>this.parseField(t)),[this.sigHash,this.sigString]=this.updateHash()}catch(t){throw t instanceof te?t:new te(`Failed to create signature from object: ${t instanceof Error?t.message:"Unknown error"}`,void 0,"Check that all fields in inputs and outputs arrays are valid AxField objects")}}else throw new te("Invalid signature argument type",void 0,"Signature must be a string, another AxSignature instance, or an object with inputs and outputs arrays")}static create(e){return new i(e)}parseParsedField=e=>{if(!e.name||e.name.length===0)throw new te("Field name is required",e.name,'Every field must have a descriptive name. Example: "userInput", "responseText"');let t=this.toTitle(e.name);return{name:e.name,title:t,description:"desc"in e?e.desc:void 0,type:e.type??{name:"string",isArray:!1},..."isInternal"in e?{isInternal:e.isInternal}:{},..."isOptional"in e?{isOptional:e.isOptional}:{}}};parseField=e=>{let t=!e.title||e.title.length===0?this.toTitle(e.name):e.title;if(e.type&&(!e.type.name||e.type.name.length===0))throw new te("Field type name is required",e.name,"Specify a valid type. Available types: string, number, boolean, json, image, audio, file, url, date, datetime, class, code");return{...e,title:t}};setDescription=e=>{if(typeof e!="string")throw new te("Description must be a string",void 0,"Provide a string description for the signature");this.description=e,this.invalidateValidationCache(),this.updateHashLight()};addInputField=e=>{try{let t=this.parseField(e);bt(t,"input");for(let n of this.inputFields)if(n.name===t.name)throw new te(`Duplicate input field name: "${t.name}"`,t.name,"Each field name must be unique within the signature");for(let n of this.outputFields)if(n.name===t.name)throw new te(`Field name "${t.name}" appears in both inputs and outputs`,t.name,"Use different names for input and output fields to avoid confusion");this.inputFields.push(t),this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof te?t:new te(`Failed to add input field "${e.name}": ${t instanceof Error?t.message:"Unknown error"}`,e.name)}};addOutputField=e=>{try{let t=this.parseField(e);bt(t,"output");for(let n of this.outputFields)if(n.name===t.name)throw new te(`Duplicate output field name: "${t.name}"`,t.name,"Each field name must be unique within the signature");for(let n of this.inputFields)if(n.name===t.name)throw new te(`Field name "${t.name}" appears in both inputs and outputs`,t.name,"Use different names for input and output fields to avoid confusion");this.outputFields.push(t),this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof te?t:new te(`Failed to add output field "${e.name}": ${t instanceof Error?t.message:"Unknown error"}`,e.name)}};setInputFields=e=>{if(!Array.isArray(e))throw new te("Input fields must be an array",void 0,"Provide an array of field objects");try{let t=e.map(n=>{let r=this.parseField(n);return bt(r,"input"),r});this.inputFields=t,this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof te?t:new te(`Failed to set input fields: ${t instanceof Error?t.message:"Unknown error"}`)}};setOutputFields=e=>{if(!Array.isArray(e))throw new te("Output fields must be an array",void 0,"Provide an array of field objects");try{let t=e.map(n=>{let r=this.parseField(n);return bt(r,"output"),r});this.outputFields=t,this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof te?t:new te(`Failed to set output fields: ${t instanceof Error?t.message:"Unknown error"}`)}};getInputFields=()=>this.inputFields;getOutputFields=()=>this.outputFields;getDescription=()=>this.description;appendInputField=(e,t)=>{let n=new i(this);return n.addInputField({name:e,..._o(t)}),n};prependInputField=(e,t)=>{let n=new i(this),r={name:e,..._o(t)},o=n.parseField(r);bt(o,"input");for(let s of n.inputFields)if(s.name===o.name)throw new te(`Duplicate input field name: "${o.name}"`,o.name,"Each field name must be unique within the signature");for(let s of n.outputFields)if(s.name===o.name)throw new te(`Field name "${o.name}" appears in both inputs and outputs`,o.name,"Use different names for input and output fields to avoid confusion");return n.inputFields.unshift(o),n.invalidateValidationCache(),n.updateHashLight(),n};appendOutputField=(e,t)=>{let n=new i(this);return n.addOutputField({name:e,..._o(t)}),n};prependOutputField=(e,t)=>{let n=new i(this),r={name:e,..._o(t)},o=n.parseField(r);bt(o,"output");for(let s of n.outputFields)if(s.name===o.name)throw new te(`Duplicate output field name: "${o.name}"`,o.name,"Each field name must be unique within the signature");for(let s of n.inputFields)if(s.name===o.name)throw new te(`Field name "${o.name}" appears in both inputs and outputs`,o.name,"Use different names for input and output fields to avoid confusion");return n.outputFields.unshift(o),n.invalidateValidationCache(),n.updateHashLight(),n};invalidateValidationCache=()=>{this.validatedAtHash=void 0,this._hasComplexFields=void 0};toTitle=e=>{let t=e.replace(/_/g," ");return t=t.replace(/([A-Z]|[0-9]+)/g," $1").trim(),t.charAt(0).toUpperCase()+t.slice(1)};updateHashLight=()=>{try{return this.getInputFields().forEach(e=>{bt(e,"input")}),this.getOutputFields().forEach(e=>{bt(e,"output")}),this.sigHash=et("sha256").update(JSON.stringify(this.inputFields)).update(JSON.stringify(this.outputFields)).digest("hex"),this.sigString=Gl(this.description,this.inputFields,this.outputFields),this._hasComplexFields=this.computeHasComplexFields(),[this.sigHash,this.sigString]}catch(e){throw e instanceof te?e:new te(`Signature validation failed: ${e instanceof Error?e.message:"Unknown error"}`)}};updateHash=()=>{try{return this.getInputFields().forEach(e=>{bt(e,"input")}),this.getOutputFields().forEach(e=>{bt(e,"output")}),this.validateSignatureConsistency(),this.sigHash=et("sha256").update(this.description??"").update(JSON.stringify(this.inputFields)).update(JSON.stringify(this.outputFields)).digest("hex"),this.sigString=Gl(this.description,this.inputFields,this.outputFields),this._hasComplexFields=this.computeHasComplexFields(),[this.sigHash,this.sigString]}catch(e){throw e instanceof te?e:new te(`Signature validation failed: ${e instanceof Error?e.message:"Unknown error"}`)}};validateSignatureConsistency(){let e=new Set;for(let n of this.inputFields){if(e.has(n.name))throw new te(`Duplicate input field name: "${n.name}"`,n.name,"Each field name must be unique within the signature");e.add(n.name)}let t=new Set;for(let n of this.outputFields){if(t.has(n.name))throw new te(`Duplicate output field name: "${n.name}"`,n.name,"Each field name must be unique within the signature");t.add(n.name)}for(let n of this.outputFields)if(e.has(n.name))throw new te(`Field name "${n.name}" appears in both inputs and outputs`,n.name,"Use different names for input and output fields to avoid confusion");if(this.inputFields.length===0)throw new te("Signature must have at least one input field",void 0,'Add an input field. Example: "userInput:string -> ..."');if(this.outputFields.length===0)throw new te("Signature must have at least one output field",void 0,'Add an output field. Example: "... -> responseText:string"')}_forceComplexFields=!1;_hasComplexFields;hasComplexFields=()=>this._hasComplexFields!==void 0?this._hasComplexFields:(this._hasComplexFields=this.computeHasComplexFields(),this._hasComplexFields);computeHasComplexFields=()=>this._forceComplexFields?!0:this.outputFields.some(e=>e.type?.name==="object"||e.type?.isArray&&e.type.fields!==void 0);validate=()=>{if(this.validatedAtHash===this.sigHash)return!0;try{return this.updateHash(),this.validatedAtHash=this.sigHash,!0}catch(e){throw this.validatedAtHash=void 0,e}};hash=()=>this.sigHash;toString=()=>this.sigString;toJSON=()=>({id:this.hash(),description:this.description,inputFields:this.inputFields,outputFields:this.outputFields});toJSONSchema=()=>{let e=[...this.inputFields,...this.outputFields];return Tn(e,this.description??"Schema")};toInputJSONSchema=()=>Tn(this.inputFields,this.description??"Schema")};function $l(i){let e=i.name;return i.isOptional&&(e+="?"),i.isInternal&&(e+="!"),i.type&&(e+=`:${i.type.name}`,i.type.isArray&&(e+="[]"),i.type.name==="class"&&i.type.options&&(e+=` "${i.type.options.join(" | ")}"`)),i.description&&i.type?.name!=="class"&&(e+=` "${i.description}"`),e}function Gl(i,e,t){let n=i?`"${i}" `:"",r=e.map($l).join(", "),o=t.map($l).join(", ");return`${n}${r} -> ${o}`}function Nc(i){let e=/^[a-z][a-zA-Z0-9]*$/,t=/^[a-z]+(_[a-z0-9]+)*$/;return e.test(i)||t.test(i)}function bt(i,e){if(!i.name||i.name.length===0)throw new te("Field name cannot be blank",i.name,"Every field must have a descriptive name");if(!Nc(i.name))throw new te(`Invalid field name '${i.name}' - must be camelCase or snake_case`,i.name,'Use camelCase (e.g., "userInput") or snake_case (e.g., "user_input")');if(de.signatureStrict&&["text","object","image","string","number","boolean","json","array","datetime","date","time","type","class","input","output","data","value","result","response","request","item","element"].includes(i.name.toLowerCase())){let n=e==="input"?["userInput","questionText","documentContent","messageText","queryString"]:["responseText","analysisResult","categoryType","summaryText","outputData"];throw new te(`Field name '${i.name}' is too generic`,i.name,`Use a more descriptive name. Examples for ${e} fields: ${n.join(", ")}`)}if(i.name.length<2)throw new te(`Field name '${i.name}' is too short`,i.name,"Field names must be at least 2 characters long");if(i.name.length>50)throw new te(`Field name '${i.name}' is too long (${i.name.length} characters)`,i.name,"Field names should be 50 characters or less");i.type&&Lc(i,e)}function Lc(i,e){if(!i.type)return;let{type:t}=i;if((t.name==="image"||t.name==="audio"||t.name==="file")&&e==="output")throw new te(`${t.name} type is not supported in output fields`,i.name,`${t.name} types can only be used in input fields`);if(t.name==="class"){if(e==="input")throw new te("Class type is not supported in input fields",i.name,'Class types are only allowed on output fields. Use "string" type for input classifications');if(!t.options||t.options.length===0)throw new te("Class type requires options",i.name,'Provide class options. Example: class "positive, negative, neutral"');for(let r of t.options){if(!r||r.trim().length===0)throw new te("Empty class option found",i.name,"All class options must be non-empty strings");let o=r.trim();if(o.includes(",")||o.includes("|"))throw new te(`Invalid class option "${o}"`,i.name,"Class options cannot contain commas (,) or pipes (|) as they are used to separate options")}if(new Set(t.options.map(r=>r.trim().toLowerCase())).size!==t.options.length)throw new te("Duplicate class options found",i.name,"Each class option must be unique (case-insensitive)")}if(t.name==="code"&&t.isArray)throw new te("Arrays of code are not commonly supported",i.name,"Consider using a single code field or an array of strings instead");if(i.isInternal&&e==="input")throw new te("Internal marker (!) is not allowed on input fields",i.name,"Internal markers are only allowed on output fields");t.name==="object"&&t.fields&&Ai(t.fields,i.name,e)}function Ai(i,e,t,n=1){for(let[r,o]of Object.entries(i)){let s=`${e}.${r}`;if(o.type==="image"||o.type==="audio"||o.type==="file")throw new te(`${o.type} type is not allowed in nested object fields`,s,`Media types (image, audio, file) can only be used as top-level input fields, not within objects. Found at depth ${n}.`);o.type==="object"&&o.fields&&Ai(o.fields,s,t,n+1),o.isArray&&o.fields&&Ai(o.fields,`${s}[]`,t,n+1)}}var Pt=class i{signature;sigHash;examples;examplesOptions;demos;trace;usage=[];traceLabel;key;children;childNames=new Map;childCount=0;constructor(e,t){this.signature=new Oe(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 wn,this.key={id:"root"}}getSignature(){return new Oe(this.signature)}setSignature(e){this.signature=new Oe(e),e&&this.signature.validate(),this.updateSignatureHash()}setDescription(e){this.signature.setDescription(e),this.updateSignatureHash()}updateSignatureHash(){this.sigHash=this.signature.hash()}getId(){return this.key.id}register(e,t){let n=t??`p${this.childCount}`;this.childCount++,e.setId([this.key.id,n].join(".")),this.childNames.set(e,n),this.children.register(e)}setId(e){this.key={id:e,custom:!0};for(let[t,n]of this.childNames)t.setId([e,n].join("."))}setExamples(e,t){let n=[];if("programId"in e&&e.programId===this.key.id&&(n=e.traces),Array.isArray(e)&&(n=e),n){this.examplesOptions=t;let r=this.signature,o=[...r.getInputFields(),...r.getOutputFields()];this.examples=n.map(s=>{let a={};for(let l of o){let u=s[l.name];u!==void 0&&(gr(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 hr(e)}resetUsage(){this.usage=[];for(let e of Array.from(this.children))e?.resetUsage()}static _propagating=!1;setDemos(e,t){if(!i._propagating&&e.length>0){let u=new Set(this.namedPrograms().map(p=>p.id)),c=[...new Set(e.map(p=>p.programId))].filter(p=>!u.has(p));if(c.length>0){let p=[...u].join(", ");throw new Error(`Unknown program ID(s) in demos: ${c.join(", ")}. Valid IDs: ${p}. Use namedPrograms() to discover available IDs.`)}}let n=e.filter(u=>u.programId===this.key.id).map(u=>u.traces).flat(),r=this.signature,o=[...r.getInputFields(),...r.getOutputFields()],s=new Set(r.getInputFields().map(u=>u.name)),a=new Set(r.getOutputFields().map(u=>u.name));this.demos=n.map((u,c)=>{let p={};for(let g of o){let h=u[g.name];h!==void 0&&(gr(g,h),p[g.name]=h)}let d=Object.keys(p).some(g=>s.has(g));if(!Object.keys(p).some(g=>a.has(g)))throw new Error(`Demo trace[${c}] for '${this.key.id}' has no output field values. Expected at least one of: ${[...a].join(", ")}`);if(!d)throw new Error(`Demo trace[${c}] for '${this.key.id}' has no input field values. Expected at least one of: ${[...s].join(", ")}. Provide input context so the demo renders as a complete few-shot example.`);return p}),t?.modelConfig&&(this._optimizedModelConfig=t.modelConfig);let l=i._propagating;i._propagating=!0;try{for(let u of Array.from(this.children))u?.setDemos(e,t)}finally{i._propagating=l}}namedPrograms(){let e=[];[...this.signature.getInputFields(),...this.signature.getOutputFields()].length>0&&e.push({id:this.key.id,signature:this.signature.toString()});for(let n of Array.from(this.children))n&&"namedPrograms"in n&&typeof n.namedPrograms=="function"?e.push(...n.namedPrograms()):n&&e.push({id:n.getId()});return e}applyOptimization(e){this.setDemos(e.demos??[],{modelConfig:e.modelConfig})}};var Dl={"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.
@@ -307,12 +307,12 @@ You synthesize a final answer from the provided actorResult payload. The payload
307
307
  1. Base your answer ONLY on evidence from actorResult payload arguments.
308
308
  2. If actorResult lacks sufficient information, provide the best possible answer from available evidence.
309
309
  3. If actorResult.type is \`ask_clarification\`, ask for the missing information clearly in your output fields.
310
- `};var yi=/{{\s*([^}]+?)\s*}}/g,Ul=/^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*$/,Bl=new Map;function bt(i,e,t,n){let o=e.slice(0,t).split(`
311
- `),s=o.length,a=(o.at(-1)?.length??0)+1;return`${i}:${s}:${a} ${n}`}function Nc(i){let e=[],t=0;yi.lastIndex=0;let n=yi.exec(i);for(;n;){let[r,o]=n,s=n.index;s>t&&e.push({type:"text",value:i.slice(t,s)}),e.push({type:"tag",value:o.trim(),index:s}),t=s+r.length,n=yi.exec(i)}return t<i.length&&e.push({type:"text",value:i.slice(t)}),e}function bi(i,e,t,n=0,r=new Set){let o=[],s=n;for(;s<i.length;){let a=i[s];if(a.type==="text"){o.push({type:"text",value:a.value}),s++;continue}let l=a.value;if(r.has(l))return{nodes:o,nextIndex:s,terminator:l};if(l.startsWith("if ")){let u=l.slice(3).trim();if(!Ul.test(u))throw new Error(bt(t,e,a.index,`Invalid if condition '${u}'`));let c=bi(i,e,t,s+1,new Set(["else","/if"]));if(!c.terminator)throw new Error(bt(t,e,a.index,"Unclosed 'if' block"));let p=[],d=c.nextIndex+1;if(c.terminator==="else"){let m=bi(i,e,t,c.nextIndex+1,new Set(["/if"]));if(m.terminator!=="/if")throw new Error(bt(t,e,a.index,"Unclosed 'if' block"));p=m.nodes,d=m.nextIndex+1}o.push({type:"if",condition:u,thenNodes:c.nodes,elseNodes:p,index:a.index}),s=d;continue}if(l==="else")throw new Error(bt(t,e,a.index,"Unexpected 'else'"));if(l==="/if")throw new Error(bt(t,e,a.index,"Unexpected '/if'"));if(l.startsWith("include "))throw new Error(bt(t,e,a.index,"Unexpected 'include' directive at runtime (includes must be compiled)"));if(!Ul.test(l))throw new Error(bt(t,e,a.index,`Invalid tag '${l}'`));o.push({type:"var",name:l,index:a.index}),s++}return{nodes:o,nextIndex:s}}function jl(i,e,t,n,r){let o=e.split("."),s=i;for(let a of o){if(s===null||typeof s!="object"||!(a in s))throw new Error(bt(n,t,r,`Missing template variable '${e}'`));s=s[a]}return s}function Ii(i,e,t,n){let r="";for(let o of i){if(o.type==="text"){r+=o.value;continue}if(o.type==="var"){let a=jl(e,o.name,t,n,o.index);if(typeof a!="string"&&typeof a!="number"&&typeof a!="boolean")throw new Error(bt(n,t,o.index,`Variable '${o.name}' must be string, number, or boolean`));r+=String(a);continue}let s=jl(e,o.condition,t,n,o.index);if(typeof s!="boolean")throw new Error(bt(n,t,o.index,`Condition '${o.condition}' must be boolean`));s?r+=Ii(o.thenNodes,e,t,n):r+=Ii(o.elseNodes,e,t,n)}return r}function Lc(i,e){let t=Nc(i),n=bi(t,i,e);if(n.terminator)throw new Error(`Unexpected template terminator '${n.terminator}' in ${e}`);return n.nodes}function ut(i,e={}){let t=Dl[i],n=`template:${i}`;if(!t)throw new Error(`Unknown template id: ${String(i)}`);let r=Bl.get(i);return r||(r=Lc(t,n),Bl.set(i,r)),Ii(r,e,t,n)}var ql=ut("dsp/function-call-instructions.md"),$c=ut("dsp/strict-output-formatting-rules.md"),Gc=ut("dsp/example-disclaimer.md"),Sn=ut("dsp/example-separator.md"),Ut=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,ce.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=No(this.sig.getInputFields()),r=No(this.sig.getOutputFields());e.push(`You will be provided with the following fields: ${n}. Your task is to generate new fields: ${r}.`);let s=this.functions?.map(c=>"toFunction"in c?c.toFunction():c)?.flat()?.map(c=>`- \`${c.name}\`: ${vn(c.description)}`).join(`
310
+ `};var yi=/{{\s*([^}]+?)\s*}}/g,Ul=/^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*$/,Bl=new Map;function It(i,e,t,n){let o=e.slice(0,t).split(`
311
+ `),s=o.length,a=(o.at(-1)?.length??0)+1;return`${i}:${s}:${a} ${n}`}function $c(i){let e=[],t=0;yi.lastIndex=0;let n=yi.exec(i);for(;n;){let[r,o]=n,s=n.index;s>t&&e.push({type:"text",value:i.slice(t,s)}),e.push({type:"tag",value:o.trim(),index:s}),t=s+r.length,n=yi.exec(i)}return t<i.length&&e.push({type:"text",value:i.slice(t)}),e}function bi(i,e,t,n=0,r=new Set){let o=[],s=n;for(;s<i.length;){let a=i[s];if(a.type==="text"){o.push({type:"text",value:a.value}),s++;continue}let l=a.value;if(r.has(l))return{nodes:o,nextIndex:s,terminator:l};if(l.startsWith("if ")){let u=l.slice(3).trim();if(!Ul.test(u))throw new Error(It(t,e,a.index,`Invalid if condition '${u}'`));let c=bi(i,e,t,s+1,new Set(["else","/if"]));if(!c.terminator)throw new Error(It(t,e,a.index,"Unclosed 'if' block"));let p=[],d=c.nextIndex+1;if(c.terminator==="else"){let m=bi(i,e,t,c.nextIndex+1,new Set(["/if"]));if(m.terminator!=="/if")throw new Error(It(t,e,a.index,"Unclosed 'if' block"));p=m.nodes,d=m.nextIndex+1}o.push({type:"if",condition:u,thenNodes:c.nodes,elseNodes:p,index:a.index}),s=d;continue}if(l==="else")throw new Error(It(t,e,a.index,"Unexpected 'else'"));if(l==="/if")throw new Error(It(t,e,a.index,"Unexpected '/if'"));if(l.startsWith("include "))throw new Error(It(t,e,a.index,"Unexpected 'include' directive at runtime (includes must be compiled)"));if(!Ul.test(l))throw new Error(It(t,e,a.index,`Invalid tag '${l}'`));o.push({type:"var",name:l,index:a.index}),s++}return{nodes:o,nextIndex:s}}function jl(i,e,t,n,r){let o=e.split("."),s=i;for(let a of o){if(s===null||typeof s!="object"||!(a in s))throw new Error(It(n,t,r,`Missing template variable '${e}'`));s=s[a]}return s}function Ii(i,e,t,n){let r="";for(let o of i){if(o.type==="text"){r+=o.value;continue}if(o.type==="var"){let a=jl(e,o.name,t,n,o.index);if(typeof a!="string"&&typeof a!="number"&&typeof a!="boolean")throw new Error(It(n,t,o.index,`Variable '${o.name}' must be string, number, or boolean`));r+=String(a);continue}let s=jl(e,o.condition,t,n,o.index);if(typeof s!="boolean")throw new Error(It(n,t,o.index,`Condition '${o.condition}' must be boolean`));s?r+=Ii(o.thenNodes,e,t,n):r+=Ii(o.elseNodes,e,t,n)}return r}function Gc(i,e){let t=$c(i),n=bi(t,i,e);if(n.terminator)throw new Error(`Unexpected template terminator '${n.terminator}' in ${e}`);return n.nodes}function lt(i,e={}){let t=Dl[i],n=`template:${i}`;if(!t)throw new Error(`Unknown template id: ${String(i)}`);let r=Bl.get(i);return r||(r=Gc(t,n),Bl.set(i,r)),Ii(r,e,t,n)}var ql=lt("dsp/function-call-instructions.md"),Dc=lt("dsp/strict-output-formatting-rules.md"),Uc=lt("dsp/example-disclaimer.md"),Rn=lt("dsp/example-separator.md"),Ut=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,de.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=No(this.sig.getInputFields()),r=No(this.sig.getOutputFields());e.push(`You will be provided with the following fields: ${n}. Your task is to generate new fields: ${r}.`);let s=this.functions?.map(c=>"toFunction"in c?c.toFunction():c)?.flat()?.map(c=>`- \`${c.name}\`: ${Sn(c.description)}`).join(`
312
312
  `);s&&s.length>0&&e.push(`## Available Functions
313
313
  ${s}`);let a=this.getFieldNameToTitleMap(),l=zl(this.sig.getInputFields(),a);if(e.push(`## Input Fields
314
314
  ${l}`),!t){let c=Hl(this.sig.getOutputFields(),a);e.push(`## Output Fields
315
- ${c}`)}s&&s.length>0&&e.push(ql.trim()),t&&this.structuredOutputFunctionName?e.push(ut("dsp/legacy-formatting-rules-structured-function.md",{structuredOutputFunctionName:this.structuredOutputFunctionName}).trim()):t||e.push($c.trim());let u=this.sig.getDescription();if(u){let c=vn(u);c=Lo(c,a),e.push(c)}return{type:"text",text:e.join(`
315
+ ${c}`)}s&&s.length>0&&e.push(ql.trim()),t&&this.structuredOutputFunctionName?e.push(lt("dsp/legacy-formatting-rules-structured-function.md",{structuredOutputFunctionName:this.structuredOutputFunctionName}).trim()):t||e.push(Dc.trim());let u=this.sig.getDescription();if(u){let c=Sn(u);c=Lo(c,a),e.push(c)}return{type:"text",text:e.join(`
316
316
 
317
317
  `)}}buildStructuredPrompt(){let e=[],t=this.sig.hasComplexFields();e.push("<identity>"),e.push(this.buildIdentitySection()),e.push("</identity>");let n=this.buildTaskDefinitionSection();n&&(e.push(`
318
318
  <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(`
@@ -320,32 +320,32 @@ ${c}`)}s&&s.length>0&&e.push(ql.trim()),t&&this.structuredOutputFunctionName?e.p
320
320
  <input_fields>`),e.push(this.buildInputFieldsSection()),e.push("</input_fields>"),t||(e.push(`
321
321
  <output_fields>`),e.push(this.buildOutputFieldsSection()),e.push("</output_fields>")),e.push(`
322
322
  <formatting_rules>`),e.push(this.buildFormattingRulesSection()),e.push("</formatting_rules>"),{type:"text",text:e.join(`
323
- `)}}buildIdentitySection(){let e=No(this.sig.getInputFields()),t=No(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=vn(e);return n=Lo(n,t),n}buildFunctionsSection(e){let t=[];t.push(`**Available Functions**: You can call the following functions to complete the task:
324
- `);let n=e.map(r=>`- \`${r.name}\`: ${vn(r.description??"")}`).join(`
323
+ `)}}buildIdentitySection(){let e=No(this.sig.getInputFields()),t=No(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=Sn(e);return n=Lo(n,t),n}buildFunctionsSection(e){let t=[];t.push(`**Available Functions**: You can call the following functions to complete the task:
324
+ `);let n=e.map(r=>`- \`${r.name}\`: ${Sn(r.description??"")}`).join(`
325
325
  `);return t.push(n),t.push(`
326
326
  ${ql.trim()}`),t.join(`
327
327
  `)}buildInputFieldsSection(){let e=this.getFieldNameToTitleMap();return`**Input Fields**: The following fields will be provided to you:
328
328
 
329
329
  ${zl(this.sig.getInputFields(),e)}`}buildOutputFieldsSection(){let e=this.getFieldNameToTitleMap();return`**Output Fields**: You must generate the following fields:
330
330
 
331
- ${Hl(this.sig.getOutputFields(),e)}`}buildFormattingRulesSection(){let e=this.sig.hasComplexFields();return e&&this.structuredOutputFunctionName?ut("dsp/formatting-rules-structured-function.md",{structuredOutputFunctionName:this.structuredOutputFunctionName}).trim():e?ut("dsp/formatting-rules-structured-json.md").trim():ut("dsp/formatting-rules-plain-text.md").trim()}renderSingleValueUserContent=(e,t,n,r)=>{let o=this.renderInputFields(e),s;if(r)s=o;else{let l=[...t,...n];if(this.contextCache&&l.length>0){let u=l.length-1,c=l[u];c&&(l[u]={...c,cache:!0})}s=[...l,...o]}let a=s.filter(l=>l!==void 0);return a.every(l=>l.type==="text")?a.map(l=>l.text).join(`
331
+ ${Hl(this.sig.getOutputFields(),e)}`}buildFormattingRulesSection(){let e=this.sig.hasComplexFields();return e&&this.structuredOutputFunctionName?lt("dsp/formatting-rules-structured-function.md",{structuredOutputFunctionName:this.structuredOutputFunctionName}).trim():e?lt("dsp/formatting-rules-structured-json.md").trim():lt("dsp/formatting-rules-plain-text.md").trim()}renderSingleValueUserContent=(e,t,n,r)=>{let o=this.renderInputFields(e),s;if(r)s=o;else{let l=[...t,...n];if(this.contextCache&&l.length>0){let u=l.length-1,c=l[u];c&&(l[u]={...c,cache:!0})}s=[...l,...o]}let a=s.filter(l=>l!==void 0);return a.every(l=>l.type==="text")?a.map(l=>l.text).join(`
332
332
  `):a.reduce(Dt(`
333
333
  `),[])};render=(e,{examples:t,demos:n})=>{if(!this.examplesInSystem)return this.renderWithMessagePairs(e,{examples:t,demos:n});let r=t?[{type:"text",text:`
334
334
 
335
335
  ## Examples
336
336
  `},...this.renderExamples(t)]:[],o=n?this.renderDemos(n):[],s=r.every(d=>d.type==="text"),a=o.every(d=>d.type==="text"),l=s&&a,u=this.task.text;if(l){let d=[{type:"text",text:u},...r,...o];d.reduce(Dt(""),[]),d?.[0]&&(u=d[0].text)}let c={role:"system",content:u,cache:!!this.contextCache};if(Array.isArray(e)){let d=[],m=e,g=!0;for(let h of m){let f;if(g?(f=this.renderSingleValueUserContent(h.values,r,o,l),g=!1):f=this.renderSingleValueUserContent(h.values,[],[],!1),h.role==="user"){d.push({role:"user",content:f});continue}if(h.role!=="assistant")throw new Error("Invalid message role");if(typeof f!="string")throw new Error("Assistant message cannot contain non-text content like images, files,etc");d.push({role:"assistant",content:f})}return[c,...d]}let p=this.renderSingleValueUserContent(e,r,o,l);return[c,{role:"user",content:p}]};renderWithMessagePairs=(e,{examples:t,demos:n})=>{let r=t&&t.length>0||n&&n.length>0,s={role:"system",content:r?`${this.task.text}
337
- ${Gc}`:this.task.text,cache:!!this.contextCache},a=t?this.renderExamplesAsMessages(t):[],l=n?this.renderDemosAsMessages(n):[],u=[];for(let I of[...a,...l])u.push(I.userMessage),u.push(I.assistantMessage),I.functionResultMessage&&u.push(I.functionResultMessage);let c=this.contextCache?.cacheBreakpoint??"after-examples",p=this.ignoreBreakpoints||c==="after-examples";if(this.contextCache&&u.length>0&&p){let I=u.length-1,T=u[I];T?.role==="assistant"&&(u[I]={...T,cache:!0})}if(Array.isArray(e)){let I=[],T=e,v=!0;for(let F of T){let E=this.renderInputFields(F.values),D=E.every(U=>U.type==="text")?E.map(U=>U.text).join(`
338
- `):E.reduce(Dt(`
339
- `),[]);if(F.role==="user"){v&&r&&(typeof D=="string"?D=Sn+D:D=[{type:"text",text:Sn},...D],v=!1),I.push({role:"user",content:D});continue}if(F.role!=="assistant")throw new Error("Invalid message role");if(typeof D!="string")throw new Error("Assistant message cannot contain non-text content like images, files, etc");I.push({role:"assistant",content:D})}return[s,...u,...I]}let d=this.sig.getInputFields(),m=d.filter(I=>I.isCached),g=d.filter(I=>!I.isCached),h=m.length>0;if(this.contextCache&&h&&(this.ignoreBreakpoints||c!=="system"&&c!=="after-functions")&&g.length>0){let I=m.map(E=>this.renderInField(E,e,void 0)).filter(E=>E!==void 0).flat();I.filter(E=>E.type==="text").forEach(E=>{E.text=`${E.text}
340
- `});let T=I.every(E=>E.type==="text")?I.map(E=>E.text).join(`
341
- `):I.reduce(Dt(`
342
- `),[]);r&&(typeof T=="string"?T=Sn+T:T=[{type:"text",text:Sn},...T]);let v=g.map(E=>this.renderInField(E,e,void 0)).filter(E=>E!==void 0).flat();v.filter(E=>E.type==="text").forEach(E=>{E.text=`${E.text}
343
- `});let F=v.every(E=>E.type==="text")?v.map(E=>E.text).join(`
337
+ ${Uc}`:this.task.text,cache:!!this.contextCache},a=t?this.renderExamplesAsMessages(t):[],l=n?this.renderDemosAsMessages(n):[],u=[];for(let b of[...a,...l])u.push(b.userMessage),u.push(b.assistantMessage),b.functionResultMessage&&u.push(b.functionResultMessage);let c=this.contextCache?.cacheBreakpoint??"after-examples",p=this.ignoreBreakpoints||c==="after-examples";if(this.contextCache&&u.length>0&&p){let b=u.length-1,T=u[b];T?.role==="assistant"&&(u[b]={...T,cache:!0})}if(Array.isArray(e)){let b=[],T=e,v=!0;for(let F of T){let O=this.renderInputFields(F.values),D=O.every(B=>B.type==="text")?O.map(B=>B.text).join(`
338
+ `):O.reduce(Dt(`
339
+ `),[]);if(F.role==="user"){v&&r&&(typeof D=="string"?D=Rn+D:D=[{type:"text",text:Rn},...D],v=!1),b.push({role:"user",content:D});continue}if(F.role!=="assistant")throw new Error("Invalid message role");if(typeof D!="string")throw new Error("Assistant message cannot contain non-text content like images, files, etc");b.push({role:"assistant",content:D})}return[s,...u,...b]}let d=this.sig.getInputFields(),m=d.filter(b=>b.isCached),g=d.filter(b=>!b.isCached),h=m.length>0;if(this.contextCache&&h&&(this.ignoreBreakpoints||c!=="system"&&c!=="after-functions")&&g.length>0){let b=m.map(O=>this.renderInField(O,e,void 0)).filter(O=>O!==void 0).flat();b.filter(O=>O.type==="text").forEach(O=>{O.text=`${O.text}
340
+ `});let T=b.every(O=>O.type==="text")?b.map(O=>O.text).join(`
341
+ `):b.reduce(Dt(`
342
+ `),[]);r&&(typeof T=="string"?T=Rn+T:T=[{type:"text",text:Rn},...T]);let v=g.map(O=>this.renderInField(O,e,void 0)).filter(O=>O!==void 0).flat();v.filter(O=>O.type==="text").forEach(O=>{O.text=`${O.text}
343
+ `});let F=v.every(O=>O.type==="text")?v.map(O=>O.text).join(`
344
344
  `):v.reduce(Dt(`
345
- `),[]);return[s,...u,{role:"user",content:T,cache:!0},{role:"user",content:F}]}let x=this.sortFieldsCachedFirst(d).map(I=>this.renderInField(I,e,void 0)).filter(I=>I!==void 0).flat();x.filter(I=>I.type==="text").forEach(I=>{I.text=`${I.text}
346
- `});let A=x.every(I=>I.type==="text")?x.map(I=>I.text).join(`
345
+ `),[]);return[s,...u,{role:"user",content:T,cache:!0},{role:"user",content:F}]}let x=this.sortFieldsCachedFirst(d).map(b=>this.renderInField(b,e,void 0)).filter(b=>b!==void 0).flat();x.filter(b=>b.type==="text").forEach(b=>{b.text=`${b.text}
346
+ `});let A=x.every(b=>b.type==="text")?x.map(b=>b.text).join(`
347
347
  `):x.reduce(Dt(`
348
- `),[]);r&&(typeof A=="string"?A=Sn+A:A=[{type:"text",text:Sn},...A]);let w=h&&g.length===0&&this.contextCache;return[s,...u,{role:"user",content:A,...w?{cache:!0}:{}}]};renderExtraFields=e=>{let t=[];if(!e||e.length===0)return t;let n=e.reduce((s,a)=>{let l=a.title;return s[l]||(s[l]=[]),s[l].push(a),s},{}),r=this.sig.hasComplexFields();return Object.entries(n).map(([s,a])=>{if(a.length===1){let l=a[0];return l.type?.name==="object"||l.type?.isArray&&l.type.fields?{title:s,name:l.name,description:`${l.description}
348
+ `),[]);r&&(typeof A=="string"?A=Rn+A:A=[{type:"text",text:Rn},...A]);let w=h&&g.length===0&&this.contextCache;return[s,...u,{role:"user",content:A,...w?{cache:!0}:{}}]};renderExtraFields=e=>{let t=[];if(!e||e.length===0)return t;let n=e.reduce((s,a)=>{let l=a.title;return s[l]||(s[l]=[]),s[l].push(a),s},{}),r=this.sig.hasComplexFields();return Object.entries(n).map(([s,a])=>{if(a.length===1){let l=a[0];return l.type?.name==="object"||l.type?.isArray&&l.type.fields?{title:s,name:l.name,description:`${l.description}
349
349
  IMPORTANT: Provide the FULL JSON object for this field, matching the schema exactly.`}:{title:s,name:l.name,description:l.description}}if(a.length>1){let l=a.map(u=>`- ${u.description}`).join(`
350
350
  `);return{title:s,name:a[0].name,description:l}}}).filter(Boolean).forEach(s=>{let a=this.fieldTemplates?.[s.name]??this.defaultRenderInField;t.push(...a(s,s.description))}),t};renderExamples=e=>{let t=[],n={isExample:!0},r=this.sig.hasComplexFields();for(let[o,s]of e.entries()){if(r){let c=this.sig.getInputFields().map(h=>this.renderInField(h,s,{...n,isInputField:!0})).filter(h=>h!==void 0).flat(),p=this.sig.getOutputFields(),d={};for(let h of p)h.name in s&&(d[h.name]=s[h.name]);let m=JSON.stringify(d,null,2),g=[...c,{type:"text",text:`\`\`\`json
351
351
  ${m}
@@ -365,20 +365,20 @@ ${m}
365
365
  `):a.reduce(Dt(`
366
366
  `),[]);if(r&&this.structuredOutputFunctionName){let d={};for(let h of this.sig.getOutputFields())h.name in o&&(d[h.name]=o[h.name]);if(typeof l=="string"&&l.trim()===""||Array.isArray(l)&&l.length===0||Object.keys(d).length===0)continue;let g=`example-${t.length}`;t.push({userMessage:{role:"user",content:l},assistantMessage:{role:"assistant",functionCalls:[{id:g,type:"function",function:{name:this.structuredOutputFunctionName,params:d}}]},functionResultMessage:{role:"function",result:"done",functionId:g}});continue}let u;if(r){let d={};for(let m of this.sig.getOutputFields())m.name in o&&(d[m.name]=o[m.name]);u=JSON.stringify(d,null,2)}else u=this.sig.getOutputFields().map(m=>this.renderInField(m,o,{...n,isInputField:!1})).filter(m=>m!==void 0).flat().filter(m=>m.type==="text").map(m=>m.text).join(`
367
367
  `);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}
368
- `}),n};renderInField=(e,t,n)=>{let r=t[e.name];if(Bc(e,r,n))return;e.type&&hr(e,r);let o=Dc(e,r);return(this.fieldTemplates?.[e.name]??this.defaultRenderInField)(e,o)};defaultRenderInField=(e,t)=>{if(e.type?.name==="image"){let r=s=>{if(!s)throw new Error("Image field value is required.");if(typeof s!="object")throw new Error("Image field value must be an object.");if(!("mimeType"in s))throw new Error("Image field must have mimeType");if(!("data"in s))throw new Error("Image field must have data");return s},o=[{type:"text",text:`${e.title}: `}];if(e.type.isArray){if(!Array.isArray(t))throw new Error("Image field value must be an array.");o=o.concat(t.map(s=>{let a=r(s);return{type:"image",mimeType:a.mimeType,image:a.data}}))}else{let s=r(t);o.push({type:"image",mimeType:s.mimeType,image:s.data})}return o}if(e.type?.name==="audio"){let r=s=>{if(!s)throw new Error("Audio field value is required.");if(typeof s!="object")throw new Error("Audio field value must be an object.");if(!("data"in s))throw new Error("Audio field must have data");return s},o=[{type:"text",text:`${e.title}: `}];if(e.type.isArray){if(!Array.isArray(t))throw new Error("Audio field value must be an array.");o=o.concat(t.map(s=>{let a=r(s);return{type:"audio",format:a.format??"wav",data:a.data}}))}else{let s=r(t);o.push({type:"audio",format:s.format??"wav",data:s.data})}return o}if(e.type?.name==="file"){let r=s=>{if(!s)throw new Error("File field value is required.");if(typeof s!="object")throw new Error("File field value must be an object.");if(!("mimeType"in s))throw new Error("File field must have mimeType");let a="data"in s,l="fileUri"in s;if(!a&&!l)throw new Error("File field must have either data or fileUri");if(a&&l)throw new Error("File field cannot have both data and fileUri");return s},o=[{type:"text",text:`${e.title}: `}];if(e.type.isArray){if(!Array.isArray(t))throw new Error("File field value must be an array.");o=o.concat(t.map(s=>{let a=r(s);return"fileUri"in a?{type:"file",mimeType:a.mimeType,fileUri:a.fileUri}:{type:"file",mimeType:a.mimeType,data:a.data}}))}else{let s=r(t);o.push("fileUri"in s?{type:"file",mimeType:s.mimeType,fileUri:s.fileUri}:{type:"file",mimeType:s.mimeType,data:s.data})}return o}if(e.type?.name==="url"){let r=s=>{if(!s)throw new Error("URL field value is required.");if(typeof s=="string")return{url:s};if(typeof s!="object")throw new Error("URL field value must be a string or object.");if(!("url"in s))throw new Error("URL field must have url property");return s},o=[{type:"text",text:`${e.title}: `}];if(e.type.isArray){if(!Array.isArray(t))throw new Error("URL field value must be an array.");o=o.concat(t.map(s=>{let a=r(s);return{type:"url",url:a.url,...a.title?{title:a.title}:{},...a.description?{description:a.description}:{}}}))}else{let s=r(t);o.push({type:"url",url:s.url,...s.title?{title:s.title}:{},...s.description?{description:s.description}:{}})}return o}let n=[e.title,": "];return Array.isArray(t)?(n.push(`
368
+ `}),n};renderInField=(e,t,n)=>{let r=t[e.name];if(qc(e,r,n))return;e.type&&gr(e,r);let o=Bc(e,r);return(this.fieldTemplates?.[e.name]??this.defaultRenderInField)(e,o)};defaultRenderInField=(e,t)=>{if(e.type?.name==="image"){let r=s=>{if(!s)throw new Error("Image field value is required.");if(typeof s!="object")throw new Error("Image field value must be an object.");if(!("mimeType"in s))throw new Error("Image field must have mimeType");if(!("data"in s))throw new Error("Image field must have data");return s},o=[{type:"text",text:`${e.title}: `}];if(e.type.isArray){if(!Array.isArray(t))throw new Error("Image field value must be an array.");o=o.concat(t.map(s=>{let a=r(s);return{type:"image",mimeType:a.mimeType,image:a.data}}))}else{let s=r(t);o.push({type:"image",mimeType:s.mimeType,image:s.data})}return o}if(e.type?.name==="audio"){let r=s=>{if(!s)throw new Error("Audio field value is required.");if(typeof s!="object")throw new Error("Audio field value must be an object.");if(!("data"in s))throw new Error("Audio field must have data");return s},o=[{type:"text",text:`${e.title}: `}];if(e.type.isArray){if(!Array.isArray(t))throw new Error("Audio field value must be an array.");o=o.concat(t.map(s=>{let a=r(s);return{type:"audio",format:a.format??"wav",data:a.data}}))}else{let s=r(t);o.push({type:"audio",format:s.format??"wav",data:s.data})}return o}if(e.type?.name==="file"){let r=s=>{if(!s)throw new Error("File field value is required.");if(typeof s!="object")throw new Error("File field value must be an object.");if(!("mimeType"in s))throw new Error("File field must have mimeType");let a="data"in s,l="fileUri"in s;if(!a&&!l)throw new Error("File field must have either data or fileUri");if(a&&l)throw new Error("File field cannot have both data and fileUri");return s},o=[{type:"text",text:`${e.title}: `}];if(e.type.isArray){if(!Array.isArray(t))throw new Error("File field value must be an array.");o=o.concat(t.map(s=>{let a=r(s);return"fileUri"in a?{type:"file",mimeType:a.mimeType,fileUri:a.fileUri}:{type:"file",mimeType:a.mimeType,data:a.data}}))}else{let s=r(t);o.push("fileUri"in s?{type:"file",mimeType:s.mimeType,fileUri:s.fileUri}:{type:"file",mimeType:s.mimeType,data:s.data})}return o}if(e.type?.name==="url"){let r=s=>{if(!s)throw new Error("URL field value is required.");if(typeof s=="string")return{url:s};if(typeof s!="object")throw new Error("URL field value must be a string or object.");if(!("url"in s))throw new Error("URL field must have url property");return s},o=[{type:"text",text:`${e.title}: `}];if(e.type.isArray){if(!Array.isArray(t))throw new Error("URL field value must be an array.");o=o.concat(t.map(s=>{let a=r(s);return{type:"url",url:a.url,...a.title?{title:a.title}:{},...a.description?{description:a.description}:{}}}))}else{let s=r(t);o.push({type:"url",url:s.url,...s.title?{title:s.title}:{},...s.description?{description:s.description}:{}})}return o}let n=[e.title,": "];return Array.isArray(t)?(n.push(`
369
369
  `),n.push(t.map(r=>`- ${r}`).join(`
370
- `))):n.push(t),[{type:"text",text:n.join("")}]}},No=i=>i.map(e=>`\`${e.title}\``).join(", "),zl=(i,e)=>i.map(n=>{let r=n.title,o=n.type?.name?kn(n.type):"string",s=n.isOptional?`This optional ${o} field may be omitted`:`${/^[aeiou]/i.test(o)?"An":"A"} ${o} field`,a="";if(n.description){let l=vn(n.description);e&&(l=Lo(l,e)),a=` ${l}`}return`${r}: (${s})${a}`.trim()}).join(`
371
- `),Hl=(i,e)=>i.map(n=>{let r=n.title,o=n.type?.name?kn(n.type):"string",s=n.isOptional?`Only include this ${o} field if its value is available`:`This ${o} field must be included`,a="";if(n.description&&n.description.length>0){let l=n.type?.name==="class"?n.description:vn(n.description);e&&(l=Lo(l,e)),a=` ${l}`}return n.type?.options&&n.type.options.length>0&&(a.length>0&&(a+=". "),a+=`Allowed values: ${n.type.options.join(", ")}`),`${r}: (${s})${a}`.trim()}).join(`
372
- `),Dc=(i,e)=>{if(i.type?.name==="date"&&e instanceof Date){let t=e.toISOString();return t.slice(0,t.indexOf("T"))}return i.type?.name==="datetime"&&e instanceof Date?Tl(e):i.type?.name==="image"&&typeof e=="object"||i.type?.name==="audio"&&typeof e=="object"||i.type?.name==="file"&&typeof e=="object"||i.type?.name==="url"&&(typeof e=="string"||typeof e=="object")||typeof e=="string"?e:JSON.stringify(e,null,2)};function Uc(i){return`{ ${Object.entries(i).map(([t,n])=>{let r=n.isOptional?"?":"",o=kn({name:n.type,isArray:n.isArray,fields:n.fields,options:n.options});return`${t}${r}: ${o}`}).join(", ")} }`}var kn=i=>{let e=(()=>{switch(i?.name){case"string":return"string";case"number":return"number";case"boolean":return"boolean (true or false)";case"date":return'date ("YYYY-MM-DD" format)';case"datetime":return'date time ("YYYY-MM-DD HH:mm Timezone" format)';case"json":return"JSON object";case"class":return"classification class";case"code":return"code";case"file":return"file (with filename, mimeType, and data)";case"url":return"URL (string or object with url, title, description)";case"object":return i?.fields?`object ${Uc(i.fields)}`:"object";default:return"string"}})();return i?.isArray?`json array of ${e} items`:e};function Dt(i){return(e,t)=>{if(t.type==="text"){let n=e.length>0?e[e.length-1]:null;n&&n.type==="text"?n.text+=i+t.text:e.push(t)}else e.push(t);return e}}var Bc=(i,e,t)=>{if(typeof e=="boolean"||i?.type?.name==="number"&&typeof e=="number")return!1;if(!e||(Array.isArray(e)||typeof e=="string")&&e.length===0){if(t?.isExample||i.isOptional||i.isInternal)return!0;let n=t?.isInputField!==!1?"input":"output";throw new Error(`Value for ${n} field '${i.name}' is required.`)}return!1};function vn(i){let e=i.trim();return e.length>0?`${e.charAt(0).toUpperCase()}${e.slice(1)}${e.endsWith(".")?"":"."}`:""}function Lo(i,e){if(e.size===0)return i;let t=i,n=Array.from(e.keys()).sort((r,o)=>o.length-r.length);for(let r of n){let o=e.get(r),s=new RegExp(`\`${r}\``,"g");t=t.replace(s,`\`${o}\``);let a=new RegExp(`"${r}"`,"g");t=t.replace(a,`"${o}"`);let l=new RegExp(`'${r}'`,"g");t=t.replace(l,`'${o}'`);let u=new RegExp(`\\[${r}\\]`,"g");t=t.replace(u,`[${o}]`);let c=new RegExp(`\\(${r}\\)`,"g");t=t.replace(c,`(${o})`);let p=new RegExp(`\\$${r}\\b`,"g");t=t.replace(p,`\`${o}\``)}return t}function jc(i,e){let t=i.history(0,e),n=t.some(o=>o.role==="function");return t.some(o=>o.role==="assistant"&&"functionCalls"in o&&Array.isArray(o.functionCalls)&&o.functionCalls.length>0)&&n}function qc(i,e){let t=i.history(0,e),n=[],r=t.filter(s=>s.role==="assistant"&&"functionCalls"in s&&Array.isArray(s.functionCalls)&&s.functionCalls.length>0),o=t.filter(s=>s.role==="function");for(let s of r)if("functionCalls"in s&&s.functionCalls)for(let a of s.functionCalls){let l=o.find(u=>"functionId"in u&&u.functionId===a.id);l&&"result"in l&&"functionId"in l&&n.push({index:n.length,functionName:a.function.name,functionId:a.id,args:a.function.params||"",result:String(l.result),isError:"isError"in l?!!l.isError:!1})}return n}async function $o(i,e,t,n){if(!e?.resultPicker||i.length<=1)return 0;let r=e.resultPicker;if((t?jc(t,n):!1)&&t){let l=qc(t,n),u=await r({type:"function",results:l});if(u<0||u>=l.length)throw new Error(`Result picker returned invalid index: ${u}. Must be between 0 and ${l.length-1}`);return u}let s=i.map((l,u)=>({index:u,sample:l.delta})),a=await r({type:"fields",results:s});if(a<0||a>=i.length)throw new Error(`Result picker returned invalid index: ${a}. Must be between 0 and ${i.length-1}`);return a}async function Kl(i,e,t){let n=i?.getLast(e);if(!n||n.role!=="assistant"||n.chat.length<=1)return 0;let r=n.chat.map(s=>({version:0,index:s.index,delta:s.value}));return await $o(r,t,i,e)}var On=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 Go=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}=zc(t);e.set(t.name,n)}else e.set(t.name,new Map);return e}async route(e,t){let n=[],r={},o=new Map,s=new Map;for(let[a,l]of this.tools.entries())s.set(a,this.buildSanitizedFieldMap(l));for(let[a,l]of Object.entries(e)){let u=this.tools.get(this.normalizeToolName(a));if(u){l!=null&&typeof l=="object"&&o.set(u.name,l);continue}r[a]=l}for(let[a,l]of Object.entries(e))for(let[u,c]of this.tools.entries()){let p=s.get(u);if(!p)continue;let d=p.get(a);if(!d)continue;let m=o.get(c.name)??{};this.setNested(m,d,l),o.set(c.name,m)}for(let[a,l]of this.tools.entries()){let u=o.get(l.name);if(!(!u||Object.keys(u).length===0)){if(l.parameters&&l.parameters.type==="object"){let p=(l.parameters.required||[]).filter(d=>u[d]===void 0);if(p.length>0)throw new Ue(`Missing required arguments for tool '${l.name}': ${p.join(", ")}`)}n.push({id:l.name,name:l.name,args:JSON.stringify(u)})}}return{functionCalls:n,remainingFields:r}}normalizeToolName(e){return e.replace(/_([a-z])/g,(t,n)=>n.toUpperCase())}sanitizeFieldName(e){return e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}buildSanitizedFieldMap(e){let t=new Map;if(!e.parameters||!("properties"in e.parameters))return t;let n=(r,o)=>{for(let[s,a]of Object.entries(r)){let l=[...o,s];if(a&&a.type==="object"&&a.properties)n(a.properties,l);else{let u=`${e.name}.${l.join(".")}`,c=this.sanitizeFieldName(u);t.set(c,l)}}};return n(e.parameters.properties??{},[]),t}setNested(e,t,n){let r=e;for(let o=0;o<t.length-1;o++){let s=t[o],a=r[s];(typeof a!="object"||a===null)&&(r[s]={}),r=r[s]}r[t[t.length-1]]=n}isToolField(e){return this.tools.has(this.normalizeToolName(e))}getToolFieldNames(){return Array.from(this.tools.keys()).map(e=>e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_/,""))}};function zc(i){let e=[],t=new Map;if(!i.parameters||!i.parameters.properties)return{fields:e,paramFieldMap:t};let n=i.parameters.properties,r=i.parameters.required||[],o=(s,a,l)=>{for(let[u,c]of Object.entries(s)){let p=a?`${a}.${u}`:u,d=`${i.name}.${p}`;if(c.type==="object"&&c.properties)o(c.properties,p,c.required||[]);else{let m=Hc(c);e.push({name:Wc(d),title:Kc(i.name,p),type:m,description:c.description||`${u} parameter for ${i.name}`,isOptional:!0}),t.set(d,e[e.length-1])}}};return o(n,"",r),{fields:e,paramFieldMap:t}}function Hc(i){switch(i.type){case"string":return{name:"string",isArray:!1};case"number":case"integer":return{name:"number",isArray:!1};case"boolean":return{name:"boolean",isArray:!1};case"array":{let e=i.items;if(e?.type)switch(e.type){case"string":return{name:"string",isArray:!0};case"number":case"integer":return{name:"number",isArray:!0};case"boolean":return{name:"boolean",isArray:!0};default:return{name:"json",isArray:!0}}return{name:"json",isArray:!0}}case"object":return{name:"json",isArray:!1};default:return{name:"string",isArray:!1}}}function Kc(i,e){return`${i} ${e.replace(/\./g," ")}`}function Wc(i){return i.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}function Jl(i,e,t){let n=new Oe(e);if(t){let r=t.getToolParamFieldMap();for(let o of i){let s=r.get(o.name);if(s&&s.size>0)for(let a of s.values())n.getOutputFields().some(u=>u.name===a.name)||n.addOutputField(a);else{let a=Ci(o.name),l=Vl(o.parameters);n.getOutputFields().some(c=>c.name===a)||n.addOutputField({name:a,title:Wl(o.name),type:l,description:o.description||`Parameters for ${o.name}`,isOptional:!0})}}return{signature:n,toolParamFieldMap:r}}else{let r=new Map;for(let o of i)if(o.parameters?.properties&&Object.keys(o.parameters.properties).length>0){let{fields:s,paramFieldMap:a}=Vc(o);r.set(o.name,a);for(let l of s)n.getOutputFields().some(c=>c.name===l.name)||n.addOutputField(l)}else{let s=Ci(o.name),a=Vl(o.parameters);n.getOutputFields().some(u=>u.name===s)||n.addOutputField({name:s,title:Wl(o.name),type:a,description:o.description||`Parameters for ${o.name}`,isOptional:!0})}return{signature:n,toolParamFieldMap:r}}}function Vc(i){let e=[],t=new Map;if(!i.parameters||!i.parameters.properties)return{fields:e,paramFieldMap:t};let n=i.parameters.properties,r=i.parameters.required||[],o=(s,a,l)=>{for(let[u,c]of Object.entries(s)){let p=a?`${a}.${u}`:u,d=`${i.name}.${p}`;if(c.type==="object"&&c.properties)o(c.properties,p,c.required||[]);else{let m=Jc(c),g={name:Ci(d),title:Qc(i.name,p),type:m,description:c.description||`${u} parameter for ${i.name}`,isOptional:!0};e.push(g),t.set(d,g)}}};return o(n,"",r),{fields:e,paramFieldMap:t}}function Jc(i){switch(i.type){case"string":return{name:"string",isArray:!1};case"number":case"integer":return{name:"number",isArray:!1};case"boolean":return{name:"boolean",isArray:!1};case"array":{let e=i.items;if(e?.type)switch(e.type){case"string":return{name:"string",isArray:!0};case"number":case"integer":return{name:"number",isArray:!0};case"boolean":return{name:"boolean",isArray:!0};default:return{name:"json",isArray:!0}}return{name:"json",isArray:!0}}case"object":return{name:"json",isArray:!1};default:return{name:"string",isArray:!1}}}function Qc(i,e){return`${i} ${e.replace(/\./g," ")}`}function Ci(i){return i.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}function Wl(i){return i.replace(/([A-Z])/g," $1").replace(/^./,e=>e.toUpperCase()).trim()}function Vl(i){return!i||!i.properties||Object.keys(i.properties).length===0?{name:"string",isArray:!1}:{name:"json",isArray:!1}}var Cr=class{tools;router;injectedToolFieldNames=new Set;constructor(e){this.tools=e,this.router=new Go(e)}processSignature(e){let{signature:t}=Jl(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 Mn="__finalResult",ve=class i extends Pt{clone=()=>new i(this.signature,this.options);promptTemplate;asserts;streamingAsserts;options;functions;fieldProcessors=[];streamingFieldProcessors=[];excludeContentFromTrace=!1;thoughtFieldName;signatureToolCallingManager;structuredOutputFunctionFallback=!1;activeAbortControllers=new Set;_stopRequested=!1;constructor(e,t){super(e,{description:t?.description,traceLabel:t?.traceLabel}),this.options=t,this.thoughtFieldName=t?.thoughtFieldName??"thought";let n={functions:t?.functions,thoughtFieldName:this.thoughtFieldName};this.promptTemplate=new(t?.promptTemplate??Ut)(this.signature,n),this.asserts=this.options?.asserts??[],this.streamingAsserts=this.options?.streamingAsserts??[],this.excludeContentFromTrace=t?.excludeContentFromTrace??!1,this.functions=t?.functions?Gt(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 Ft(ce.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:s,stepIndex:a}){let{sessionId:l,model:u,rateLimiter:c,stream:p,thinkingTokenBudget:d,showThoughts:m}=n??{},g=await Kl(t,l,{resultPicker:n?.resultPicker}),h=t?.history(g,l)??[];if(h.length===0)throw new Error("No chat prompt found");let f={...n?.modelConfig,...n?.sampleCount?{n:n.sampleCount}:{},...n?.sampleCount&&n?.modelConfig?.temperature===1?{temperature:.8}:{}},b=this.isDebug(e,n),x=a===0,A=this.getLogger(e,n);o=this.signatureToolCallingManager?[]:o;let w,I=this.signature.getOutputFields();if(this.signature.hasComplexFields()&&!this.structuredOutputFunctionFallback){if(!e.getFeatures(u)?.structuredOutputs)throw new Error(`Complex structured outputs (object/array types) require a provider that supports structured outputs. Current provider/model (${u}) does not support this feature. Supported providers: OpenAI (GPT-4o, GPT-4.1+), Google Gemini, Anthropic (Sonnet/Opus).`);w={type:"json_schema",schema:{name:"output",strict:!0,schema:wn(I)}}}let v=n?.contextCache?.cacheBreakpoint??"after-examples",F=e.getFeatures?.(u)?.caching?.cacheBreakpoints===!1,E=n?.contextCache&&(F||v==="after-functions"||v==="after-examples"),D=o?.length&&E?o.map((V,k)=>({...V,cache:k===o.length-1})):o;return await e.chat({chatPrompt:h,functions:D,functionCall:s,modelConfig:f,model:u,responseFormat:w},{sessionId:l,rateLimiter:c,stream:p,debug:b,debugHideSystemPrompt:n?.debugHideSystemPrompt??this.options?.debugHideSystemPrompt??!x,thinkingTokenBudget:d,showThoughts:m,traceContext:r,abortSignal:n?.abortSignal??ce.abortSignal,stepIndex:a,logger:A,functionCallMode:n?.functionCallMode??this.options?.functionCallMode??"auto",retry:n?.retry??this.options?.retry,customLabels:n?.customLabels})}async*forwardCore({ai:e,mem:t,options:n,stepIndex:r,span:o,traceContext:s,states:a,stopFunctionNames:l,stepContext:u}){let{sessionId:c,functions:p}=n??{},d=n?.functionResultFormatter??this.options?.functionResultFormatter,m=n?.functionCall??this.options?.functionCall,g=this.signatureToolCallingManager,h=n?.strictMode??!1,f=n.model,b=this.usage,x=r===0,A=this.isDebug(e,n),w=this.getLogger(e,n),{functions:I,functionCall:T}=El(p,m,x,n);this.structuredOutputFunctionFallback&&I.filter(E=>E.name!==Mn).length===0&&(T={type:"function",function:{name:Mn}});let v=await this.forwardSendRequest({ai:e,mem:t,options:n,traceContext:s,functions:I,functionCall:T,stepIndex:r});v instanceof ReadableStream?yield*Fl({ai:e,model:f,res:v,mem:t,sessionId:c,traceId:o?o.spanContext?.().traceId:void 0,functions:I,strictMode:h,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:w,debug:A,functionResultFormatter:d,signatureToolCallingManager:g,stopFunctionNames:l,disableMemoryCleanup:n.disableMemoryCleanup,stepContext:u,abortSignal:n.abortSignal}):yield*_l({ai:e,model:f,res:v,mem:t,sessionId:c,traceId:o?o.spanContext?.().traceId:void 0,functions:I,span:o,strictMode:h,states:a,usage:b,asserts:this.asserts,fieldProcessors:this.fieldProcessors,thoughtFieldName:this.thoughtFieldName,excludeContentFromTrace:this.excludeContentFromTrace,signature:this.signature,logger:w,debug:A,functionResultFormatter:d,signatureToolCallingManager:g,stopFunctionNames:l,disableMemoryCleanup:n.disableMemoryCleanup,stepContext:u,abortSignal:n.abortSignal})}async*_forward2(e,t,n,r,o,s){this.signatureToolCallingManager=void 0;let a=r?.stopFunction??this.options?.stopFunction,l=Array.isArray(a)?a.map(X=>X.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 yn,d=r.functions?Gt(r.functions):[...this.functions],m=new On(c),g;if(r.selfTuning){if(g=r.selfTuning===!0?{model:!0,thinkingBudget:!0}:r.selfTuning,g.model!==!1){let ne=e.getModelList()?.filter(re=>"model"in re);if(!ne||ne.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 X=hi(e,g,r.model?String(r.model):void 0);d.push(X)}let h={...r},f=r.stepHooks,b=d&&d.length>0,x=r.functionCallMode??this.options?.functionCallMode??"auto";b&&x==="prompt"&&(this.signatureToolCallingManager=new Cr(d)),b&&x==="auto"&&!e.getFeatures(r.model).functions&&(this.signatureToolCallingManager=new Cr(d));let A,w,I=this.options?.promptTemplate??Ut;this.signatureToolCallingManager&&(this.signature=this.signatureToolCallingManager.processSignature(this.signature),this.setSignature(this.signature));let T=this.signature.hasComplexFields(),v=e.getFeatures?.(r.model),F=r.structuredOutputMode??this.options?.structuredOutputMode??"auto";if(this.structuredOutputFunctionFallback=T&&(F==="function"||F==="auto"&&!v?.structuredOutputs),this.structuredOutputFunctionFallback){let X={name:Mn,description:"Return the final result. Call this function with the complete output data.",parameters:wn(this.signature.getOutputFields()),func:async()=>"done"};d.push(X),l=[...l??[],Mn.toLowerCase()]}let E=e.getFeatures?.(r.model)?.caching?.cacheBreakpoints===!1,D={functions:this.signatureToolCallingManager?[]:d,thoughtFieldName:this.thoughtFieldName,contextCache:r.contextCache,examplesInSystem:r.examplesInSystem,ignoreBreakpoints:E,structuredOutputFunctionName:this.structuredOutputFunctionFallback?Mn:void 0};this.promptTemplate=new I(this.signature,D);let U,V=performance.now();Array.isArray(t)?(Ta(t),U=this.promptTemplate.render(t,{examples:this.examples,demos:this.demos})):U=this.promptTemplate.render(t,{examples:this.examples,demos:this.demos});let k=performance.now()-V,$=this.getMetricsInstruments(),N=this.getMergedCustomLabels(e,r);$&&Ro($,"prompt_render",k,this.getSignatureName(),N);let Q=performance.now();p.addRequest(U,r.sessionId);let M=performance.now()-Q;$&&Ro($,"memory_update",M,this.getSignatureName(),N);let L=new Map;n.forEach(X=>{L.set(X.index,{})});let Y=()=>{let X=m._consumePendingOptions();if(X){let{modelConfig:re,...J}=X;h={...h,...J},re&&(h.modelConfig={...h.modelConfig,...re})}let xe=m._consumeFunctionsToAdd();if(xe){let re=Gt(xe);for(let J of re)d.some(oe=>oe.name===J.name)||d.push(J)}let ne=m._consumeFunctionsToRemove();if(ne){let re=new Set(ne.map(J=>J.toLowerCase()));for(let J=d.length-1;J>=0;J--)re.has(d[J].name.toLowerCase())&&d.splice(J,1)}},_=r?.abortSignal??ce.abortSignal;e:for(let X=0;X<c;X++){if(m._beginStep(X),Y(),g&&g.model!==!1){let ne=d.findIndex(re=>re.name==="adjustGeneration");if(ne!==-1){let re=h.model?String(h.model):void 0;d[ne]=hi(e,g,re)}}if(m._isStopRequested)break;if(_?.aborted)throw new Fe("between-steps",_.reason??"Aborted between steps");if(f?.beforeStep&&(await f.beforeStep(m),Y(),m._isStopRequested))break;let xe=u;for(let ne=0;ne<=xe;ne++)try{let re=u+1;for(let oe=0;oe<re;oe++){n.forEach(se=>{se.content="",se.values={},se.functionCalls=[],se.functionsExecuted=new Set,se.xstate={extractedFields:[],streamedIndex:{},s:-1}}),oe>0&&L.forEach((se,Te)=>{L.set(Te,{})});let Pe=new Map;n.forEach(se=>{Pe.set(se.index,{})});try{let se=this.forwardCore({options:{...h,functions:d},ai:e,mem:p,stepIndex:X,span:o,traceContext:s,states:n,stopFunctionNames:l,stepContext:m}),Te=!1;try{for await(let C of se)if(C!==void 0){let O=C.index,R=C.delta,y=Pe.get(O)??{},P=L.get(O)??{},B={},j=!1;for(let H of Object.keys(R)){let q=R[H],K=y[H],z;typeof q=="string"&&(typeof K=="string"||K===void 0)?z=(K??"")+q:Array.isArray(q)&&(Array.isArray(K)||K===void 0)?z=[...K??[],...q]:z=q,y[H]=z;let W=z,ee=P[H];if(typeof W=="string"&&typeof ee=="string")if(W.startsWith(ee)){let ie=W.slice(ee.length);ie&&(B[H]=ie,j=!0,P[H]=W)}else ee.startsWith(W)||W!==ee&&(B[H]=W,j=!0,P[H]=W);else if(Array.isArray(W)&&Array.isArray(ee)){if(W.length>ee.length){let ie=W.slice(ee.length);B[H]=ie,j=!0,P[H]=W}}else JSON.stringify(W)!==JSON.stringify(ee)&&(B[H]=W,j=!0,P[H]=W)}j&&(yield{version:oe,index:C.index,delta:B})}}catch(C){if(C instanceof $t){if(Te=!0,this.structuredOutputFunctionFallback){let O=C.calls.find(R=>R.func.name===Mn);if(O?.args){let R=O.args;Tn(this.signature,R);let y=this.signature.getOutputFields();for(let P of n){let B={};for(let j of y)j.name in R&&!j.isInternal&&(B[j.name]=R[j.name],P.values[j.name]=R[j.name]);yield{version:oe,index:P.index,delta:B}}for(let P of n)await bn(this.asserts,P.values);if(this.fieldProcessors.length>0)for(let P of n)await xr(this.fieldProcessors,P.values,p,r.sessionId)}}}else throw C}if(this.usage.length>0){let C=this.usage[this.usage.length-1];C?.tokens&&m._addUsage(C.tokens.promptTokens??0,C.tokens.completionTokens??0,C.tokens.totalTokens??0)}n.some(C=>C.functionsExecuted.size>0)&&f?.afterFunctionExecution&&(await f.afterFunctionExecution(m),Y());let _e=Te||m._isStopRequested?!1:Nl(p,l,n,h?.sessionId);if(f?.afterStep&&(await f.afterStep(m),Y()),_e&&!m._isStopRequested&&!_?.aborted){let C=this.getMetricsInstruments();C&&wo(C,X+1,c,this.getSignatureName(),N);continue e}if(_?.aborted)throw new Fe("mid-step",_.reason??"Aborted");r?.disableMemoryCleanup||(p.removeByTag("invalid-assistant",r.sessionId),p.removeByTag("correction",r.sessionId),p.removeByTag("error",r.sessionId));let Ge=this.getMetricsInstruments();if(Ge){wo(Ge,X+1,c,this.getSignatureName(),N);let C=new Set;n.forEach(O=>{O.functionsExecuted.forEach(R=>C.add(R))}),C.size>0&&ol(Ge,!0,C.size,!0,!1,this.getSignatureName(),N),sl(Ge,this.fieldProcessors.length,this.streamingFieldProcessors.length,this.getSignatureName(),N)}return}catch(se){if(se instanceof Fe)throw se;w=se;let Te,et=this.isDebug(e,r),_e=this.getLogger(e,r),Ge=this.getMetricsInstruments(),C=this.getSignatureName(),O={error:se,errCount:oe,logger:_e,metricsInstruments:Ge,signatureName:C,span:o,debug:et,customLabels:N};if(o?.recordException(se),se instanceof Ue)Te=hl(O),A=se;else if(se instanceof At)Te=fl(O),A=se;else if(se instanceof Se)xl(O);else{if(se instanceof ot)throw se;{let R=se,y=R instanceof Qe&&R.status>=500&&R.status<600,P=R instanceof De,B=R instanceof nt;throw y||P||B?se:Ti(se,e,this.signature)}}if(Te&&(p.addTag("error",r.sessionId),p.addRequest([{role:"user",content:this.promptTemplate.renderExtraFields(Te)}],r.sessionId),p.addTag("correction",r.sessionId),this.signature.hasComplexFields()))for(let y of n)y.content="",y.values={},y.xstate={extractedFields:[],streamedIndex:{},s:-1}}}let J=this.getMetricsInstruments();throw J&&ri(J,u,!1,u,this.getSignatureName(),N),Ti(new Error(`Unable to fix validation error: ${(A??w)?.message??(A??w)?.toString()??"unknown error"}
370
+ `))):n.push(t),[{type:"text",text:n.join("")}]}},No=i=>i.map(e=>`\`${e.title}\``).join(", "),zl=(i,e)=>i.map(n=>{let r=n.title,o=n.type?.name?vn(n.type):"string",s=n.isOptional?`This optional ${o} field may be omitted`:`${/^[aeiou]/i.test(o)?"An":"A"} ${o} field`,a="";if(n.description){let l=Sn(n.description);e&&(l=Lo(l,e)),a=` ${l}`}return`${r}: (${s})${a}`.trim()}).join(`
371
+ `),Hl=(i,e)=>i.map(n=>{let r=n.title,o=n.type?.name?vn(n.type):"string",s=n.isOptional?`Only include this ${o} field if its value is available`:`This ${o} field must be included`,a="";if(n.description&&n.description.length>0){let l=n.type?.name==="class"?n.description:Sn(n.description);e&&(l=Lo(l,e)),a=` ${l}`}return n.type?.options&&n.type.options.length>0&&(a.length>0&&(a+=". "),a+=`Allowed values: ${n.type.options.join(", ")}`),`${r}: (${s})${a}`.trim()}).join(`
372
+ `),Bc=(i,e)=>{if(i.type?.name==="date"&&e instanceof Date){let t=e.toISOString();return t.slice(0,t.indexOf("T"))}return i.type?.name==="datetime"&&e instanceof Date?Tl(e):i.type?.name==="image"&&typeof e=="object"||i.type?.name==="audio"&&typeof e=="object"||i.type?.name==="file"&&typeof e=="object"||i.type?.name==="url"&&(typeof e=="string"||typeof e=="object")||typeof e=="string"?e:JSON.stringify(e,null,2)};function jc(i){return`{ ${Object.entries(i).map(([t,n])=>{let r=n.isOptional?"?":"",o=vn({name:n.type,isArray:n.isArray,fields:n.fields,options:n.options});return`${t}${r}: ${o}`}).join(", ")} }`}var vn=i=>{let e=(()=>{switch(i?.name){case"string":return"string";case"number":return"number";case"boolean":return"boolean (true or false)";case"date":return'date ("YYYY-MM-DD" format)';case"datetime":return'date time ("YYYY-MM-DD HH:mm Timezone" format)';case"json":return"JSON object";case"class":return"classification class";case"code":return"code";case"file":return"file (with filename, mimeType, and data)";case"url":return"URL (string or object with url, title, description)";case"object":return i?.fields?`object ${jc(i.fields)}`:"object";default:return"string"}})();return i?.isArray?`json array of ${e} items`:e};function Dt(i){return(e,t)=>{if(t.type==="text"){let n=e.length>0?e[e.length-1]:null;n&&n.type==="text"?n.text+=i+t.text:e.push(t)}else e.push(t);return e}}var qc=(i,e,t)=>{if(typeof e=="boolean"||i?.type?.name==="number"&&typeof e=="number")return!1;if(!e||(Array.isArray(e)||typeof e=="string")&&e.length===0){if(t?.isExample||i.isOptional||i.isInternal)return!0;let n=t?.isInputField!==!1?"input":"output";throw new Error(`Value for ${n} field '${i.name}' is required.`)}return!1};function Sn(i){let e=i.trim();return e.length>0?`${e.charAt(0).toUpperCase()}${e.slice(1)}${e.endsWith(".")?"":"."}`:""}function Lo(i,e){if(e.size===0)return i;let t=i,n=Array.from(e.keys()).sort((r,o)=>o.length-r.length);for(let r of n){let o=e.get(r),s=new RegExp(`\`${r}\``,"g");t=t.replace(s,`\`${o}\``);let a=new RegExp(`"${r}"`,"g");t=t.replace(a,`"${o}"`);let l=new RegExp(`'${r}'`,"g");t=t.replace(l,`'${o}'`);let u=new RegExp(`\\[${r}\\]`,"g");t=t.replace(u,`[${o}]`);let c=new RegExp(`\\(${r}\\)`,"g");t=t.replace(c,`(${o})`);let p=new RegExp(`\\$${r}\\b`,"g");t=t.replace(p,`\`${o}\``)}return t}function zc(i,e){let t=i.history(0,e),n=t.some(o=>o.role==="function");return t.some(o=>o.role==="assistant"&&"functionCalls"in o&&Array.isArray(o.functionCalls)&&o.functionCalls.length>0)&&n}function Hc(i,e){let t=i.history(0,e),n=[],r=t.filter(s=>s.role==="assistant"&&"functionCalls"in s&&Array.isArray(s.functionCalls)&&s.functionCalls.length>0),o=t.filter(s=>s.role==="function");for(let s of r)if("functionCalls"in s&&s.functionCalls)for(let a of s.functionCalls){let l=o.find(u=>"functionId"in u&&u.functionId===a.id);l&&"result"in l&&"functionId"in l&&n.push({index:n.length,functionName:a.function.name,functionId:a.id,args:a.function.params||"",result:String(l.result),isError:"isError"in l?!!l.isError:!1})}return n}async function $o(i,e,t,n){if(!e?.resultPicker||i.length<=1)return 0;let r=e.resultPicker;if((t?zc(t,n):!1)&&t){let l=Hc(t,n),u=await r({type:"function",results:l});if(u<0||u>=l.length)throw new Error(`Result picker returned invalid index: ${u}. Must be between 0 and ${l.length-1}`);return u}let s=i.map((l,u)=>({index:u,sample:l.delta})),a=await r({type:"fields",results:s});if(a<0||a>=i.length)throw new Error(`Result picker returned invalid index: ${a}. Must be between 0 and ${i.length-1}`);return a}async function Kl(i,e,t){let n=i?.getLast(e);if(!n||n.role!=="assistant"||n.chat.length<=1)return 0;let r=n.chat.map(s=>({version:0,index:s.index,delta:s.value}));return await $o(r,t,i,e)}var kn=class{_stepIndex=0;maxSteps;_functionsExecuted=new Set;_lastFunctionCalls=[];_usage={promptTokens:0,completionTokens:0,totalTokens:0};state=new Map;_pendingOptions={};_functionsToAdd=[];_functionsToRemove=[];_stopRequested=!1;_stopResultValues;constructor(e){this.maxSteps=e}get stepIndex(){return this._stepIndex}get isFirstStep(){return this._stepIndex===0}get functionsExecuted(){return this._functionsExecuted}get lastFunctionCalls(){return this._lastFunctionCalls}get usage(){return this._usage}setModel(e){this._pendingOptions.model=e}setThinkingBudget(e){this._pendingOptions.thinkingTokenBudget=e}setTemperature(e){this._pendingOptions.modelConfig||(this._pendingOptions.modelConfig={}),this._pendingOptions.modelConfig.temperature=e}setMaxTokens(e){this._pendingOptions.modelConfig||(this._pendingOptions.modelConfig={}),this._pendingOptions.modelConfig.maxTokens=e}setOptions(e){Object.assign(this._pendingOptions,e)}addFunctions(e){this._functionsToAdd.push(...e)}removeFunctions(...e){this._functionsToRemove.push(...e)}stop(e){this._stopRequested=!0,this._stopResultValues=e}_beginStep(e){this._stepIndex=e,this._functionsExecuted=new Set,this._lastFunctionCalls=[]}_recordFunctionCall(e,t,n){this._functionsExecuted.add(e.toLowerCase()),this._lastFunctionCalls.push({name:e,args:t,result:n})}_addUsage(e,t,n){this._usage.promptTokens+=e,this._usage.completionTokens+=t,this._usage.totalTokens+=n}_consumePendingOptions(){if(Object.keys(this._pendingOptions).length===0)return;let e=this._pendingOptions;return this._pendingOptions={},e}_consumeFunctionsToAdd(){if(this._functionsToAdd.length===0)return;let e=this._functionsToAdd;return this._functionsToAdd=[],e}_consumeFunctionsToRemove(){if(this._functionsToRemove.length===0)return;let e=this._functionsToRemove;return this._functionsToRemove=[],e}get _isStopRequested(){return this._stopRequested}get _stopValues(){return this._stopResultValues}};var Go=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}=Kc(t);e.set(t.name,n)}else e.set(t.name,new Map);return e}async route(e,t){let n=[],r={},o=new Map,s=new Map;for(let[a,l]of this.tools.entries())s.set(a,this.buildSanitizedFieldMap(l));for(let[a,l]of Object.entries(e)){let u=this.tools.get(this.normalizeToolName(a));if(u){l!=null&&typeof l=="object"&&o.set(u.name,l);continue}r[a]=l}for(let[a,l]of Object.entries(e))for(let[u,c]of this.tools.entries()){let p=s.get(u);if(!p)continue;let d=p.get(a);if(!d)continue;let m=o.get(c.name)??{};this.setNested(m,d,l),o.set(c.name,m)}for(let[a,l]of this.tools.entries()){let u=o.get(l.name);if(!(!u||Object.keys(u).length===0)){if(l.parameters&&l.parameters.type==="object"){let p=(l.parameters.required||[]).filter(d=>u[d]===void 0);if(p.length>0)throw new Ue(`Missing required arguments for tool '${l.name}': ${p.join(", ")}`)}n.push({id:l.name,name:l.name,args:JSON.stringify(u)})}}return{functionCalls:n,remainingFields:r}}normalizeToolName(e){return e.replace(/_([a-z])/g,(t,n)=>n.toUpperCase())}sanitizeFieldName(e){return e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}buildSanitizedFieldMap(e){let t=new Map;if(!e.parameters||!("properties"in e.parameters))return t;let n=(r,o)=>{for(let[s,a]of Object.entries(r)){let l=[...o,s];if(a&&a.type==="object"&&a.properties)n(a.properties,l);else{let u=`${e.name}.${l.join(".")}`,c=this.sanitizeFieldName(u);t.set(c,l)}}};return n(e.parameters.properties??{},[]),t}setNested(e,t,n){let r=e;for(let o=0;o<t.length-1;o++){let s=t[o],a=r[s];(typeof a!="object"||a===null)&&(r[s]={}),r=r[s]}r[t[t.length-1]]=n}isToolField(e){return this.tools.has(this.normalizeToolName(e))}getToolFieldNames(){return Array.from(this.tools.keys()).map(e=>e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_/,""))}};function Kc(i){let e=[],t=new Map;if(!i.parameters||!i.parameters.properties)return{fields:e,paramFieldMap:t};let n=i.parameters.properties,r=i.parameters.required||[],o=(s,a,l)=>{for(let[u,c]of Object.entries(s)){let p=a?`${a}.${u}`:u,d=`${i.name}.${p}`;if(c.type==="object"&&c.properties)o(c.properties,p,c.required||[]);else{let m=Wc(c);e.push({name:Jc(d),title:Vc(i.name,p),type:m,description:c.description||`${u} parameter for ${i.name}`,isOptional:!0}),t.set(d,e[e.length-1])}}};return o(n,"",r),{fields:e,paramFieldMap:t}}function Wc(i){switch(i.type){case"string":return{name:"string",isArray:!1};case"number":case"integer":return{name:"number",isArray:!1};case"boolean":return{name:"boolean",isArray:!1};case"array":{let e=i.items;if(e?.type)switch(e.type){case"string":return{name:"string",isArray:!0};case"number":case"integer":return{name:"number",isArray:!0};case"boolean":return{name:"boolean",isArray:!0};default:return{name:"json",isArray:!0}}return{name:"json",isArray:!0}}case"object":return{name:"json",isArray:!1};default:return{name:"string",isArray:!1}}}function Vc(i,e){return`${i} ${e.replace(/\./g," ")}`}function Jc(i){return i.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}function Jl(i,e,t){let n=new Oe(e);if(t){let r=t.getToolParamFieldMap();for(let o of i){let s=r.get(o.name);if(s&&s.size>0)for(let a of s.values())n.getOutputFields().some(u=>u.name===a.name)||n.addOutputField(a);else{let a=Ci(o.name),l=Vl(o.parameters);n.getOutputFields().some(c=>c.name===a)||n.addOutputField({name:a,title:Wl(o.name),type:l,description:o.description||`Parameters for ${o.name}`,isOptional:!0})}}return{signature:n,toolParamFieldMap:r}}else{let r=new Map;for(let o of i)if(o.parameters?.properties&&Object.keys(o.parameters.properties).length>0){let{fields:s,paramFieldMap:a}=Qc(o);r.set(o.name,a);for(let l of s)n.getOutputFields().some(c=>c.name===l.name)||n.addOutputField(l)}else{let s=Ci(o.name),a=Vl(o.parameters);n.getOutputFields().some(u=>u.name===s)||n.addOutputField({name:s,title:Wl(o.name),type:a,description:o.description||`Parameters for ${o.name}`,isOptional:!0})}return{signature:n,toolParamFieldMap:r}}}function Qc(i){let e=[],t=new Map;if(!i.parameters||!i.parameters.properties)return{fields:e,paramFieldMap:t};let n=i.parameters.properties,r=i.parameters.required||[],o=(s,a,l)=>{for(let[u,c]of Object.entries(s)){let p=a?`${a}.${u}`:u,d=`${i.name}.${p}`;if(c.type==="object"&&c.properties)o(c.properties,p,c.required||[]);else{let m=Yc(c),g={name:Ci(d),title:Xc(i.name,p),type:m,description:c.description||`${u} parameter for ${i.name}`,isOptional:!0};e.push(g),t.set(d,g)}}};return o(n,"",r),{fields:e,paramFieldMap:t}}function Yc(i){switch(i.type){case"string":return{name:"string",isArray:!1};case"number":case"integer":return{name:"number",isArray:!1};case"boolean":return{name:"boolean",isArray:!1};case"array":{let e=i.items;if(e?.type)switch(e.type){case"string":return{name:"string",isArray:!0};case"number":case"integer":return{name:"number",isArray:!0};case"boolean":return{name:"boolean",isArray:!0};default:return{name:"json",isArray:!0}}return{name:"json",isArray:!0}}case"object":return{name:"json",isArray:!1};default:return{name:"string",isArray:!1}}}function Xc(i,e){return`${i} ${e.replace(/\./g," ")}`}function Ci(i){return i.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}function Wl(i){return i.replace(/([A-Z])/g," $1").replace(/^./,e=>e.toUpperCase()).trim()}function Vl(i){return!i||!i.properties||Object.keys(i.properties).length===0?{name:"string",isArray:!1}:{name:"json",isArray:!1}}var Ir=class{tools;router;injectedToolFieldNames=new Set;constructor(e){this.tools=e,this.router=new Go(e)}processSignature(e){let{signature:t}=Jl(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 On="__finalResult",Se=class i extends Pt{clone=()=>new i(this.signature,this.options);promptTemplate;asserts;streamingAsserts;options;functions;fieldProcessors=[];streamingFieldProcessors=[];excludeContentFromTrace=!1;thoughtFieldName;signatureToolCallingManager;structuredOutputFunctionFallback=!1;activeAbortControllers=new Set;_stopRequested=!1;constructor(e,t){super(e,{description:t?.description,traceLabel:t?.traceLabel}),this.options=t,this.thoughtFieldName=t?.thoughtFieldName??"thought";let n={functions:t?.functions,thoughtFieldName:this.thoughtFieldName};this.promptTemplate=new(t?.promptTemplate??Ut)(this.signature,n),this.asserts=this.options?.asserts??[],this.streamingAsserts=this.options?.streamingAsserts??[],this.excludeContentFromTrace=t?.excludeContentFromTrace??!1,this.functions=t?.functions?Gt(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 Ft(de.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:s,stepIndex:a}){let{sessionId:l,model:u,rateLimiter:c,stream:p,thinkingTokenBudget:d,showThoughts:m}=n??{},g=await Kl(t,l,{resultPicker:n?.resultPicker}),h=t?.history(g,l)??[];if(h.length===0)throw new Error("No chat prompt found");let f={...n?.modelConfig,...n?.sampleCount?{n:n.sampleCount}:{},...n?.sampleCount&&n?.modelConfig?.temperature===1?{temperature:.8}:{}},I=this.isDebug(e,n),x=a===0,A=this.getLogger(e,n);o=this.signatureToolCallingManager?[]:o;let w,b=this.signature.getOutputFields();if(this.signature.hasComplexFields()&&!this.structuredOutputFunctionFallback){if(!e.getFeatures(u)?.structuredOutputs)throw new Error(`Complex structured outputs (object/array types) require a provider that supports structured outputs. Current provider/model (${u}) does not support this feature. Supported providers: OpenAI (GPT-4o, GPT-4.1+), Google Gemini, Anthropic (Sonnet/Opus).`);w={type:"json_schema",schema:{name:"output",strict:!0,schema:Tn(b)}}}let v=n?.contextCache?.cacheBreakpoint??"after-examples",F=e.getFeatures?.(u)?.caching?.cacheBreakpoints===!1,O=n?.contextCache&&(F||v==="after-functions"||v==="after-examples"),D=o?.length&&O?o.map((Q,S)=>({...Q,cache:S===o.length-1})):o;return await e.chat({chatPrompt:h,functions:D,functionCall:s,modelConfig:f,model:u,responseFormat:w},{sessionId:l,rateLimiter:c,stream:p,debug:I,debugHideSystemPrompt:n?.debugHideSystemPrompt??this.options?.debugHideSystemPrompt??!x,thinkingTokenBudget:d,showThoughts:m,traceContext:r,abortSignal:n?.abortSignal??de.abortSignal,stepIndex:a,logger:A,functionCallMode:n?.functionCallMode??this.options?.functionCallMode??"auto",retry:n?.retry??this.options?.retry,customLabels:n?.customLabels})}async*forwardCore({ai:e,mem:t,options:n,stepIndex:r,span:o,traceContext:s,states:a,stopFunctionNames:l,stepContext:u}){let{sessionId:c,functions:p}=n??{},d=n?.functionResultFormatter??this.options?.functionResultFormatter,m=n?.functionCall??this.options?.functionCall,g=this.signatureToolCallingManager,h=n?.strictMode??!1,f=n.model,I=this.usage,x=r===0,A=this.isDebug(e,n),w=this.getLogger(e,n),{functions:b,functionCall:T}=El(p,m,x,n);this.structuredOutputFunctionFallback&&b.filter(O=>O.name!==On).length===0&&(T={type:"function",function:{name:On}});let v=await this.forwardSendRequest({ai:e,mem:t,options:n,traceContext:s,functions:b,functionCall:T,stepIndex:r});v instanceof ReadableStream?yield*Fl({ai:e,model:f,res:v,mem:t,sessionId:c,traceId:o?o.spanContext?.().traceId:void 0,functions:b,strictMode:h,span:o,states:a,usage:I,asserts:this.asserts,streamingAsserts:this.streamingAsserts,fieldProcessors:this.fieldProcessors,streamingFieldProcessors:this.streamingFieldProcessors,thoughtFieldName:this.thoughtFieldName,excludeContentFromTrace:this.excludeContentFromTrace,signature:this.signature,logger:w,debug:A,functionResultFormatter:d,signatureToolCallingManager:g,stopFunctionNames:l,disableMemoryCleanup:n.disableMemoryCleanup,stepContext:u,abortSignal:n.abortSignal}):yield*_l({ai:e,model:f,res:v,mem:t,sessionId:c,traceId:o?o.spanContext?.().traceId:void 0,functions:b,span:o,strictMode:h,states:a,usage:I,asserts:this.asserts,fieldProcessors:this.fieldProcessors,thoughtFieldName:this.thoughtFieldName,excludeContentFromTrace:this.excludeContentFromTrace,signature:this.signature,logger:w,debug:A,functionResultFormatter:d,signatureToolCallingManager:g,stopFunctionNames:l,disableMemoryCleanup:n.disableMemoryCleanup,stepContext:u,abortSignal:n.abortSignal})}async*_forward2(e,t,n,r,o,s){this.signatureToolCallingManager=void 0;let a=r?.stopFunction??this.options?.stopFunction,l=Array.isArray(a)?a.map(Z=>Z.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 An,d=r.functions?Gt(r.functions):[...this.functions],m=new kn(c),g;if(r.selfTuning){if(g=r.selfTuning===!0?{model:!0,thinkingBudget:!0}:r.selfTuning,g.model!==!1){let re=e.getModelList()?.filter(oe=>"model"in oe);if(!re||re.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 Z=hi(e,g,r.model?String(r.model):void 0);d.push(Z)}let h={...r},f=r.stepHooks,I=d&&d.length>0,x=r.functionCallMode??this.options?.functionCallMode??"auto";I&&x==="prompt"&&(this.signatureToolCallingManager=new Ir(d)),I&&x==="auto"&&!e.getFeatures(r.model).functions&&(this.signatureToolCallingManager=new Ir(d));let A,w,b=this.options?.promptTemplate??Ut;this.signatureToolCallingManager&&(this.signature=this.signatureToolCallingManager.processSignature(this.signature),this.setSignature(this.signature));let T=this.signature.hasComplexFields(),v=e.getFeatures?.(r.model),F=r.structuredOutputMode??this.options?.structuredOutputMode??"auto";if(this.structuredOutputFunctionFallback=T&&(F==="function"||F==="auto"&&!v?.structuredOutputs),this.structuredOutputFunctionFallback){let Z={name:On,description:"Return the final result. Call this function with the complete output data.",parameters:Tn(this.signature.getOutputFields()),func:async()=>"done"};d.push(Z),l=[...l??[],On.toLowerCase()]}let O=e.getFeatures?.(r.model)?.caching?.cacheBreakpoints===!1,D={functions:this.signatureToolCallingManager?[]:d,thoughtFieldName:this.thoughtFieldName,contextCache:r.contextCache,examplesInSystem:r.examplesInSystem,ignoreBreakpoints:O,structuredOutputFunctionName:this.structuredOutputFunctionFallback?On:void 0};this.promptTemplate=new b(this.signature,D);let B,Q=performance.now();Array.isArray(t)?(Ta(t),B=this.promptTemplate.render(t,{examples:this.examples,demos:this.demos})):B=this.promptTemplate.render(t,{examples:this.examples,demos:this.demos});let S=performance.now()-Q,G=this.getMetricsInstruments(),N=this.getMergedCustomLabels(e,r);G&&Ro(G,"prompt_render",S,this.getSignatureName(),N);let Y=performance.now();p.addRequest(B,r.sessionId);let M=performance.now()-Y;G&&Ro(G,"memory_update",M,this.getSignatureName(),N);let L=new Map;n.forEach(Z=>{L.set(Z.index,{})});let X=()=>{let Z=m._consumePendingOptions();if(Z){let{modelConfig:oe,...J}=Z;h={...h,...J},oe&&(h.modelConfig={...h.modelConfig,...oe})}let Ae=m._consumeFunctionsToAdd();if(Ae){let oe=Gt(Ae);for(let J of oe)d.some(se=>se.name===J.name)||d.push(J)}let re=m._consumeFunctionsToRemove();if(re){let oe=new Set(re.map(J=>J.toLowerCase()));for(let J=d.length-1;J>=0;J--)oe.has(d[J].name.toLowerCase())&&d.splice(J,1)}},_=r?.abortSignal??de.abortSignal;e:for(let Z=0;Z<c;Z++){if(m._beginStep(Z),X(),g&&g.model!==!1){let re=d.findIndex(oe=>oe.name==="adjustGeneration");if(re!==-1){let oe=h.model?String(h.model):void 0;d[re]=hi(e,g,oe)}}if(m._isStopRequested)break;if(_?.aborted)throw new Fe("between-steps",_.reason??"Aborted between steps");if(f?.beforeStep&&(await f.beforeStep(m),X(),m._isStopRequested))break;let Ae=u;for(let re=0;re<=Ae;re++)try{let oe=u+1;for(let se=0;se<oe;se++){n.forEach(le=>{le.content="",le.values={},le.functionCalls=[],le.functionsExecuted=new Set,le.xstate={extractedFields:[],streamedIndex:{},s:-1}}),se>0&&L.forEach((le,Ce)=>{L.set(Ce,{})});let Ee=new Map;n.forEach(le=>{Ee.set(le.index,{})});try{let le=this.forwardCore({options:{...h,functions:d},ai:e,mem:p,stepIndex:Z,span:o,traceContext:s,states:n,stopFunctionNames:l,stepContext:m}),Ce=!1;try{for await(let C of le)if(C!==void 0){let k=C.index,R=C.delta,y=Ee.get(k)??{},E=L.get(k)??{},U={},j=!1;for(let K of Object.keys(R)){let q=R[K],H=y[K],z;typeof q=="string"&&(typeof H=="string"||H===void 0)?z=(H??"")+q:Array.isArray(q)&&(Array.isArray(H)||H===void 0)?z=[...H??[],...q]:z=q,y[K]=z;let W=z,ee=E[K];if(typeof W=="string"&&typeof ee=="string")if(W.startsWith(ee)){let ce=W.slice(ee.length);ce&&(U[K]=ce,j=!0,E[K]=W)}else ee.startsWith(W)||W!==ee&&(U[K]=W,j=!0,E[K]=W);else if(Array.isArray(W)&&Array.isArray(ee)){if(W.length>ee.length){let ce=W.slice(ee.length);U[K]=ce,j=!0,E[K]=W}}else JSON.stringify(W)!==JSON.stringify(ee)&&(U[K]=W,j=!0,E[K]=W)}j&&(yield{version:se,index:C.index,delta:U})}}catch(C){if(C instanceof $t){if(Ce=!0,this.structuredOutputFunctionFallback){let k=C.calls.find(R=>R.func.name===On);if(k?.args){let R=k.args;Cn(this.signature,R);let y=this.signature.getOutputFields();for(let E of n){let U={};for(let j of y)j.name in R&&!j.isInternal&&(U[j.name]=R[j.name],E.values[j.name]=R[j.name]);yield{version:se,index:E.index,delta:U}}for(let E of n)await yn(this.asserts,E.values);if(this.fieldProcessors.length>0)for(let E of n)await fr(this.fieldProcessors,E.values,p,r.sessionId)}}}else throw C}if(this.usage.length>0){let C=this.usage[this.usage.length-1];C?.tokens&&m._addUsage(C.tokens.promptTokens??0,C.tokens.completionTokens??0,C.tokens.totalTokens??0)}n.some(C=>C.functionsExecuted.size>0)&&f?.afterFunctionExecution&&(await f.afterFunctionExecution(m),X());let _e=Ce||m._isStopRequested?!1:Nl(p,l,n,h?.sessionId);if(f?.afterStep&&(await f.afterStep(m),X()),_e&&!m._isStopRequested&&!_?.aborted){let C=this.getMetricsInstruments();C&&wo(C,Z+1,c,this.getSignatureName(),N);continue e}if(_?.aborted)throw new Fe("mid-step",_.reason??"Aborted");r?.disableMemoryCleanup||(p.removeByTag("invalid-assistant",r.sessionId),p.removeByTag("correction",r.sessionId),p.removeByTag("error",r.sessionId));let $e=this.getMetricsInstruments();if($e){wo($e,Z+1,c,this.getSignatureName(),N);let C=new Set;n.forEach(k=>{k.functionsExecuted.forEach(R=>C.add(R))}),C.size>0&&ol($e,!0,C.size,!0,!1,this.getSignatureName(),N),sl($e,this.fieldProcessors.length,this.streamingFieldProcessors.length,this.getSignatureName(),N)}return}catch(le){if(le instanceof Fe)throw le;w=le;let Ce,Xe=this.isDebug(e,r),_e=this.getLogger(e,r),$e=this.getMetricsInstruments(),C=this.getSignatureName(),k={error:le,errCount:se,logger:_e,metricsInstruments:$e,signatureName:C,span:o,debug:Xe,customLabels:N};if(o?.recordException(le),le instanceof Ue)Ce=hl(k),A=le;else if(le instanceof yt)Ce=fl(k),A=le;else if(le instanceof Re)xl(k);else{if(le instanceof rt)throw le;{let R=le,y=R instanceof Ve&&R.status>=500&&R.status<600,E=R instanceof De,U=R instanceof tt;throw y||E||U?le:Ti(le,e,this.signature)}}if(Ce&&(p.addTag("error",r.sessionId),p.addRequest([{role:"user",content:this.promptTemplate.renderExtraFields(Ce)}],r.sessionId),p.addTag("correction",r.sessionId),this.signature.hasComplexFields()))for(let y of n)y.content="",y.values={},y.xstate={extractedFields:[],streamedIndex:{},s:-1}}}let J=this.getMetricsInstruments();throw J&&ri(J,u,!1,u,this.getSignatureName(),N),Ti(new Error(`Unable to fix validation error: ${(A??w)?.message??(A??w)?.toString()??"unknown error"}
373
373
 
374
374
  LLM Output:
375
- ${n.map(oe=>oe.content).join(`
375
+ ${n.map(se=>se.content).join(`
376
376
  ---
377
- `)}`),e,this.signature)}catch(re){let J=re,oe=J instanceof Qe&&J.status>=500&&J.status<600,Pe=J instanceof De,se=J instanceof nt,Te=J instanceof ot;if((oe||Pe||se||Te)&&ne<xe){let _e=this.isDebug(e,r),Ge=this.getLogger(e,r),R=Math.min(6e4,1e3*Math.pow(2,ne));_e&&Ge&&Ge({name:"Notification",id:"infrastructure-retry",value:`Infrastructure error (attempt ${ne+1}/${xe+1}): ${J.message}. Retrying in ${R}ms...`}),o?.addEvent("infrastructure.retry",{attempt:ne+1,maxRetries:xe,delay:R,errorType:J instanceof Qe?"status_error":J instanceof De?"network_error":J instanceof nt?"timeout_error":"stream_terminated",errorMessage:J.message}),await new Promise((y,P)=>{let B=!1,j,H=()=>{_&&j&&_.removeEventListener("abort",j)},K=setTimeout(()=>{B||(B=!0,H(),y())},R);if(_){if(j=()=>{B||(B=!0,clearTimeout(K),H(),P(new Fe("infrastructure-retry-backoff",_.reason?String(_.reason):"Aborted during retry backoff")))},_.aborted){j();return}_.addEventListener("abort",j,{once:!0})}});continue}throw re}}throw $&&wo($,c,c,this.getSignatureName(),N),Ti(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"&&Et(r,n),o.name,o.name,(o.name==="string"||o.name==="code")&&at(r,n),o.name==="number"&&lt(r,n),o.name==="object"&&o.fields&&typeof r=="object"&&r!==null&&this.validateObjectFields(r,o.fields,n.name),o.isArray&&Array.isArray(r)))for(let s=0;s<r.length;s++){let a=r[s];o.name==="string"||o.name==="code"?at(a,n):o.name==="number"?lt(a,n):o.fields&&typeof a=="object"&&a!==null&&this.validateObjectFields(a,o.fields,`${n.name}[${s}]`)}}}validateObjectFields(e,t,n){for(let[r,o]of Object.entries(t)){let s=e[r];if(o.isOptional&&s===void 0)continue;let a={name:`${n}.${r}`,type:{name:o.type,isArray:o.isArray,options:o.options?[...o.options]:void 0,fields:o.fields,minLength:o.minLength,maxLength:o.maxLength,minimum:o.minimum,maximum:o.maximum,pattern:o.pattern,format:o.format},description:o.description,isOptional:o.isOptional};if(o.type==="string"||o.type==="code"?at(s,a):o.type==="number"?lt(s,a):o.type==="object"&&o.fields&&typeof s=="object"&&s!==null&&this.validateObjectFields(s,o.fields,a.name),o.isArray&&Array.isArray(s))for(let l=0;l<s.length;l++){let u=s[l];o.type==="string"||o.type==="code"?at(u,a):o.type==="number"?lt(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=vt(r.signal,n?.abortSignal??ce.abortSignal),s=o?{...n,abortSignal:o}:n;try{let a=performance.now(),l=this.createStates(n.sampleCount??1),u=performance.now()-a,c=this.getMetricsInstruments(),p=this.getMergedCustomLabels(e,n);c&&Ro(c,"state_creation",u,this.getSignatureName(),p);let d=n?.tracer??this.options?.tracer??e.getOptions().tracer,m=this.functions;if(n?.functions&&(m=Gt(n.functions,this.functions)),!d){yield*this._forward2(e,t,l,{...s,functions:m});return}let g=m?.map(I=>I.name).join(","),h={signature:JSON.stringify(this.signature.toJSON(),null,2),...this.examples?{examples:JSON.stringify(this.examples,null,2)}:{},...g?{provided_functions:g}:{},...n?.model?{model:n.model}:{},...n?.thinkingTokenBudget?{thinking_token_budget:n.thinkingTokenBudget}:{},...n?.showThoughts?{show_thoughts:n.showThoughts}:{},...n?.maxSteps?{max_steps:n.maxSteps}:{},...n?.maxRetries?{max_retries:n.maxRetries}:{}},f=this.traceLabel&&n.traceLabel?`${this.traceLabel} > ${n.traceLabel}`:n.traceLabel??this.traceLabel,b=f?`AxGen > ${f}`:"AxGen",x=d.startSpan(b,{kind:En.SpanKind.SERVER,attributes:h}),A=En.context.active(),w=En.trace.setSpan(A,x);try{if(this.excludeContentFromTrace||x.addEvent("input",{content:JSON.stringify(t,null,2)}),yield*this._forward2(e,t,l,{...s,functions:m},x,w),!this.excludeContentFromTrace){let I=l.map(v=>v.values),T=I.length===1?I[0]:I;x.addEvent("output",{content:JSON.stringify(T,null,2)})}}finally{x.end()}}finally{this.activeAbortControllers.delete(r),this._stopRequested=!1}}async forward(e,t,n){let r=n?.cachingFunction??this.options?.cachingFunction??ce.cachingFunction,o=(()=>{if(!r)return;let d=this.signature.getInputFields().map(m=>m.name);return this.computeCacheKey(t,d)})();if(r&&o){let d=await r(o);if(d!==void 0)return d}let s=performance.now(),a=this.getSignatureName(),l=n?.stream??!1,u=!1,c=0,p=!1;try{let d=this.getMetricsInstruments(),m=this.getMergedCustomLabels(e,n);d&&ll(d,this.signature.getInputFields().length,this.signature.getOutputFields().length,this.examples?.length??0,this.demos?.length??0,a,m);let g=this._forward1(e,t,n??{}),h=[],f=0,b=0;for await(let F of g)F.version!==f&&(h=[]),f=F.version,h=ai(h,F),b++;c=f;let x=performance.now();p=!!n?.resultPicker;let A=await $o(h,{resultPicker:n?.resultPicker},n?.mem,n?.sessionId),w=performance.now()-x,T=h[A]?.delta??{},v=Array.isArray(t)?{}:t??{};if(this.trace={...v,...T},p&&this.isDebug(e,n)){let F=this.getLogger(e,n);oa(h.length,A,w,F)}if(u=!0,d&&(al(d,h.length,p,p?w:void 0,a,m),il(d,l,b,void 0,a,m)),r&&o)try{await r(o,T)}catch{}return T}catch(d){throw u=!1,d}finally{let d=performance.now()-s,m=this.getMetricsInstruments(),g=this.getMergedCustomLabels(e,n);m&&(nl(m,d,u,a,e.getName(),n?.model?String(n.model):void 0,g),c>0&&ri(m,c,u,n?.maxRetries??this.options?.maxRetries??3,a,g))}}async*streamingForward(e,t,n){let r=n?.cachingFunction??this.options?.cachingFunction??ce.cachingFunction,o=(()=>{if(!r)return;let p=this.signature.getInputFields().map(d=>d.name);return this.computeCacheKey(t,p)})();if(r&&o){let p;try{p=await r(o)}catch{}if(p!==void 0){yield{version:0,index:0,delta:p};return}}if(!n?.resultPicker){yield*this._forward1(e,t,{...n,stream:!0});return}let s=this._forward1(e,t,{...n,stream:!0}),a=[],l=0;for await(let p of s)p.version!==l&&(a=[]),l=p.version,a=ai(a,p);let u=await $o(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??ce.debug??e.getOptions().debug??!1}getLogger(e,t){return t?.logger??this.options?.logger??ce.logger??e.getLogger()}computeCacheKey(e,t){let n=tt("sha256");n.update(this.signature.hash()??"");let r=o=>{let s=typeof o;if(n.update(`|${s}|`),o==null){n.update("null");return}if(s==="string"||s==="number"||s==="boolean"){n.update(String(o));return}if(Array.isArray(o)){n.update("[");for(let a of o)r(a);n.update("]");return}if(typeof o=="object"&&o!==null&&"mimeType"in o&&"data"in o){let a=o;n.update(a.mimeType??"");let l=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 s=t.map(a=>o.values?.[a]);for(let a of s)r(a)}else{let o=t.map(s=>e?.[s]);for(let s of o)r(s)}return n.digest("hex")}},Tr=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 Ti(i,e,t){let n=i instanceof Error?i:new Error(String(i));if(n instanceof Fe)return n;let r=(n.message||"").toLowerCase();if(r.includes("at least")||r.includes("at most")||r.includes("must match pattern")||r.includes("invalid url")||r.includes("required")||r.includes("missing")||r.includes("valid email")||r.includes("number must be")||n.name==="ValidationError"||n.name==="AssertionError"||n.name==="AxAssertionError"||n.stack?.includes("asserts.ts"))return n;let s=e.getLastUsedChatModel(),a=e.getLastUsedModelConfig(),l={model:s,maxTokens:a?.maxTokens,streaming:a?.stream??!1,signature:{input:t.getInputFields(),output:t.getOutputFields(),description:t.getDescription()}};return new Tr(`Generate failed: ${n.message}`,l,{cause:n})}var Yc=i=>i.replace(/^\W+|\W+$/g,""),Xc=(i,e)=>{let t=i.search(e);if(t===-1)return[i];let n=i.match(e);if(!n)throw new Error("Match failed unexpectedly.");let r=i.substring(0,t),o=i.substring(t+n[0].length);return[r,o]},Zc=i=>{let e=new Set,t=[];for(let n of i)e.has(n)||(e.add(n),t.push(n));return t},ep=i=>{let e=i.match(/^(\d+)[.,\s]+(.*)$/);if(!e||e.length<3)throw new Error('line must start with a number, a dot and then text. e.g. "1. hello"');let t=Number.parseInt(e[1],10),n=e[2].trim();return{id:t,text:n}},tp=i=>{let e=i.match(/^(\d+)[.,\s]+(.*)$/);return e&&e[2]!==void 0?e[2].trim():i},np=(i,e)=>{let t=[];for(let n=0;n<i.length;n+=e)t.push(i.slice(n,n+e));return t},Do={trimNonAlphaNum:Yc,splitIntoTwo:Xc,dedup:Zc,extractIdAndText:ep,extractIndexPrefixedText:tp,batchArray:np};var Uo=class extends ve{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."
378
- 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}=Do.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 Bo=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 Bx=new Ye,jo=class{name;context;constructor(e,t){this.name=e,this.context=t}getName(){return this.name}getContext(){return this.context}},qo=class{ai;db;debug;constructor(e){this.db=new Ot,this.ai=e}getState(){return this.db.getDB()}setState(e){this.db.setDB(e)}setClasses=async(e,t)=>{for(let n of e){let r=await this.ai.embed({texts:n.getContext()},{abortSignal:t?.abortSignal});await this.db.upsert({id:n.getName(),table:"classes",values:r.embeddings[0]})}};async forward(e,t){let{embeddings:n}=await this.ai.embed({texts:[e]},{abortSignal:t?.abortSignal}),o=(await this.db.query({table:"classes",values:n[0]})).matches;if(typeof t?.cutoff=="number"){let{cutoff:a}=t;o=o.filter(l=>l.score<=a)}let s=o.at(0);return s?s.id:""}setOptions(e){typeof e.debug=="boolean"&&(this.debug=e.debug)}};var Ql=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 Yl(i,e){return i.filter(t=>!e.has(t))}function Xl(i){let e={};for(let t of i)e[t]=(e[t]||0)+1;return e}function Bt(i){let e=i.normalize("NFD");return e=e.replace(/\b(a|an|the)\b/g," "),e=e.split(/\s+/).join(" "),e=e.replace(/[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/g,""),e.toLowerCase()}function rp(i,e){return Bt(i)===Bt(e)?1:0}function op(i,e){let t=Bt(i).split(" "),n=Bt(e).split(" "),r=Xl(t),o=Xl(n),s=0;for(let u in r){let c=r[u]??0,p=o[u]??0;s+=Math.min(c,p)}if(s===0)return 0;let a=s/t.length,l=s/n.length;return 2*a*l/(a+l)}function sp(i,e,t,n=!1){let r=Bt(i).split(" "),o=Bt(e).split(" "),s=Bt(t).split(" "),a=new Set([...Ql,...r]);o=Yl(o,a),s=Yl(s,a);let l=0,u=l/o.length,c=l/s.length,p=2*u*c/(u+c);return n?c:p}var Zl={emScore:rp,f1Score:op,novelF1ScoreOptimized:sp};var zo=class{ai;program;examples;constructor({ai:e,program:t,examples:n=[]}){if(n.length===0)throw new Error("No examples found");this.ai=e,this.program=t,this.examples=n}async run(e){let t=Date.now(),n=this.examples.length,r=0;for(let s=0;s<n;s++){let a=this.examples[s];if(!a)throw new Error("Invalid example");try{let l=await this.program.forward(this.ai,a,{maxRetries:1}),u=await e({prediction:l,example:a});r+=u}catch(l){console.warn(`Program evaluation failed for example ${s}: ${l instanceof Error?l.message:"Unknown error"}`)}}let o=n>0?r/n:0;this.ai.getOptions().debug&&console.log(`
377
+ `)}`),e,this.signature)}catch(oe){let J=oe,se=J instanceof Ve&&J.status>=500&&J.status<600,Ee=J instanceof De,le=J instanceof tt,Ce=J instanceof rt;if((se||Ee||le||Ce)&&re<Ae){let _e=this.isDebug(e,r),$e=this.getLogger(e,r),R=Math.min(6e4,1e3*Math.pow(2,re));_e&&$e&&$e({name:"Notification",id:"infrastructure-retry",value:`Infrastructure error (attempt ${re+1}/${Ae+1}): ${J.message}. Retrying in ${R}ms...`}),o?.addEvent("infrastructure.retry",{attempt:re+1,maxRetries:Ae,delay:R,errorType:J instanceof Ve?"status_error":J instanceof De?"network_error":J instanceof tt?"timeout_error":"stream_terminated",errorMessage:J.message}),await new Promise((y,E)=>{let U=!1,j,K=()=>{_&&j&&_.removeEventListener("abort",j)},H=setTimeout(()=>{U||(U=!0,K(),y())},R);if(_){if(j=()=>{U||(U=!0,clearTimeout(H),K(),E(new Fe("infrastructure-retry-backoff",_.reason?String(_.reason):"Aborted during retry backoff")))},_.aborted){j();return}_.addEventListener("abort",j,{once:!0})}});continue}throw oe}}throw G&&wo(G,c,c,this.getSignatureName(),N),Ti(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"&&Et(r,n),o.name,o.name,(o.name==="string"||o.name==="code")&&it(r,n),o.name==="number"&&at(r,n),o.name==="object"&&o.fields&&typeof r=="object"&&r!==null&&this.validateObjectFields(r,o.fields,n.name),o.isArray&&Array.isArray(r)))for(let s=0;s<r.length;s++){let a=r[s];o.name==="string"||o.name==="code"?it(a,n):o.name==="number"?at(a,n):o.fields&&typeof a=="object"&&a!==null&&this.validateObjectFields(a,o.fields,`${n.name}[${s}]`)}}}validateObjectFields(e,t,n){for(let[r,o]of Object.entries(t)){let s=e[r];if(o.isOptional&&s===void 0)continue;let a={name:`${n}.${r}`,type:{name:o.type,isArray:o.isArray,options:o.options?[...o.options]:void 0,fields:o.fields,minLength:o.minLength,maxLength:o.maxLength,minimum:o.minimum,maximum:o.maximum,pattern:o.pattern,format:o.format},description:o.description,isOptional:o.isOptional};if(o.type==="string"||o.type==="code"?it(s,a):o.type==="number"?at(s,a):o.type==="object"&&o.fields&&typeof s=="object"&&s!==null&&this.validateObjectFields(s,o.fields,a.name),o.isArray&&Array.isArray(s))for(let l=0;l<s.length;l++){let u=s[l];o.type==="string"||o.type==="code"?it(u,a):o.type==="number"?at(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=vt(r.signal,n?.abortSignal??de.abortSignal),s=o?{...n,abortSignal:o}:n;try{let a=performance.now(),l=this.createStates(n.sampleCount??1),u=performance.now()-a,c=this.getMetricsInstruments(),p=this.getMergedCustomLabels(e,n);c&&Ro(c,"state_creation",u,this.getSignatureName(),p);let d=n?.tracer??this.options?.tracer??e.getOptions().tracer,m=this.functions;if(n?.functions&&(m=Gt(n.functions,this.functions)),!d){yield*this._forward2(e,t,l,{...s,functions:m});return}let g=m?.map(b=>b.name).join(","),h={signature:JSON.stringify(this.signature.toJSON(),null,2),...this.examples?{examples:JSON.stringify(this.examples,null,2)}:{},...g?{provided_functions:g}:{},...n?.model?{model:n.model}:{},...n?.thinkingTokenBudget?{thinking_token_budget:n.thinkingTokenBudget}:{},...n?.showThoughts?{show_thoughts:n.showThoughts}:{},...n?.maxSteps?{max_steps:n.maxSteps}:{},...n?.maxRetries?{max_retries:n.maxRetries}:{}},f=this.traceLabel&&n.traceLabel?`${this.traceLabel} > ${n.traceLabel}`:n.traceLabel??this.traceLabel,I=f?`AxGen > ${f}`:"AxGen",x=d.startSpan(I,{kind:Mn.SpanKind.SERVER,attributes:h}),A=Mn.context.active(),w=Mn.trace.setSpan(A,x);try{if(this.excludeContentFromTrace||x.addEvent("input",{content:JSON.stringify(t,null,2)}),yield*this._forward2(e,t,l,{...s,functions:m},x,w),!this.excludeContentFromTrace){let b=l.map(v=>v.values),T=b.length===1?b[0]:b;x.addEvent("output",{content:JSON.stringify(T,null,2)})}}finally{x.end()}}finally{this.activeAbortControllers.delete(r),this._stopRequested=!1}}async forward(e,t,n){let r=n?.cachingFunction??this.options?.cachingFunction??de.cachingFunction,o=(()=>{if(!r)return;let d=this.signature.getInputFields().map(m=>m.name);return this.computeCacheKey(t,d)})();if(r&&o){let d=await r(o);if(d!==void 0)return d}let s=performance.now(),a=this.getSignatureName(),l=n?.stream??!1,u=!1,c=0,p=!1;try{let d=this.getMetricsInstruments(),m=this.getMergedCustomLabels(e,n);d&&ll(d,this.signature.getInputFields().length,this.signature.getOutputFields().length,this.examples?.length??0,this.demos?.length??0,a,m);let g=this._forward1(e,t,n??{}),h=[],f=0,I=0;for await(let F of g)F.version!==f&&(h=[]),f=F.version,h=ai(h,F),I++;c=f;let x=performance.now();p=!!n?.resultPicker;let A=await $o(h,{resultPicker:n?.resultPicker},n?.mem,n?.sessionId),w=performance.now()-x,T=h[A]?.delta??{},v=Array.isArray(t)?{}:t??{};if(this.trace={...v,...T},p&&this.isDebug(e,n)){let F=this.getLogger(e,n);oa(h.length,A,w,F)}if(u=!0,d&&(al(d,h.length,p,p?w:void 0,a,m),il(d,l,I,void 0,a,m)),r&&o)try{await r(o,T)}catch{}return T}catch(d){throw u=!1,d}finally{let d=performance.now()-s,m=this.getMetricsInstruments(),g=this.getMergedCustomLabels(e,n);m&&(nl(m,d,u,a,e.getName(),n?.model?String(n.model):void 0,g),c>0&&ri(m,c,u,n?.maxRetries??this.options?.maxRetries??3,a,g))}}async*streamingForward(e,t,n){let r=n?.cachingFunction??this.options?.cachingFunction??de.cachingFunction,o=(()=>{if(!r)return;let p=this.signature.getInputFields().map(d=>d.name);return this.computeCacheKey(t,p)})();if(r&&o){let p;try{p=await r(o)}catch{}if(p!==void 0){yield{version:0,index:0,delta:p};return}}if(!n?.resultPicker){yield*this._forward1(e,t,{...n,stream:!0});return}let s=this._forward1(e,t,{...n,stream:!0}),a=[],l=0;for await(let p of s)p.version!==l&&(a=[]),l=p.version,a=ai(a,p);let u=await $o(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??de.debug??e.getOptions().debug??!1}getLogger(e,t){return t?.logger??this.options?.logger??de.logger??e.getLogger()}computeCacheKey(e,t){let n=et("sha256");n.update(this.signature.hash()??"");let r=o=>{let s=typeof o;if(n.update(`|${s}|`),o==null){n.update("null");return}if(s==="string"||s==="number"||s==="boolean"){n.update(String(o));return}if(Array.isArray(o)){n.update("[");for(let a of o)r(a);n.update("]");return}if(typeof o=="object"&&o!==null&&"mimeType"in o&&"data"in o){let a=o;n.update(a.mimeType??"");let l=et("sha256").update(a.data??"").digest("hex");n.update(l);return}if(typeof o=="object"){let a=o,l=Object.keys(a).sort();for(let u of l)n.update(`{${u}}`),r(a[u]);return}n.update(String(o))};if(Array.isArray(e))for(let o of e){n.update(`role:${o.role}`);let s=t.map(a=>o.values?.[a]);for(let a of s)r(a)}else{let o=t.map(s=>e?.[s]);for(let s of o)r(s)}return n.digest("hex")}},Cr=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 Ti(i,e,t){let n=i instanceof Error?i:new Error(String(i));if(n instanceof Fe)return n;let r=(n.message||"").toLowerCase();if(r.includes("at least")||r.includes("at most")||r.includes("must match pattern")||r.includes("invalid url")||r.includes("required")||r.includes("missing")||r.includes("valid email")||r.includes("number must be")||n.name==="ValidationError"||n.name==="AssertionError"||n.name==="AxAssertionError"||n.stack?.includes("asserts.ts"))return n;let s=e.getLastUsedChatModel(),a=e.getLastUsedModelConfig(),l={model:s,maxTokens:a?.maxTokens,streaming:a?.stream??!1,signature:{input:t.getInputFields(),output:t.getOutputFields(),description:t.getDescription()}};return new Cr(`Generate failed: ${n.message}`,l,{cause:n})}var Zc=i=>i.replace(/^\W+|\W+$/g,""),ep=(i,e)=>{let t=i.search(e);if(t===-1)return[i];let n=i.match(e);if(!n)throw new Error("Match failed unexpectedly.");let r=i.substring(0,t),o=i.substring(t+n[0].length);return[r,o]},tp=i=>{let e=new Set,t=[];for(let n of i)e.has(n)||(e.add(n),t.push(n));return t},np=i=>{let e=i.match(/^(\d+)[.,\s]+(.*)$/);if(!e||e.length<3)throw new Error('line must start with a number, a dot and then text. e.g. "1. hello"');let t=Number.parseInt(e[1],10),n=e[2].trim();return{id:t,text:n}},rp=i=>{let e=i.match(/^(\d+)[.,\s]+(.*)$/);return e&&e[2]!==void 0?e[2].trim():i},op=(i,e)=>{let t=[];for(let n=0;n<i.length;n+=e)t.push(i.slice(n,n+e));return t},Do={trimNonAlphaNum:Zc,splitIntoTwo:ep,dedup:tp,extractIdAndText:np,extractIndexPrefixedText:rp,batchArray:op};var Uo=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."
378
+ 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}=Do.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 Bo=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 Bx=new Je,jo=class{name;context;constructor(e,t){this.name=e,this.context=t}getName(){return this.name}getContext(){return this.context}},qo=class{ai;db;debug;constructor(e){this.db=new Ot,this.ai=e}getState(){return this.db.getDB()}setState(e){this.db.setDB(e)}setClasses=async(e,t)=>{for(let n of e){let r=await this.ai.embed({texts:n.getContext()},{abortSignal:t?.abortSignal});await this.db.upsert({id:n.getName(),table:"classes",values:r.embeddings[0]})}};async forward(e,t){let{embeddings:n}=await this.ai.embed({texts:[e]},{abortSignal:t?.abortSignal}),o=(await this.db.query({table:"classes",values:n[0]})).matches;if(typeof t?.cutoff=="number"){let{cutoff:a}=t;o=o.filter(l=>l.score<=a)}let s=o.at(0);return s?s.id:""}setOptions(e){typeof e.debug=="boolean"&&(this.debug=e.debug)}};var Ql=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 Yl(i,e){return i.filter(t=>!e.has(t))}function Xl(i){let e={};for(let t of i)e[t]=(e[t]||0)+1;return e}function Bt(i){let e=i.normalize("NFD");return e=e.replace(/\b(a|an|the)\b/g," "),e=e.split(/\s+/).join(" "),e=e.replace(/[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/g,""),e.toLowerCase()}function sp(i,e){return Bt(i)===Bt(e)?1:0}function ip(i,e){let t=Bt(i).split(" "),n=Bt(e).split(" "),r=Xl(t),o=Xl(n),s=0;for(let u in r){let c=r[u]??0,p=o[u]??0;s+=Math.min(c,p)}if(s===0)return 0;let a=s/t.length,l=s/n.length;return 2*a*l/(a+l)}function ap(i,e,t,n=!1){let r=Bt(i).split(" "),o=Bt(e).split(" "),s=Bt(t).split(" "),a=new Set([...Ql,...r]);o=Yl(o,a),s=Yl(s,a);let l=0,u=l/o.length,c=l/s.length,p=2*u*c/(u+c);return n?c:p}var Zl={emScore:sp,f1Score:ip,novelF1ScoreOptimized:ap};var zo=class{ai;program;examples;constructor({ai:e,program:t,examples:n=[]}){if(n.length===0)throw new Error("No examples found");this.ai=e,this.program=t,this.examples=n}async run(e){let t=Date.now(),n=this.examples.length,r=0;for(let s=0;s<n;s++){let a=this.examples[s];if(!a)throw new Error("Invalid example");try{let l=await this.program.forward(this.ai,a,{maxRetries:1}),u=await e({prediction:l,example:a});r+=u}catch(l){console.warn(`Program evaluation failed for example ${s}: ${l instanceof Error?l.message:"Unknown error"}`)}}let o=n>0?r/n:0;this.ai.getOptions().debug&&console.log(`
379
379
  Performance: `,r,"/",n,"Average Score: ",o,`
380
380
  `)}};function Ho(i){return i.map(e=>{let t=e.type?.name??"string",n=e.description?`: ${e.description}`:"";return`- ${e.name} (${t})${n}`}).join(`
381
- `)}function ip(i,e){if(i===e)return!0;if(typeof i!=typeof e)return!1;if(typeof i=="object"&&i!==null&&e!==null){let t=JSON.stringify(i),n=JSON.stringify(e);return t===n}return!1}function ap(i){return i==null?!1:!!(typeof i=="string"||typeof i=="number"||typeof i=="boolean"||Array.isArray(i)&&i.every(e=>typeof e!="object"))}var Pn=class{signature;options;constructor(e,t){this.signature=e,this.options={ai:t.ai,randomizeOrder:t.randomizeOrder??!0,model:t.model,criteria:t.criteria}}async evaluate(e,t,n){return n&&ap(n)?this.runAbsolute(t,n):n?this.runRelativistic(e,t,n):this.runReferenceFree(e,t)}async runAbsolute(e,t){let n=this.signature.getOutputFields(),r=0,o=0,s=[];for(let u of n){let c=e[u.name],p=t[u.name];p!==void 0&&(o++,ip(c,p)?r++:s.push(`${u.name}: expected "${p}", got "${c}"`))}let a=o>0?r/o:0,l=a===1?"All fields match expected values exactly.":`Mismatches found: ${s.join("; ")}`;return{score:a,reasoning:l,mode:"absolute"}}async runRelativistic(e,t,n){let r=this.signature.getDescription(),o=this.signature.getInputFields(),s=this.signature.getOutputFields(),a=this.options.randomizeOrder?Math.random()>.5:!0,l=a?t:n,u=a?n:t,c=`
381
+ `)}function lp(i,e){if(i===e)return!0;if(typeof i!=typeof e)return!1;if(typeof i=="object"&&i!==null&&e!==null){let t=JSON.stringify(i),n=JSON.stringify(e);return t===n}return!1}function up(i){return i==null?!1:!!(typeof i=="string"||typeof i=="number"||typeof i=="boolean"||Array.isArray(i)&&i.every(e=>typeof e!="object"))}var En=class{signature;options;constructor(e,t){this.signature=e,this.options={ai:t.ai,randomizeOrder:t.randomizeOrder??!0,model:t.model,criteria:t.criteria}}async evaluate(e,t,n){return n&&up(n)?this.runAbsolute(t,n):n?this.runRelativistic(e,t,n):this.runReferenceFree(e,t)}async runAbsolute(e,t){let n=this.signature.getOutputFields(),r=0,o=0,s=[];for(let u of n){let c=e[u.name],p=t[u.name];p!==void 0&&(o++,lp(c,p)?r++:s.push(`${u.name}: expected "${p}", got "${c}"`))}let a=o>0?r/o:0,l=a===1?"All fields match expected values exactly.":`Mismatches found: ${s.join("; ")}`;return{score:a,reasoning:l,mode:"absolute"}}async runRelativistic(e,t,n){let r=this.signature.getDescription(),o=this.signature.getInputFields(),s=this.signature.getOutputFields(),a=this.options.randomizeOrder?Math.random()>.5:!0,l=a?t:n,u=a?n:t,c=`
382
382
  You are an impartial judge comparing two AI responses to the same input.
383
383
 
384
384
  **Task Description:** ${r||"Complete the task based on the input."}
@@ -396,7 +396,7 @@ ${Ho(s)}
396
396
  4. Provide clear reasoning for your decision
397
397
 
398
398
  **Important:** Do not be swayed by response length alone. Focus on quality and correctness.
399
- `.trim(),p=new ve(`
399
+ `.trim(),p=new Se(`
400
400
  input:string "The original input",
401
401
  response_a:string "Response A",
402
402
  response_b:string "Response B"
@@ -431,13 +431,13 @@ ${s}
431
431
  - unacceptable: Response is wrong, harmful, or completely off-topic
432
432
 
433
433
  First explain your reasoning, then classify the response into one of the quality tiers.
434
- `.trim(),l=new ve(`
434
+ `.trim(),l=new Se(`
435
435
  input:string "The original input",
436
436
  response:string "The AI response to evaluate"
437
437
  ->
438
438
  reasoning:string "Detailed explanation for the quality assessment",
439
439
  quality:class "excellent, good, acceptable, poor, unacceptable" "Quality tier"
440
- `);l.setInstruction(a);let u=await l.forward(this.options.ai,{input:JSON.stringify(e),response:JSON.stringify(t)},{model:this.options.model}),c={excellent:1,good:.8,acceptable:.5,poor:.2,unacceptable:0},p=u.quality.toLowerCase();return{score:c[p]??.5,reasoning:u.reasoning,mode:"reference-free",qualityTier:p}}toMetricFn(){return async e=>{let{example:t,prediction:n}=e,r={};for(let u of this.signature.getInputFields())u.name in t&&(r[u.name]=t[u.name]);let o={},s=!1;for(let u of this.signature.getOutputFields())u.name in t&&(o[u.name]=t[u.name],s=!0);let a=n;return(await this.evaluate(r,a,s?o:void 0)).score}}getSignature(){return this.signature}};function eu(){let i=Date.now().toString(36),e=Math.random().toString(36).substring(2,8);return`trace-${i}-${e}`}var Fn=class i{gen;options;constructor(e,t){this.gen=e,this.options={name:t.name,storage:t.storage,logInputs:t.logInputs??!0,logOutputs:t.logOutputs??!0,metadata:t.metadata,onTrace:t.onTrace,throwOnError:t.throwOnError??!1}}async*streamingForward(e,t,n){let r=eu(),o=new Date,s,a;try{let l=this.gen.streamingForward(e,t,n);for await(let u of l)u.partial?s=u.partial:u.delta,yield u}catch(l){throw a=l instanceof Error?l.message:String(l),l}finally{let l=new Date,u=l.getTime()-o.getTime(),c={type:"trace",id:r,name:this.options.name,input:this.options.logInputs?t:{},output:this.options.logOutputs&&s?s:{},startTime:o,endTime:l,durationMs:u,model:n?.model??void 0,metadata:this.options.metadata,error:a};this.saveTrace(c)}}async forward(e,t,n){let r=eu(),o=new Date,s,a;try{return s=await this.gen.forward(e,t,n),s}catch(l){throw a=l instanceof Error?l.message:String(l),l}finally{let l=new Date,u=l.getTime()-o.getTime(),c={type:"trace",id:r,name:this.options.name,input:this.options.logInputs?t:{},output:this.options.logOutputs&&s?s:{},startTime:o,endTime:l,durationMs:u,model:n?.model??void 0,metadata:this.options.metadata,error:a};this.saveTrace(c)}}async saveTrace(e){try{await this.options.storage.save(this.options.name,e),this.options.onTrace&&this.options.onTrace(e)}catch(t){if(this.options.throwOnError)throw t;console.warn("AxTraceLogger: Failed to save trace:",t)}}getGen(){return this.gen}getName(){return this.options.name}getStorage(){return this.options.storage}setMetadata(e){this.options.metadata=e}clone(e){return new i(e??this.gen.clone(),{name:this.options.name,storage:this.options.storage,logInputs:this.options.logInputs,logOutputs:this.options.logOutputs,metadata:this.options.metadata,onTrace:this.options.onTrace,throwOnError:this.options.throwOnError})}};var tu=i=>{console.log(i)},wi=(i=tu)=>{let e=new Ye,t=e.gray("\u2500".repeat(50)),n=e.gray("\u2501".repeat(50));return r=>{let o="";switch(r.name){case"OptimizationStart":o=`
440
+ `);l.setInstruction(a);let u=await l.forward(this.options.ai,{input:JSON.stringify(e),response:JSON.stringify(t)},{model:this.options.model}),c={excellent:1,good:.8,acceptable:.5,poor:.2,unacceptable:0},p=u.quality.toLowerCase();return{score:c[p]??.5,reasoning:u.reasoning,mode:"reference-free",qualityTier:p}}toMetricFn(){return async e=>{let{example:t,prediction:n}=e,r={};for(let u of this.signature.getInputFields())u.name in t&&(r[u.name]=t[u.name]);let o={},s=!1;for(let u of this.signature.getOutputFields())u.name in t&&(o[u.name]=t[u.name],s=!0);let a=n;return(await this.evaluate(r,a,s?o:void 0)).score}}getSignature(){return this.signature}};function eu(){let i=Date.now().toString(36),e=Math.random().toString(36).substring(2,8);return`trace-${i}-${e}`}var Pn=class i{gen;options;constructor(e,t){this.gen=e,this.options={name:t.name,storage:t.storage,logInputs:t.logInputs??!0,logOutputs:t.logOutputs??!0,metadata:t.metadata,onTrace:t.onTrace,throwOnError:t.throwOnError??!1}}async*streamingForward(e,t,n){let r=eu(),o=new Date,s,a;try{let l=this.gen.streamingForward(e,t,n);for await(let u of l)u.partial?s=u.partial:u.delta,yield u}catch(l){throw a=l instanceof Error?l.message:String(l),l}finally{let l=new Date,u=l.getTime()-o.getTime(),c={type:"trace",id:r,name:this.options.name,input:this.options.logInputs?t:{},output:this.options.logOutputs&&s?s:{},startTime:o,endTime:l,durationMs:u,model:n?.model??void 0,metadata:this.options.metadata,error:a};this.saveTrace(c)}}async forward(e,t,n){let r=eu(),o=new Date,s,a;try{return s=await this.gen.forward(e,t,n),s}catch(l){throw a=l instanceof Error?l.message:String(l),l}finally{let l=new Date,u=l.getTime()-o.getTime(),c={type:"trace",id:r,name:this.options.name,input:this.options.logInputs?t:{},output:this.options.logOutputs&&s?s:{},startTime:o,endTime:l,durationMs:u,model:n?.model??void 0,metadata:this.options.metadata,error:a};this.saveTrace(c)}}async saveTrace(e){try{await this.options.storage.save(this.options.name,e),this.options.onTrace&&this.options.onTrace(e)}catch(t){if(this.options.throwOnError)throw t;console.warn("AxTraceLogger: Failed to save trace:",t)}}getGen(){return this.gen}getName(){return this.options.name}getStorage(){return this.options.storage}setMetadata(e){this.options.metadata=e}clone(e){return new i(e??this.gen.clone(),{name:this.options.name,storage:this.options.storage,logInputs:this.options.logInputs,logOutputs:this.options.logOutputs,metadata:this.options.metadata,onTrace:this.options.onTrace,throwOnError:this.options.throwOnError})}};var tu=i=>{console.log(i)},wi=(i=tu)=>{let e=new Je,t=e.gray("\u2500".repeat(50)),n=e.gray("\u2501".repeat(50));return r=>{let o="";switch(r.name){case"OptimizationStart":o=`
441
441
  ${e.blueBright("\u25CF ")}${e.whiteBright("Optimization Started")}
442
442
  ${t}
443
443
  ${e.white("Optimizer:")} ${e.cyan(r.value.optimizerType)}
@@ -506,7 +506,7 @@ Score: ${t.value.score.toFixed(3)}
506
506
  Config: ${JSON.stringify(t.value.config)}
507
507
  ${e}`;break;default:n=`[ UNKNOWN OPTIMIZER EVENT ]
508
508
  ${JSON.stringify(t)}
509
- ${e}`}i(n)}},wr=wi();var Ri={enabled:!0,enabledCategories:["optimization","convergence","resource_usage","teacher_student","checkpointing","pareto"],maxLabelLength:100,samplingRate:1},Ko,lp=i=>{if(Ko)return Ko;if(i)return Ko=up(i),Ko};var Wo=Ri,ru=i=>{Wo={...Wo,...i}},ou=()=>({...Wo}),up=i=>({optimizationLatencyHistogram:i.createHistogram("ax_optimizer_optimization_duration_ms",{description:"End-to-end duration of optimization runs",unit:"ms"}),optimizationRequestsCounter:i.createCounter("ax_optimizer_optimization_requests_total",{description:"Total number of optimization requests"}),optimizationErrorsCounter:i.createCounter("ax_optimizer_optimization_errors_total",{description:"Total number of failed optimizations"}),convergenceRoundsHistogram:i.createHistogram("ax_optimizer_convergence_rounds",{description:"Number of rounds until convergence"}),convergenceScoreGauge:i.createGauge("ax_optimizer_convergence_score",{description:"Current best score during optimization"}),convergenceImprovementGauge:i.createGauge("ax_optimizer_convergence_improvement",{description:"Improvement in score from baseline"}),stagnationRoundsGauge:i.createGauge("ax_optimizer_stagnation_rounds",{description:"Number of rounds without improvement"}),earlyStoppingCounter:i.createCounter("ax_optimizer_early_stopping_total",{description:"Total number of early stopping events"}),tokenUsageCounter:i.createCounter("ax_optimizer_token_usage_total",{description:"Total tokens used during optimization"}),costUsageCounter:i.createCounter("ax_optimizer_cost_usage_total",{description:"Total cost incurred during optimization",unit:"$"}),memoryUsageGauge:i.createGauge("ax_optimizer_memory_usage_bytes",{description:"Peak memory usage during optimization",unit:"By"}),optimizationDurationHistogram:i.createHistogram("ax_optimizer_duration_ms",{description:"Duration of optimization runs",unit:"ms"}),teacherStudentUsageCounter:i.createCounter("ax_optimizer_teacher_student_usage_total",{description:"Total number of teacher-student interactions"}),teacherStudentLatencyHistogram:i.createHistogram("ax_optimizer_teacher_student_latency_ms",{description:"Latency of teacher-student interactions",unit:"ms"}),teacherStudentScoreImprovementGauge:i.createGauge("ax_optimizer_teacher_student_score_improvement",{description:"Score improvement from teacher-student interactions"}),checkpointSaveCounter:i.createCounter("ax_optimizer_checkpoint_save_total",{description:"Total number of checkpoint saves"}),checkpointLoadCounter:i.createCounter("ax_optimizer_checkpoint_load_total",{description:"Total number of checkpoint loads"}),checkpointSaveLatencyHistogram:i.createHistogram("ax_optimizer_checkpoint_save_latency_ms",{description:"Latency of checkpoint save operations",unit:"ms"}),checkpointLoadLatencyHistogram:i.createHistogram("ax_optimizer_checkpoint_load_latency_ms",{description:"Latency of checkpoint load operations",unit:"ms"}),paretoOptimizationsCounter:i.createCounter("ax_optimizer_pareto_optimizations_total",{description:"Total number of Pareto optimizations"}),paretoFrontSizeHistogram:i.createHistogram("ax_optimizer_pareto_front_size",{description:"Size of Pareto frontier"}),paretoHypervolumeGauge:i.createGauge("ax_optimizer_pareto_hypervolume",{description:"Hypervolume of Pareto frontier"}),paretoSolutionsGeneratedHistogram:i.createHistogram("ax_optimizer_pareto_solutions_generated",{description:"Number of solutions generated for Pareto optimization"}),programInputFieldsGauge:i.createGauge("ax_optimizer_program_input_fields",{description:"Number of input fields in optimized program"}),programOutputFieldsGauge:i.createGauge("ax_optimizer_program_output_fields",{description:"Number of output fields in optimized program"}),examplesCountGauge:i.createGauge("ax_optimizer_examples_count",{description:"Number of training examples used"}),validationSetSizeGauge:i.createGauge("ax_optimizer_validation_set_size",{description:"Size of validation set used"}),evaluationLatencyHistogram:i.createHistogram("ax_optimizer_evaluation_latency_ms",{description:"Latency of program evaluations",unit:"ms"}),demoGenerationLatencyHistogram:i.createHistogram("ax_optimizer_demo_generation_latency_ms",{description:"Latency of demo generation",unit:"ms"}),metricComputationLatencyHistogram:i.createHistogram("ax_optimizer_metric_computation_latency_ms",{description:"Latency of metric computation",unit:"ms"}),optimizerTypeGauge:i.createGauge("ax_optimizer_type",{description:"Type of optimizer being used"}),targetScoreGauge:i.createGauge("ax_optimizer_target_score",{description:"Target score for optimization"}),maxRoundsGauge:i.createGauge("ax_optimizer_max_rounds",{description:"Maximum rounds for optimization"})}),ft=i=>{let e={};for(let[t,n]of Object.entries(i))if(n!=null){let r=String(n),o=Wo.maxLabelLength;e[t]=r.length>o?r.substring(0,o):r}return e},cp=(i,e,t,n,r,o)=>{try{let s=ft({success:t.toString(),optimizer_type:n,...r?{program_signature:r}:{},...o});i.optimizationLatencyHistogram&&i.optimizationLatencyHistogram.record(e,s),i.optimizationRequestsCounter&&i.optimizationRequestsCounter.add(1,s),!t&&i.optimizationErrorsCounter&&i.optimizationErrorsCounter.add(1,s)}catch(s){console.warn("Failed to record optimization metric:",s)}},pp=(i,e,t,n,r,o,s)=>{try{let a=ft({optimizer_type:o,...s});i.convergenceRoundsHistogram&&i.convergenceRoundsHistogram.record(e,a),i.convergenceScoreGauge&&i.convergenceScoreGauge.record(t,a),i.convergenceImprovementGauge&&i.convergenceImprovementGauge.record(n,a),i.stagnationRoundsGauge&&i.stagnationRoundsGauge.record(r,a)}catch(a){console.warn("Failed to record convergence metric:",a)}},dp=(i,e,t,n)=>{try{let r=ft({reason:e,optimizer_type:t,...n});i.earlyStoppingCounter&&i.earlyStoppingCounter.add(1,r)}catch(r){console.warn("Failed to record early stopping metric:",r)}},mp=(i,e,t,n,r,o)=>{try{let s=ft({optimizer_type:n,...o});i.tokenUsageCounter&&i.tokenUsageCounter.add(e,s),i.costUsageCounter&&i.costUsageCounter.add(t,s),r!==void 0&&i.memoryUsageGauge&&i.memoryUsageGauge.record(r,s)}catch(s){console.warn("Failed to record resource usage metric:",s)}},gp=(i,e,t,n)=>{try{let r=ft({optimizer_type:t,...n});i.optimizationDurationHistogram&&i.optimizationDurationHistogram.record(e,r)}catch(r){console.warn("Failed to record optimization duration metric:",r)}},hp=(i,e,t,n,r)=>{try{let o=ft({optimizer_type:n,...r});i.teacherStudentUsageCounter&&i.teacherStudentUsageCounter.add(1,o),i.teacherStudentLatencyHistogram&&i.teacherStudentLatencyHistogram.record(e,o),i.teacherStudentScoreImprovementGauge&&i.teacherStudentScoreImprovementGauge.record(t,o)}catch(o){console.warn("Failed to record teacher-student metric:",o)}},fp=(i,e,t,n,r,o)=>{try{let s=ft({operation:e,success:n.toString(),optimizer_type:r,...o});e==="save"?(i.checkpointSaveCounter&&i.checkpointSaveCounter.add(1,s),i.checkpointSaveLatencyHistogram&&i.checkpointSaveLatencyHistogram.record(t,s)):(i.checkpointLoadCounter&&i.checkpointLoadCounter.add(1,s),i.checkpointLoadLatencyHistogram&&i.checkpointLoadLatencyHistogram.record(t,s))}catch(s){console.warn("Failed to record checkpoint metric:",s)}},xp=(i,e,t,n,r,o)=>{try{let s=ft({optimizer_type:n,...o});i.paretoOptimizationsCounter&&i.paretoOptimizationsCounter.add(1,s),i.paretoFrontSizeHistogram&&i.paretoFrontSizeHistogram.record(e,s),r!==void 0&&i.paretoHypervolumeGauge&&i.paretoHypervolumeGauge.record(r,s),i.paretoSolutionsGeneratedHistogram&&i.paretoSolutionsGeneratedHistogram.record(t,s)}catch(s){console.warn("Failed to record Pareto metric:",s)}},Ap=(i,e,t,n,r,o,s)=>{try{let a=ft({optimizer_type:o,...s});i.programInputFieldsGauge&&i.programInputFieldsGauge.record(e,a),i.programOutputFieldsGauge&&i.programOutputFieldsGauge.record(t,a),i.examplesCountGauge&&i.examplesCountGauge.record(n,a),i.validationSetSizeGauge&&i.validationSetSizeGauge.record(r,a)}catch(a){console.warn("Failed to record program complexity metric:",a)}},yp=(i,e,t,n,r)=>{try{let o=ft({metric_type:e,optimizer_type:n,...r});switch(e){case"evaluation":i.evaluationLatencyHistogram&&i.evaluationLatencyHistogram.record(t,o);break;case"demo_generation":i.demoGenerationLatencyHistogram&&i.demoGenerationLatencyHistogram.record(t,o);break;case"metric_computation":i.metricComputationLatencyHistogram&&i.metricComputationLatencyHistogram.record(t,o);break}}catch(o){console.warn("Failed to record optimizer performance metric:",o)}},bp=(i,e,t,n,r)=>{try{let o=ft({optimizer_type:e,...r});i.optimizerTypeGauge&&i.optimizerTypeGauge.record(1,o),t!==void 0&&i.targetScoreGauge&&i.targetScoreGauge.record(t,o),n!==void 0&&i.maxRoundsGauge&&i.maxRoundsGauge.record(n,o)}catch(o){console.warn("Failed to record optimizer configuration metric:",o)}},It=class{bestScore;stats;instruction;demos;examples;modelConfig;optimizerType;optimizationTime;totalRounds;converged;scoreHistory;configurationHistory;constructor(e){this.bestScore=e.bestScore,this.stats=e.stats,this.instruction=e.instruction,this.demos=e.demos,this.examples=e.examples,this.modelConfig=e.modelConfig,this.optimizerType=e.optimizerType,this.optimizationTime=e.optimizationTime,this.totalRounds=e.totalRounds,this.converged=e.converged,this.scoreHistory=e.scoreHistory,this.configurationHistory=e.configurationHistory}applyTo(e){this.demos&&this.demos.length>0?e.setDemos(this.demos,{modelConfig:this.modelConfig}):this.modelConfig&&e.setDemos([],{modelConfig:this.modelConfig})}},Rr=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}},Ze=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 Rr({maxTokens:1e6});this.costTracker=e.costTracker??t,this.metricsInstruments=lp(ce.meter),this.stats=this.initializeStats(),this.debugOptimizer=e.debugOptimizer??!1,this.optimizerLogger=e.optimizerLogger??(this.verbose?wr:void 0),this.initializeResultExplainer()}getMergedCustomLabels(e){return Ft(ce.customLabels,this.studentAI?.getOptions?.()?.customLabels,this.teacherAI?.getOptions?.()?.customLabels,e?.customLabels)}initializeResultExplainer(){this.resultExplainer=void 0}initializeStats(){return{totalCalls:0,successfulDemos:0,estimatedTokenUsage:0,earlyStopped:!1,resourceUsage:{totalTokens:0,totalTime:0,avgLatencyPerEval:0,costByModel:{}},convergenceInfo:{converged:!1,finalImprovement:0,stagnationRounds:0,convergenceThreshold:.01},bestScore:0,bestConfiguration:{}}}setupRandomSeed(){this.seed!==void 0&&(Math.random=(()=>{let e=this.seed;return()=>(e=(e*9301+49297)%233280,e/233280)})())}checkCostLimits(){return this.costTracker?.isLimitReached()??!1}checkTargetScore(e){return this.targetScore!==void 0&&e>=this.targetScore}updateResourceUsage(e,t=0){this.stats.resourceUsage.totalTime=Date.now()-e,this.stats.resourceUsage.totalTokens+=t,this.stats.totalCalls>0&&(this.stats.resourceUsage.avgLatencyPerEval=this.stats.resourceUsage.totalTime/this.stats.totalCalls)}triggerEarlyStopping(e,t,n){this.stats.earlyStopped=!0,this.stats.earlyStopping={bestScoreRound:t,patienceExhausted:e.includes("improvement"),reason:e},this.recordEarlyStoppingMetrics(e,"unknown",n),this.onEarlyStop&&this.onEarlyStop(e,this.stats),this.getOptimizerLogger()?.({name:"EarlyStopping",value:{reason:e,finalScore:this.stats.bestScore??0,round:t}})}validateExamples(e,t=!0){if(!e||e.length===0)throw new Error("At least 1 example is required for optimization");if(t&&e.length<2)throw new Error("At least 2 examples are required for optimization with auto-splitting. Provide more examples to enable proper train/validation split.");let n=t?10:5;e.length<n&&this.verbose&&console.warn(`[Ax Optimizer] Warning: Only ${e.length} examples provided. Consider providing more examples (${n}+ recommended) for better optimization results.`)}getAIService(e=!1,t){return e&&t?.overrideTeacherAI?t.overrideTeacherAI:e&&this.teacherAI?this.teacherAI:this.studentAI}hasTeacherAI(e){return e?.overrideTeacherAI!==void 0||this.teacherAI!==void 0}getTeacherOrStudentAI(e){return e?.overrideTeacherAI||this.teacherAI||this.studentAI}async executeWithTeacher(e,t=!0,n){let r=this.getAIService(t,n);return await e(r)}async*compileStream(e,t,n,r){let o=Date.now(),s=this.constructor.name,a=e.getSignature().toString();this.recordOptimizationStart(s,a,r);let l,u=(g,h,f,b,x,A,w,I={},T)=>{this.getOptimizerLogger(T)?.({name:"RoundProgress",value:{round:g,totalRounds:T?.maxIterations??0,currentScore:h,bestScore:A,configuration:f}}),this.updateOptimizationProgress(g,h,f,b,x,A,w,I,T)},c=(g,h)=>{l=g,this.triggerEarlyStopping(g,this.currentRound,r)},p=g=>{this.onProgress?.(g),u(g.round,g.currentScore,g.currentConfiguration||{},s,{},g.bestScore,g.bestConfiguration,g.convergenceInfo,r)},d=await this.compile(e,t,n,{...r,overrideOnProgress:p,overrideOnEarlyStop:c}),m=Date.now()-o;return this.recordOptimizationComplete(m,!0,s,a,r),l&&this.getLogger(r)?.({name:"Notification",id:"optimization_early_stop",value:`Optimization stopped early due to ${l}`}),{demos:d.demos,stats:d.stats,bestScore:d.bestScore,finalConfiguration:d.finalConfiguration,scoreHistory:d.scoreHistory,configurationHistory:d.configurationHistory}}async compilePareto(e,t,n,r){let o=this.constructor.name,s=Date.now(),a=await this.generateWeightedSolutions(e,t,n,r),l=await this.generateConstraintSolutions(e,t,n,r),u=[...a,...l],c=this.findParetoFrontier(u),p=this.calculateHypervolume(c);this.updateResourceUsage(s),this.stats.convergenceInfo.converged=!0,this.recordParetoMetrics(c.length,u.length,"base_optimizer",p,r);let d=c.length>0?Math.max(...c.map(m=>Math.max(...Object.values(m.scores)))):0;return{demos:c.length>0?[...c[0].demos]:void 0,stats:this.stats,bestScore:d,paretoFront:c,hypervolume:p,paretoFrontSize:c.length,finalConfiguration:{paretoFrontSize:c.length,hypervolume:p,strategy:"weighted_combinations_and_constraints",numSolutions:u.length}}}async generateWeightedSolutions(e,t,n,r){let o=[];if(!t||t.length===0)throw new Error("No examples provided for Pareto optimization");let s=t[0],a=await e.forward(this.getAIService(!1,r),s),l=await n({prediction:a,example:s}),u=Object.keys(l),c=this.generateWeightCombinations(u);for(let p=0;p<c.length;p++){let d=c[p],m=async({prediction:g,example:h})=>{let f=await n({prediction:g,example:h}),b=0;for(let[x,A]of Object.entries(f))b+=A*(d[x]||0);return b};try{let g=await this.compile(e,t,m,{...r,verbose:!1}),h=await this.evaluateWithMultiObjective(e,g,n,t);o.push({scores:h,demos:g.demos,configuration:{...g.finalConfiguration,weights:d,strategy:"weighted_combination"}})}catch{}}return o}async generateConstraintSolutions(e,t,n,r){let o=[];if(!t||t.length===0)throw new Error("No examples provided for multi-objective optimization");let s=t[0],a=await e.forward(this.getAIService(!1,r),s),l=await n({prediction:a,example:s}),u=Object.keys(l);for(let c of u){let p=async({prediction:d,example:m})=>{let g=await n({prediction:d,example:m}),h=g[c]||0,f=0;for(let[b,x]of Object.entries(g))b!==c&&x<.3&&(f+=(.3-x)*2);return h-f};try{let d=await this.compile(e,t,p,{...r,verbose:!1}),m=await this.evaluateWithMultiObjective(e,d,n,t);o.push({scores:m,demos:d.demos,configuration:{...d.finalConfiguration,primaryObjective:c,strategy:"constraint_based"}})}catch{}}return o}generateWeightCombinations(e){let t=[];for(let r of e){let o={};for(let s of e)o[s]=s===r?1:0;t.push(o)}let n={};for(let r of e)n[r]=1/e.length;if(t.push(n),e.length===2){let[r,o]=e;for(let s=.1;s<=.9;s+=.2){let a=1-s;t.push({[r]:s,[o]:a})}}if(e.length===3){let[r,o,s]=e;t.push({[r]:.5,[o]:.3,[s]:.2},{[r]:.3,[o]:.5,[s]:.2},{[r]:.2,[o]:.3,[s]:.5})}return t}async evaluateWithMultiObjective(e,t,n,r){let o=new ve(e.getSignature());t.demos&&o.setDemos(t.demos);let s=[],a=Math.max(1,Math.min(5,Math.floor(r.length*.2))),l=r.slice(-a),u={},c=l;for(let d of c)try{let m=await o.forward(this.studentAI,d),g=await n({prediction:m,example:d});for(let[h,f]of Object.entries(g))u[h]||(u[h]=[]),u[h].push(f)}catch{}let p={};for(let[d,m]of Object.entries(u))p[d]=m.length>0?m.reduce((g,h)=>g+h,0)/m.length:0;return p}findParetoFrontier(e){let t=[];for(let n=0;n<e.length;n++){let r=e[n],o=!1,s=0;for(let a=0;a<e.length;a++){if(n===a)continue;let l=e[a];if(this.dominates(l.scores,r.scores)){o=!0;break}this.dominates(r.scores,l.scores)&&s++}o||t.push({demos:r.demos||[],scores:r.scores,configuration:r.configuration,dominatedSolutions:s})}return t}dominates(e,t){let n=Object.keys(e),r=!0,o=!1;for(let s of n){let a=e[s]||0,l=t[s]||0;if(a<l){r=!1;break}a>l&&(o=!0)}return r&&o}calculateHypervolume(e){if(e.length===0)return;let t=e[0],n=Object.keys(t.scores);if(n.length===2){let[r,o]=n,s=0,a=[...e].sort((u,c)=>(c.scores[r]||0)-(u.scores[r]||0)),l=0;for(let u of a){let c=u.scores[r]||0,p=u.scores[o]||0;s+=c*(p-l),l=Math.max(l,p)}return s}}async saveCheckpoint(e,t,n,r,o={},s){let a=s?.overrideCheckpointSave||this.checkpointSave;if(!a)return;let l=Date.now(),u=!1,c;try{let p={version:"1.0.0",timestamp:Date.now(),optimizerType:e,optimizerConfig:t,currentRound:this.currentRound,totalRounds:this.stats.resourceUsage.totalTime>0?this.currentRound:0,bestScore:n,bestConfiguration:r,scoreHistory:[...this.scoreHistory],configurationHistory:[...this.configurationHistory],stats:{...this.stats},optimizerState:o,examples:[]};c=await a(p),u=!0}catch(p){throw u=!1,p}finally{let p=Date.now()-l;this.recordCheckpointMetrics("save",p,u,e,s)}return c}async loadCheckpoint(e,t){let n=t?.overrideCheckpointLoad||this.checkpointLoad;if(!n)return null;let r=Date.now(),o=!1,s=null;try{s=await n(e),o=s!==null}catch(a){throw o=!1,a}finally{let a=Date.now()-r;this.recordCheckpointMetrics("load",a,o,"unknown",t)}return s}restoreFromCheckpoint(e){this.currentRound=e.currentRound,this.scoreHistory=[...e.scoreHistory],this.configurationHistory=[...e.configurationHistory],this.stats={...e.stats}}shouldSaveCheckpoint(e,t){let n=t?.overrideCheckpointInterval||this.checkpointInterval;return n!==void 0&&e%n===0}async updateOptimizationProgress(e,t,n,r,o,s,a,l={},u){this.currentRound=e,this.scoreHistory.push(t),this.configurationHistory.push(n),this.shouldSaveCheckpoint(e,u)&&await this.saveCheckpoint(r,o,s,a,l,u),this.getOptimizerLogger(u)?.({name:"RoundProgress",value:{round:e,totalRounds:u?.maxIterations??0,currentScore:t,bestScore:s,configuration:n}})}async saveFinalCheckpoint(e,t,n,r,o={},s){s?.saveCheckpointOnComplete!==!1&&await this.saveCheckpoint(e,t,n,r,{...o,final:!0},s)}getLogger(e){if(this.isLoggingEnabled(e))return this.logger?this.logger:this.studentAI.getLogger()}isLoggingEnabled(e){return e?.verbose!==void 0?e.verbose:this.verbose??!0}recordOptimizationStart(e,t,n){if(!this.metricsInstruments)return;let r=this.getMergedCustomLabels(n);if(t){let o=(t.match(/input:/g)||[]).length,s=(t.match(/output:/g)||[]).length;Ap(this.metricsInstruments,o,s,0,0,e,r)}bp(this.metricsInstruments,e,this.targetScore,void 0,r)}recordOptimizationComplete(e,t,n,r,o){if(!this.metricsInstruments)return;let s=this.getMergedCustomLabels(o);cp(this.metricsInstruments,e,t,n,r,s),gp(this.metricsInstruments,e,n,s);let a=this.costTracker?.getCurrentCost()??0,l=this.costTracker?.getTotalTokens()??0;mp(this.metricsInstruments,l,a,n,void 0,s)}recordConvergenceMetrics(e,t,n,r,o,s){if(!this.metricsInstruments)return;let a=this.getMergedCustomLabels(s);pp(this.metricsInstruments,e,t,n,r,o,a)}recordEarlyStoppingMetrics(e,t,n){if(!this.metricsInstruments)return;let r=this.getMergedCustomLabels(n);dp(this.metricsInstruments,e,t,r)}recordTeacherStudentMetrics(e,t,n,r){if(!this.metricsInstruments)return;let o=this.getMergedCustomLabels(r);hp(this.metricsInstruments,e,t,n,o)}recordCheckpointMetrics(e,t,n,r,o){if(!this.metricsInstruments)return;let s=this.getMergedCustomLabels(o);fp(this.metricsInstruments,e,t,n,r,s)}recordParetoMetrics(e,t,n,r,o){if(!this.metricsInstruments)return;let s=this.getMergedCustomLabels(o);xp(this.metricsInstruments,e,t,n,r,s)}recordPerformanceMetrics(e,t,n,r){if(!this.metricsInstruments)return;let o=this.getMergedCustomLabels(r);yp(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??ce.optimizerLogger??wr}getStats(){return{...this.stats}}async explainOptimizationResults(e,t,n){let r=this.stats.convergenceInfo.converged,o=this.stats.totalCalls,s=o>0?this.stats.successfulDemos/o*100:0,a=`Optimization finished with best score ${e.toFixed(3)}${t?` using configuration ${JSON.stringify(t)}`:""}. Convergence: ${r?"yes":"no"}. Success rate: ${s.toFixed(1)}%.`,l=[];if(r||l.push("Increase numTrials or relax earlyStoppingTrials to allow further improvement."),typeof this.targetScore=="number"&&e<this.targetScore&&l.push("Tighten the metric or supply more/better-labeled examples to reach targetScore."),t&&"bootstrappedDemos"in t){let c=t.bootstrappedDemos;typeof c=="number"&&c===0&&l.push("Consider allowing a small number of bootstrapped demos to boost performance.")}l.length===0&&l.push("Re-run with more trials or different acquisition settings to explore more of the space.");let u=`Tokens used: ${this.stats.resourceUsage.totalTokens}, rounds: ${this.currentRound}, stagnationRounds: ${this.stats.convergenceInfo.stagnationRounds}.`;return{humanExplanation:a,recommendations:l,performanceAssessment:u}}async logOptimizationComplete(e,t,n,r,o){let s=this.getOptimizerLogger(r);s&&s(o?{name:"OptimizationComplete",value:{optimizerType:e,bestScore:t,bestConfiguration:n||{},totalCalls:this.stats.totalCalls,successRate:this.stats.totalCalls>0?`${(this.stats.successfulDemos/this.stats.totalCalls*100).toFixed(1)}%`:"N/A",explanation:o.humanExplanation,recommendations:o.recommendations,performanceAssessment:o.performanceAssessment,stats:this.stats}}:{name:"OptimizationComplete",value:{optimizerType:e,bestScore:t,bestConfiguration:n||{},totalCalls:this.stats.totalCalls,successRate:this.stats.totalCalls>0?`${(this.stats.successfulDemos/this.stats.totalCalls*100).toFixed(1)}%`:"N/A",stats:this.stats}})}reset(){this.stats=this.initializeStats(),this.costTracker?.reset(),this.currentRound=0,this.scoreHistory=[],this.configurationHistory=[]}};function su(i){return Oe.create(i)}function We(i,e){let t=typeof i=="string"?Oe.create(i):i;return new ve(t,e)}function iu(i,e,t=0){let n=new Set([...Object.keys(i),...Object.keys(e)]),r=!0,o=!1;for(let s of n){let a=i[s]??0,l=e[s]??0;if(a+t<l){r=!1;break}a>l+t&&(o=!0)}return r&&o}function Ct(i,e=0){let t=[];for(let n=0;n<i.length;n++){let r=0,o=!1;for(let s=0;s<i.length;s++)if(n!==s){if(iu(i[s].scores,i[n].scores,e)){o=!0;break}iu(i[n].scores,i[s].scores,e)&&r++}o||t.push({idx:i[n].idx,scores:i[n].scores,dominated:r})}return t}function ct(i){if(i.length===0)return;let e=Object.keys(i[0]??{});if(e.length!==2)return;let[t,n]=e,r=[...i].sort((a,l)=>(l[t]??0)-(a[t]??0)),o=0,s=0;for(let a of r){let l=a[t]??0,u=a[n]??0,c=Math.max(u-s,0);o+=l*c,s=Math.max(s,u)}return o}function jt(i){if(i.length===0)return 0;let e=0;for(let t of i)e+=t;return e/i.length}function Vo(i){let e={},t={};for(let r of i)for(let[o,s]of Object.entries(r))e[o]=(e[o]||0)+(typeof s=="number"?s:0),t[o]=(t[o]||0)+1;let n={};for(let[r,o]of Object.entries(e))n[r]=o/Math.max(t[r]||1,1);return n}function Sr(i,e){let t=new Set;for(let c of i)for(let p of c)t.add(p);let r=[...Array.from(t)].sort((c,p)=>(e[c]??0)-(e[p]??0)),o=new Set,s=(c,p)=>{for(let d of i){if(!d.has(c))continue;let m=!1;for(let g of p)if(d.has(g)){m=!0;break}if(!m)return!1}return!0},a=!0;for(;a;){a=!1;for(let c of r){if(o.has(c))continue;let p=new Set(r.filter(d=>d!==c&&!o.has(d)));if(s(c,p)){o.add(c),a=!0;break}}}let l=r.filter(c=>!o.has(c)),u=new Set(l);return i.map(c=>{let p=new Set;for(let d of c)u.has(d)&&p.add(d);return p})}function vr(i,e,t){let n=Sr(i,e),r={};for(let l of n)for(let u of l)r[u]=(r[u]||0)+1;let o=[];for(let[l,u]of Object.entries(r)){let c=Number(l);for(let p=0;p<u;p++)o.push(c)}if(o.length===0)return 0;let s=typeof t=="function"?t():Math.random(),a=Math.floor(s*o.length);return o[a]}var _n=class i extends Ze{numTrials;minibatch;minibatchSize;earlyStoppingTrials;minImprovementThreshold;sampleCount;paretoSetSize;crossoverEvery;tieEpsilon;feedbackMemorySize;feedbackMemory=[];mergeMax;mergesUsed=0;mergesDue=0;totalMergesTested=0;lastIterFoundNewProgram=!1;mergeAttemptKeys=new Set;mergeCompositionKeys=new Set;static REFLECTION_PROMPT_TEMPLATE="I provided an assistant with the following instructions to perform a task for me:\n```\n<curr_instructions>\n```\n\nThe following are examples of different task inputs provided to the assistant along with the assistant's response for each of them, and some feedback on how the assistant's response could be better:\n```\n<inputs_outputs_feedback>\n```\n\nYour task is to write a new instruction for the assistant. Read the inputs carefully and identify the input format and infer detailed task description about the task I wish to solve with the assistant. Read all the assistant responses and the corresponding feedback. Identify all niche and domain specific factual information about the task and include it in the instruction, as a lot of it may not be available to the assistant in the future. The assistant may have utilized a generalizable strategy to solve the task, if so, include that in the instruction as well. Provide the new instructions within ``` blocks.";rngState=123456789;samplerState={epoch:-1,shuffled:[],freq:new Map};localScoreHistory=[];localConfigurationHistory=[];constructor(e){super(e);let t=e?.seed,n=Number.isFinite(t)?Math.floor(Number(t)):0;this.rngState=n&&n!==0?n:123456789,this.numTrials=e.numTrials??30,this.minibatch=e.minibatch??!0,this.minibatchSize=e.minibatchSize??20,this.earlyStoppingTrials=e.earlyStoppingTrials??5,this.minImprovementThreshold=e.minImprovementThreshold??0,this.sampleCount=e.sampleCount??1;let r=e?.paretoSetSize;this.paretoSetSize=r&&r>0?Math.min(1e3,Math.max(5,Math.floor(r))):Math.max(10,Math.min(200,this.minibatchSize*3));let o=e?.crossoverEvery;this.crossoverEvery=Math.max(0,Math.floor(o??Math.max(3,Math.floor(this.numTrials/4))));let s=e?.tieEpsilon;this.tieEpsilon=Number.isFinite(s)?s:0;let a=e?.feedbackMemorySize;this.feedbackMemorySize=Math.max(0,Math.floor(a??4));let l=e?.mergeMax;this.mergeMax=Math.max(0,Math.floor(l??5)),this.mergesUsed=0,this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold}reset(){super.reset(),this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold,this.localScoreHistory=[],this.localConfigurationHistory=[],this.feedbackMemory=[],this.mergesUsed=0,this.mergesDue=0,this.totalMergesTested=0,this.lastIterFoundNewProgram=!1,this.mergeAttemptKeys.clear(),this.mergeCompositionKeys.clear(),this.samplerState.epoch=-1,this.samplerState.shuffled=[],this.samplerState.freq.clear()}async compile(e,t,n,r){let o=Date.now();this.validateExamples(t),r?.auto&&this.configureAuto(r.auto);let s=r?.validationExamples,a=r?.feedbackExamples,l=(s&&s.length>0?s:t).slice(0,this.paretoSetSize),u=a&&a.length>0?a:t,c=async(M,L)=>{try{e.setInstruction?.(M);let Y=await e.forward(this.studentAI,L,{sampleCount:this.sampleCount});return this.stats.totalCalls+=1,await n({prediction:Y,example:L})||{}}catch{return{}}},p=async(M,L)=>{let Y=[];for(let _ of L)Y.push(await c(M,_));return Vo(Y)},d=await this.getBaseInstruction(e),m=[{instruction:d,parent:void 0,scores:await p(d,l)}],g=M=>{let L=r?.paretoMetricKey,Y=r?.paretoScalarize;if(typeof Y=="function")return Y(M);if(L)return Number.isFinite(M[L])?M[L]:0;let _=Object.values(M);return _.length?_.reduce((X,xe)=>X+xe,0)/_.length:0},h=[],f=async(M,L)=>{let Y=[];for(let _ of L){let X=await c(M,_);Y.push(g(X))}return Y};h.push(await f(d,l));let b=()=>{let M=h[0]?.length??0,L=[];for(let _=0;_<M;_++){let X=Number.NEGATIVE_INFINITY,xe=new Set;for(let ne=0;ne<h.length;ne++){let re=h[ne][_];re>X+this.tieEpsilon?(X=re,xe.clear(),xe.add(ne)):Math.abs(re-X)<=this.tieEpsilon&&xe.add(ne)}L.push(xe)}let Y=h.map(_=>jt(_));return vr(L,Y)},x=r?.maxMetricCalls;if(!Number.isFinite(x)||x<=0)throw new Error("AxGEPA: options.maxMetricCalls must be set to a positive integer");let A=Math.floor(x),w=this.getOptimizerLogger(r),I=r?.verbose??this.verbose?M=>console.log(`[GEPA] ${M}`):M=>{};w?.({name:"OptimizationStart",value:{optimizerType:"GEPA",exampleCount:t.length,validationCount:l.length,config:{numTrials:this.numTrials,minibatch:this.minibatch,mergeMax:this.mergeMax}}}),I(`Starting GEPA optimization: ${t.length} train, ${l.length} validation, maxCalls=${A}`);let T=0,v=Ct(m.map((M,L)=>({idx:L,scores:M.scores})),this.tieEpsilon).map(M=>M.idx),F;for(let M=0;M<this.numTrials&&!(A!==void 0&&this.stats.totalCalls>=Math.max(1,Math.floor(A)));M++){let L=h[0]?.length??0,Y=[];for(let P=0;P<L;P++){let B=Number.NEGATIVE_INFINITY,j=new Set;for(let H=0;H<h.length;H++){let q=h[H][P];q>B+this.tieEpsilon?(B=q,j.clear(),j.add(H)):Math.abs(q-B)<=this.tieEpsilon&&j.add(H)}Y.push(j)}let _=h.map(P=>jt(P));if(this.mergeMax>0&&this.mergesDue>0&&this.lastIterFoundNewProgram){let P=z=>{let W=[],ee=z;for(;ee!==void 0;)W.push(ee),ee=m[ee]?.parent;return W},B=z=>z.length?z[Math.floor(this.rand()*z.length)]:void 0,j=Sr(Y,_),H=new Set;for(let z of j)for(let W of z)H.add(W);let q=Array.from(H),K;for(let z=0;z<10&&!K&&!(q.length<2);z++){let W=B(q),ee=B(q);if(W===ee)continue;ee<W&&([W,ee]=[ee,W]);let ie=new Set(P(W)),de=new Set(P(ee));if(ie.has(ee)||de.has(W))continue;let fe=[...ie].filter(G=>de.has(G));if(fe.length===0)continue;let Ae=fe.map(G=>Math.max(1e-9,_[G])),ye=this.rand()*Ae.reduce((G,Z)=>G+Z,0),S=fe[fe.length-1];for(let G=0;G<fe.length;G++){if(ye<Ae[G]){S=fe[G];break}ye-=Ae[G]}K={i:W,j:ee,a:S}}if(this.lastIterFoundNewProgram=!1,K){let{i:z,j:W,a:ee}=K,ie=_[ee],de=_[z],fe=_[W],Ae=m[ee].instruction,ye=m[z].instruction,S=m[W].instruction,G=ye===Ae&&S!==ye||S===Ae&&ye!==S,Z=ie<=Math.min(de,fe)&&G,he="",ue="i",ae=!1;if(Z){let Re=`${z}|${W}|${ee}`;if(this.mergeAttemptKeys.has(Re))Z=!1;else{ye===Ae&&S!==ye?(he=S,ue="j"):S===Ae&&ye!==S?(he=ye,ue="i"):ye!==Ae&&S!==Ae&&ye!==S?de>fe||de===fe&&this.rand()<.5?(he=ye,ue="i"):(he=S,ue="j"):(he=ye,ue="i");let Be=`${Math.min(z,W)}|${Math.max(z,W)}|${ue}`;if(this.mergeCompositionKeys.has(Be))Z=!1;else{this.mergeAttemptKeys.add(Re),this.mergeCompositionKeys.add(Be);let Ie=h[z],Ne=h[W],Ve=Array.from({length:Ie.length},(Ce,Je)=>Je),Me=Ve.filter(Ce=>(Ie[Ce]??0)>(Ne[Ce]??0)),xt=Ve.filter(Ce=>(Ne[Ce]??0)>(Ie[Ce]??0)),wt=Ve.filter(Ce=>!(Me.includes(Ce)||xt.includes(Ce))),le=5,we=Math.ceil(le/3),je=(Ce,Je)=>{if(Je<=0||Ce.length===0)return[];if(Ce.length<=Je)return[...Ce];let Hn=[],Nr=new Set;for(;Hn.length<Je;){let pt=Math.floor(this.rand()*Ce.length);Nr.has(pt)||(Nr.add(pt),Hn.push(Ce[pt]))}return Hn},qe=[];qe.push(...je(Me,Math.min(we,Me.length))),qe.push(...je(xt,Math.min(we,xt.length)));let Xe=le-qe.length;qe.push(...je(wt,Math.max(0,Xe)));let Ht=le-qe.length;if(Ht>0){let Ce=Ve.filter(Je=>!qe.includes(Je));qe.push(...je(Ce,Math.min(Ht,Ce.length)))}let gs=qe.slice(0,Math.min(le,Ve.length)),Lu=gs.map(Ce=>l[Ce]);ae=!0;let $u=(await f(he,Lu)).reduce((Ce,Je)=>Ce+Je,0),Gu=gs.reduce((Ce,Je)=>Ce+(Ie[Je]??0),0),Du=gs.reduce((Ce,Je)=>Ce+(Ne[Je]??0),0);if($u>=Math.max(Gu,Du)+this.tieEpsilon){I(`Iteration ${M+1}: Merge accepted (programs ${z} + ${W} via ancestor ${ee})`);let Ce=await p(he,l);m.push({instruction:he,parent:ee,scores:Ce}),h.push(await f(he,l));let Je=v.length,Hn=ct(v.map(pt=>m[pt].scores))??0;v=Ct(m.map((pt,Uu)=>({idx:Uu,scores:pt.scores})),this.tieEpsilon).map(pt=>pt.idx);let Nr=ct(v.map(pt=>m[pt].scores))??0;(v.length>Je||Nr>Hn+1e-6)&&(T=0),this.mergesDue-=1,this.totalMergesTested+=1}}}}if(ae)continue}}let X=vr(Y,_,()=>this.rand()),xe=this.minibatch?this.nextMinibatchIndices(u.length,M).map(P=>u[P]):u;if(r?.skipPerfectScore??!0){let P=Number(r?.perfectScore??1),B=await f(m[X].instruction,xe);if(B.length>0&&B.every(j=>j>=P))continue}let ne=!1,re=m[X].instruction,J="reflective_mutation",oe,Pe;if(ne){let P=(X+1)%m.length;re=await this.mergeInstructions(m[X].instruction,m[P].instruction,r),J="merge",this.mergesUsed+=1}else{let P=r?.gepaAdapter;if(P){try{let B={instruction:m[X].instruction},j=await P.evaluate(xe,B,!0);oe=Array.isArray(j?.scores)?j.scores.reduce((z,W)=>z+(Number(W)||0),0):void 0;let H=P.make_reflective_dataset(B,j,["instruction"]),q=await P.propose_new_texts?.(B,H,["instruction"]),K=q?.instruction??(q?Object.values(q)[0]:void 0);typeof K=="string"&&K.length>0?re=K:re=await this.reflectInstruction(m[X].instruction,e,xe,async({prediction:z,example:W})=>{let ee=await n({prediction:z,example:W}),ie=Object.values(ee||{});return ie.length?ie.reduce((de,fe)=>de+fe,0)/ie.length:0},r)}catch{re=await this.reflectInstruction(m[X].instruction,e,xe,async({prediction:B,example:j})=>{let H=await n({prediction:B,example:j}),q=Object.values(H||{});return q.length?q.reduce((K,z)=>K+z,0)/q.length:0},r)}if(oe!==void 0)try{let B=await P.evaluate(xe,{instruction:re},!1);Pe=Array.isArray(B?.scores)?B.scores.reduce((j,H)=>j+(Number(H)||0),0):void 0}catch{}}else re=await this.reflectInstruction(m[X].instruction,e,xe,async({prediction:B,example:j})=>{let H=await n({prediction:B,example:j}),q=Object.values(H||{});return q.length?q.reduce((K,z)=>K+z,0)/q.length:0},r)}let se=await f(m[X].instruction,xe),Te=await f(re,xe),et=se.reduce((P,B)=>P+B,0),_e=Te.reduce((P,B)=>P+B,0);if(this.currentRound=M+1,await this.updateOptimizationProgress(this.currentRound,_e,{instructionLen:re.length,parent:X,totalRounds:this.numTrials},"GEPA",{strategy:J,paretoSetSize:l.length},_e,{instructionLen:m[X].instruction.length,idx:X},{...r??{},maxIterations:this.numTrials}),!(_e>et+this.tieEpsilon&&(oe===void 0||Pe===void 0||Pe>oe+this.tieEpsilon))){if(I(`Iteration ${M+1}: Rejected (child=${_e.toFixed(3)} <= parent=${et.toFixed(3)})`),++T>=this.earlyStoppingTrials){I(`Early stopping: ${T} iterations without improvement`);break}continue}I(`Iteration ${M+1}: Accepted (child=${_e.toFixed(3)} > parent=${et.toFixed(3)})`);let C=await p(re,l);m.push({instruction:re,parent:X,scores:C}),h.push(await f(re,l));let O=v.length,R=ct(v.map(P=>m[P].scores))??0;v=Ct(m.map((P,B)=>({idx:B,scores:P.scores})),this.tieEpsilon).map(P=>P.idx);let y=ct(v.map(P=>m[P].scores))??0;if(v.length>O||y>R+1e-6)T=0,I(`Iteration ${M+1}: Archive improved (size=${v.length}, hv=${y.toFixed(4)})`);else if(T++,I(`Iteration ${M+1}: Archive unchanged (stagnation=${T}/${this.earlyStoppingTrials})`),T>=this.earlyStoppingTrials){I(`Early stopping: ${T} iterations without archive improvement`);break}this.lastIterFoundNewProgram=!0,this.mergeMax>0&&this.totalMergesTested<this.mergeMax&&(this.mergesDue+=1)}let E=Ct(m.map((M,L)=>({idx:L,scores:M.scores})),this.tieEpsilon),D=E.length>0?Math.max(...E.map(M=>g(M.scores))):0,U;if(E.length>0){let M=Number.NEGATIVE_INFINITY;for(let L of E){let Y=g(L.scores);Y>M&&(M=Y,U=L.idx)}}let V=ct(E.map(M=>M.scores));this.stats.convergenceInfo.converged=!0;let k=this.getMergedCustomLabels(r);this.recordParetoMetrics(E.length,m.length,"GEPA",V,k);let $=Date.now()-o,N=typeof U=="number"?new It({bestScore:D,stats:this.stats,instruction:m[U].instruction,demos:[],examples:t,modelConfig:void 0,optimizerType:"GEPA",optimizationTime:$,totalRounds:this.numTrials,converged:this.stats.convergenceInfo.converged}):void 0,Q=this.generateOptimizationReport(E,V,D);return{demos:[],stats:this.stats,bestScore:D,paretoFront:E.map(M=>({demos:[],scores:M.scores,configuration:{candidate:M.idx,instruction:m[M.idx].instruction},dominatedSolutions:M.dominated})),paretoFrontSize:E.length,hypervolume:V,finalConfiguration:{strategy:"gepa",candidates:m.length},optimizedProgram:N,report:Q}}configureAuto(e){switch(e){case"light":this.numTrials=10,this.minibatch=!0,this.minibatchSize=15;break;case"medium":this.numTrials=20,this.minibatch=!0,this.minibatchSize=25;break;case"heavy":this.numTrials=35,this.minibatch=!0,this.minibatchSize=35;break}}async getBaseInstruction(e){let t=e.getInstruction?.();if(t&&t.length>0)return t;let r=e.getSignature?.()?.getDescription?.();return r&&r.length>0?r:"Follow the task precisely. Be concise, correct, and consistent."}async evaluateOnSet(e,t,n,r){let o=[];for(let s of n){let a=await this.evaluateOne(e,t,s,r);o.push(a)}return o}async evaluateAvg(e,t,n,r){let o=await this.evaluateOnSet(e,t,n,r);return o.length>0?jt(o):0}async evaluateOne(e,t,n,r){try{e.setInstruction?.(t);let o=await e.forward(this.studentAI,n,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let s=await r({prediction:o,example:n});if(typeof s=="number"&&!Number.isNaN(s)){let a=typeof this.targetScore=="number"?this.targetScore:.5;return s>=a&&(this.stats.successfulDemos+=1),s}return 0}catch(o){return this.getLogger()?.({name:"Notification",id:"gepa_eval",value:String(o)}),0}}async reflectInstruction(e,t,n,r,o,s){let a=s??[];if(a.length===0)for(let m of n)try{t.setInstruction?.(e);let g=await t.forward(this.studentAI,m,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let h=await r({prediction:g,example:m});a.push({input:m,prediction:g,score:typeof h=="number"?h:0})}catch{a.push({input:m,prediction:{},score:0})}let l=o?.overrideTeacherAI??this.teacherAI??this.studentAI,u=o?.feedbackFn,c=()=>{let m=[];for(let g=0;g<a.length;g++){let h=a[g],f=`# Example ${g+1}
509
+ ${e}`}i(n)}},Tr=wi();var Ri={enabled:!0,enabledCategories:["optimization","convergence","resource_usage","teacher_student","checkpointing","pareto"],maxLabelLength:100,samplingRate:1},Ko,cp=i=>{if(Ko)return Ko;if(i)return Ko=pp(i),Ko};var Wo=Ri,ru=i=>{Wo={...Wo,...i}},ou=()=>({...Wo}),pp=i=>({optimizationLatencyHistogram:i.createHistogram("ax_optimizer_optimization_duration_ms",{description:"End-to-end duration of optimization runs",unit:"ms"}),optimizationRequestsCounter:i.createCounter("ax_optimizer_optimization_requests_total",{description:"Total number of optimization requests"}),optimizationErrorsCounter:i.createCounter("ax_optimizer_optimization_errors_total",{description:"Total number of failed optimizations"}),convergenceRoundsHistogram:i.createHistogram("ax_optimizer_convergence_rounds",{description:"Number of rounds until convergence"}),convergenceScoreGauge:i.createGauge("ax_optimizer_convergence_score",{description:"Current best score during optimization"}),convergenceImprovementGauge:i.createGauge("ax_optimizer_convergence_improvement",{description:"Improvement in score from baseline"}),stagnationRoundsGauge:i.createGauge("ax_optimizer_stagnation_rounds",{description:"Number of rounds without improvement"}),earlyStoppingCounter:i.createCounter("ax_optimizer_early_stopping_total",{description:"Total number of early stopping events"}),tokenUsageCounter:i.createCounter("ax_optimizer_token_usage_total",{description:"Total tokens used during optimization"}),costUsageCounter:i.createCounter("ax_optimizer_cost_usage_total",{description:"Total cost incurred during optimization",unit:"$"}),memoryUsageGauge:i.createGauge("ax_optimizer_memory_usage_bytes",{description:"Peak memory usage during optimization",unit:"By"}),optimizationDurationHistogram:i.createHistogram("ax_optimizer_duration_ms",{description:"Duration of optimization runs",unit:"ms"}),teacherStudentUsageCounter:i.createCounter("ax_optimizer_teacher_student_usage_total",{description:"Total number of teacher-student interactions"}),teacherStudentLatencyHistogram:i.createHistogram("ax_optimizer_teacher_student_latency_ms",{description:"Latency of teacher-student interactions",unit:"ms"}),teacherStudentScoreImprovementGauge:i.createGauge("ax_optimizer_teacher_student_score_improvement",{description:"Score improvement from teacher-student interactions"}),checkpointSaveCounter:i.createCounter("ax_optimizer_checkpoint_save_total",{description:"Total number of checkpoint saves"}),checkpointLoadCounter:i.createCounter("ax_optimizer_checkpoint_load_total",{description:"Total number of checkpoint loads"}),checkpointSaveLatencyHistogram:i.createHistogram("ax_optimizer_checkpoint_save_latency_ms",{description:"Latency of checkpoint save operations",unit:"ms"}),checkpointLoadLatencyHistogram:i.createHistogram("ax_optimizer_checkpoint_load_latency_ms",{description:"Latency of checkpoint load operations",unit:"ms"}),paretoOptimizationsCounter:i.createCounter("ax_optimizer_pareto_optimizations_total",{description:"Total number of Pareto optimizations"}),paretoFrontSizeHistogram:i.createHistogram("ax_optimizer_pareto_front_size",{description:"Size of Pareto frontier"}),paretoHypervolumeGauge:i.createGauge("ax_optimizer_pareto_hypervolume",{description:"Hypervolume of Pareto frontier"}),paretoSolutionsGeneratedHistogram:i.createHistogram("ax_optimizer_pareto_solutions_generated",{description:"Number of solutions generated for Pareto optimization"}),programInputFieldsGauge:i.createGauge("ax_optimizer_program_input_fields",{description:"Number of input fields in optimized program"}),programOutputFieldsGauge:i.createGauge("ax_optimizer_program_output_fields",{description:"Number of output fields in optimized program"}),examplesCountGauge:i.createGauge("ax_optimizer_examples_count",{description:"Number of training examples used"}),validationSetSizeGauge:i.createGauge("ax_optimizer_validation_set_size",{description:"Size of validation set used"}),evaluationLatencyHistogram:i.createHistogram("ax_optimizer_evaluation_latency_ms",{description:"Latency of program evaluations",unit:"ms"}),demoGenerationLatencyHistogram:i.createHistogram("ax_optimizer_demo_generation_latency_ms",{description:"Latency of demo generation",unit:"ms"}),metricComputationLatencyHistogram:i.createHistogram("ax_optimizer_metric_computation_latency_ms",{description:"Latency of metric computation",unit:"ms"}),optimizerTypeGauge:i.createGauge("ax_optimizer_type",{description:"Type of optimizer being used"}),targetScoreGauge:i.createGauge("ax_optimizer_target_score",{description:"Target score for optimization"}),maxRoundsGauge:i.createGauge("ax_optimizer_max_rounds",{description:"Maximum rounds for optimization"})}),ft=i=>{let e={};for(let[t,n]of Object.entries(i))if(n!=null){let r=String(n),o=Wo.maxLabelLength;e[t]=r.length>o?r.substring(0,o):r}return e},dp=(i,e,t,n,r,o)=>{try{let s=ft({success:t.toString(),optimizer_type:n,...r?{program_signature:r}:{},...o});i.optimizationLatencyHistogram&&i.optimizationLatencyHistogram.record(e,s),i.optimizationRequestsCounter&&i.optimizationRequestsCounter.add(1,s),!t&&i.optimizationErrorsCounter&&i.optimizationErrorsCounter.add(1,s)}catch(s){console.warn("Failed to record optimization metric:",s)}},mp=(i,e,t,n,r,o,s)=>{try{let a=ft({optimizer_type:o,...s});i.convergenceRoundsHistogram&&i.convergenceRoundsHistogram.record(e,a),i.convergenceScoreGauge&&i.convergenceScoreGauge.record(t,a),i.convergenceImprovementGauge&&i.convergenceImprovementGauge.record(n,a),i.stagnationRoundsGauge&&i.stagnationRoundsGauge.record(r,a)}catch(a){console.warn("Failed to record convergence metric:",a)}},gp=(i,e,t,n)=>{try{let r=ft({reason:e,optimizer_type:t,...n});i.earlyStoppingCounter&&i.earlyStoppingCounter.add(1,r)}catch(r){console.warn("Failed to record early stopping metric:",r)}},hp=(i,e,t,n,r,o)=>{try{let s=ft({optimizer_type:n,...o});i.tokenUsageCounter&&i.tokenUsageCounter.add(e,s),i.costUsageCounter&&i.costUsageCounter.add(t,s),r!==void 0&&i.memoryUsageGauge&&i.memoryUsageGauge.record(r,s)}catch(s){console.warn("Failed to record resource usage metric:",s)}},fp=(i,e,t,n)=>{try{let r=ft({optimizer_type:t,...n});i.optimizationDurationHistogram&&i.optimizationDurationHistogram.record(e,r)}catch(r){console.warn("Failed to record optimization duration metric:",r)}},xp=(i,e,t,n,r)=>{try{let o=ft({optimizer_type:n,...r});i.teacherStudentUsageCounter&&i.teacherStudentUsageCounter.add(1,o),i.teacherStudentLatencyHistogram&&i.teacherStudentLatencyHistogram.record(e,o),i.teacherStudentScoreImprovementGauge&&i.teacherStudentScoreImprovementGauge.record(t,o)}catch(o){console.warn("Failed to record teacher-student metric:",o)}},Ap=(i,e,t,n,r,o)=>{try{let s=ft({operation:e,success:n.toString(),optimizer_type:r,...o});e==="save"?(i.checkpointSaveCounter&&i.checkpointSaveCounter.add(1,s),i.checkpointSaveLatencyHistogram&&i.checkpointSaveLatencyHistogram.record(t,s)):(i.checkpointLoadCounter&&i.checkpointLoadCounter.add(1,s),i.checkpointLoadLatencyHistogram&&i.checkpointLoadLatencyHistogram.record(t,s))}catch(s){console.warn("Failed to record checkpoint metric:",s)}},yp=(i,e,t,n,r,o)=>{try{let s=ft({optimizer_type:n,...o});i.paretoOptimizationsCounter&&i.paretoOptimizationsCounter.add(1,s),i.paretoFrontSizeHistogram&&i.paretoFrontSizeHistogram.record(e,s),r!==void 0&&i.paretoHypervolumeGauge&&i.paretoHypervolumeGauge.record(r,s),i.paretoSolutionsGeneratedHistogram&&i.paretoSolutionsGeneratedHistogram.record(t,s)}catch(s){console.warn("Failed to record Pareto metric:",s)}},bp=(i,e,t,n,r,o,s)=>{try{let a=ft({optimizer_type:o,...s});i.programInputFieldsGauge&&i.programInputFieldsGauge.record(e,a),i.programOutputFieldsGauge&&i.programOutputFieldsGauge.record(t,a),i.examplesCountGauge&&i.examplesCountGauge.record(n,a),i.validationSetSizeGauge&&i.validationSetSizeGauge.record(r,a)}catch(a){console.warn("Failed to record program complexity metric:",a)}},Ip=(i,e,t,n,r)=>{try{let o=ft({metric_type:e,optimizer_type:n,...r});switch(e){case"evaluation":i.evaluationLatencyHistogram&&i.evaluationLatencyHistogram.record(t,o);break;case"demo_generation":i.demoGenerationLatencyHistogram&&i.demoGenerationLatencyHistogram.record(t,o);break;case"metric_computation":i.metricComputationLatencyHistogram&&i.metricComputationLatencyHistogram.record(t,o);break}}catch(o){console.warn("Failed to record optimizer performance metric:",o)}},Cp=(i,e,t,n,r)=>{try{let o=ft({optimizer_type:e,...r});i.optimizerTypeGauge&&i.optimizerTypeGauge.record(1,o),t!==void 0&&i.targetScoreGauge&&i.targetScoreGauge.record(t,o),n!==void 0&&i.maxRoundsGauge&&i.maxRoundsGauge.record(n,o)}catch(o){console.warn("Failed to record optimizer configuration metric:",o)}},Ct=class{bestScore;stats;instruction;demos;examples;modelConfig;optimizerType;optimizationTime;totalRounds;converged;scoreHistory;configurationHistory;constructor(e){this.bestScore=e.bestScore,this.stats=e.stats,this.instruction=e.instruction,this.demos=e.demos,this.examples=e.examples,this.modelConfig=e.modelConfig,this.optimizerType=e.optimizerType,this.optimizationTime=e.optimizationTime,this.totalRounds=e.totalRounds,this.converged=e.converged,this.scoreHistory=e.scoreHistory,this.configurationHistory=e.configurationHistory}applyTo(e){this.demos&&this.demos.length>0?e.setDemos(this.demos,{modelConfig:this.modelConfig}):this.modelConfig&&e.setDemos([],{modelConfig:this.modelConfig})}},wr=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}},Ye=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 wr({maxTokens:1e6});this.costTracker=e.costTracker??t,this.metricsInstruments=cp(de.meter),this.stats=this.initializeStats(),this.debugOptimizer=e.debugOptimizer??!1,this.optimizerLogger=e.optimizerLogger??(this.verbose?Tr:void 0),this.initializeResultExplainer()}getMergedCustomLabels(e){return Ft(de.customLabels,this.studentAI?.getOptions?.()?.customLabels,this.teacherAI?.getOptions?.()?.customLabels,e?.customLabels)}initializeResultExplainer(){this.resultExplainer=void 0}initializeStats(){return{totalCalls:0,successfulDemos:0,estimatedTokenUsage:0,earlyStopped:!1,resourceUsage:{totalTokens:0,totalTime:0,avgLatencyPerEval:0,costByModel:{}},convergenceInfo:{converged:!1,finalImprovement:0,stagnationRounds:0,convergenceThreshold:.01},bestScore:0,bestConfiguration:{}}}setupRandomSeed(){this.seed!==void 0&&(Math.random=(()=>{let e=this.seed;return()=>(e=(e*9301+49297)%233280,e/233280)})())}checkCostLimits(){return this.costTracker?.isLimitReached()??!1}checkTargetScore(e){return this.targetScore!==void 0&&e>=this.targetScore}updateResourceUsage(e,t=0){this.stats.resourceUsage.totalTime=Date.now()-e,this.stats.resourceUsage.totalTokens+=t,this.stats.totalCalls>0&&(this.stats.resourceUsage.avgLatencyPerEval=this.stats.resourceUsage.totalTime/this.stats.totalCalls)}triggerEarlyStopping(e,t,n){this.stats.earlyStopped=!0,this.stats.earlyStopping={bestScoreRound:t,patienceExhausted:e.includes("improvement"),reason:e},this.recordEarlyStoppingMetrics(e,"unknown",n),this.onEarlyStop&&this.onEarlyStop(e,this.stats),this.getOptimizerLogger()?.({name:"EarlyStopping",value:{reason:e,finalScore:this.stats.bestScore??0,round:t}})}validateExamples(e,t=!0){if(!e||e.length===0)throw new Error("At least 1 example is required for optimization");if(t&&e.length<2)throw new Error("At least 2 examples are required for optimization with auto-splitting. Provide more examples to enable proper train/validation split.");let n=t?10:5;e.length<n&&this.verbose&&console.warn(`[Ax Optimizer] Warning: Only ${e.length} examples provided. Consider providing more examples (${n}+ recommended) for better optimization results.`)}getAIService(e=!1,t){return e&&t?.overrideTeacherAI?t.overrideTeacherAI:e&&this.teacherAI?this.teacherAI:this.studentAI}hasTeacherAI(e){return e?.overrideTeacherAI!==void 0||this.teacherAI!==void 0}getTeacherOrStudentAI(e){return e?.overrideTeacherAI||this.teacherAI||this.studentAI}async executeWithTeacher(e,t=!0,n){let r=this.getAIService(t,n);return await e(r)}async*compileStream(e,t,n,r){let o=Date.now(),s=this.constructor.name,a=e.getSignature().toString();this.recordOptimizationStart(s,a,r);let l,u=(g,h,f,I,x,A,w,b={},T)=>{this.getOptimizerLogger(T)?.({name:"RoundProgress",value:{round:g,totalRounds:T?.maxIterations??0,currentScore:h,bestScore:A,configuration:f}}),this.updateOptimizationProgress(g,h,f,I,x,A,w,b,T)},c=(g,h)=>{l=g,this.triggerEarlyStopping(g,this.currentRound,r)},p=g=>{this.onProgress?.(g),u(g.round,g.currentScore,g.currentConfiguration||{},s,{},g.bestScore,g.bestConfiguration,g.convergenceInfo,r)},d=await this.compile(e,t,n,{...r,overrideOnProgress:p,overrideOnEarlyStop:c}),m=Date.now()-o;return this.recordOptimizationComplete(m,!0,s,a,r),l&&this.getLogger(r)?.({name:"Notification",id:"optimization_early_stop",value:`Optimization stopped early due to ${l}`}),{demos:d.demos,stats:d.stats,bestScore:d.bestScore,finalConfiguration:d.finalConfiguration,scoreHistory:d.scoreHistory,configurationHistory:d.configurationHistory}}async compilePareto(e,t,n,r){let o=this.constructor.name,s=Date.now(),a=await this.generateWeightedSolutions(e,t,n,r),l=await this.generateConstraintSolutions(e,t,n,r),u=[...a,...l],c=this.findParetoFrontier(u),p=this.calculateHypervolume(c);this.updateResourceUsage(s),this.stats.convergenceInfo.converged=!0,this.recordParetoMetrics(c.length,u.length,"base_optimizer",p,r);let d=c.length>0?Math.max(...c.map(m=>Math.max(...Object.values(m.scores)))):0;return{demos:c.length>0?[...c[0].demos]:void 0,stats:this.stats,bestScore:d,paretoFront:c,hypervolume:p,paretoFrontSize:c.length,finalConfiguration:{paretoFrontSize:c.length,hypervolume:p,strategy:"weighted_combinations_and_constraints",numSolutions:u.length}}}async generateWeightedSolutions(e,t,n,r){let o=[];if(!t||t.length===0)throw new Error("No examples provided for Pareto optimization");let s=t[0],a=await e.forward(this.getAIService(!1,r),s),l=await n({prediction:a,example:s}),u=Object.keys(l),c=this.generateWeightCombinations(u);for(let p=0;p<c.length;p++){let d=c[p],m=async({prediction:g,example:h})=>{let f=await n({prediction:g,example:h}),I=0;for(let[x,A]of Object.entries(f))I+=A*(d[x]||0);return I};try{let g=await this.compile(e,t,m,{...r,verbose:!1}),h=await this.evaluateWithMultiObjective(e,g,n,t);o.push({scores:h,demos:g.demos,configuration:{...g.finalConfiguration,weights:d,strategy:"weighted_combination"}})}catch{}}return o}async generateConstraintSolutions(e,t,n,r){let o=[];if(!t||t.length===0)throw new Error("No examples provided for multi-objective optimization");let s=t[0],a=await e.forward(this.getAIService(!1,r),s),l=await n({prediction:a,example:s}),u=Object.keys(l);for(let c of u){let p=async({prediction:d,example:m})=>{let g=await n({prediction:d,example:m}),h=g[c]||0,f=0;for(let[I,x]of Object.entries(g))I!==c&&x<.3&&(f+=(.3-x)*2);return h-f};try{let d=await this.compile(e,t,p,{...r,verbose:!1}),m=await this.evaluateWithMultiObjective(e,d,n,t);o.push({scores:m,demos:d.demos,configuration:{...d.finalConfiguration,primaryObjective:c,strategy:"constraint_based"}})}catch{}}return o}generateWeightCombinations(e){let t=[];for(let r of e){let o={};for(let s of e)o[s]=s===r?1:0;t.push(o)}let n={};for(let r of e)n[r]=1/e.length;if(t.push(n),e.length===2){let[r,o]=e;for(let s=.1;s<=.9;s+=.2){let a=1-s;t.push({[r]:s,[o]:a})}}if(e.length===3){let[r,o,s]=e;t.push({[r]:.5,[o]:.3,[s]:.2},{[r]:.3,[o]:.5,[s]:.2},{[r]:.2,[o]:.3,[s]:.5})}return t}async evaluateWithMultiObjective(e,t,n,r){let o=new Se(e.getSignature());t.demos&&o.setDemos(t.demos);let s=[],a=Math.max(1,Math.min(5,Math.floor(r.length*.2))),l=r.slice(-a),u={},c=l;for(let d of c)try{let m=await o.forward(this.studentAI,d),g=await n({prediction:m,example:d});for(let[h,f]of Object.entries(g))u[h]||(u[h]=[]),u[h].push(f)}catch{}let p={};for(let[d,m]of Object.entries(u))p[d]=m.length>0?m.reduce((g,h)=>g+h,0)/m.length:0;return p}findParetoFrontier(e){let t=[];for(let n=0;n<e.length;n++){let r=e[n],o=!1,s=0;for(let a=0;a<e.length;a++){if(n===a)continue;let l=e[a];if(this.dominates(l.scores,r.scores)){o=!0;break}this.dominates(r.scores,l.scores)&&s++}o||t.push({demos:r.demos||[],scores:r.scores,configuration:r.configuration,dominatedSolutions:s})}return t}dominates(e,t){let n=Object.keys(e),r=!0,o=!1;for(let s of n){let a=e[s]||0,l=t[s]||0;if(a<l){r=!1;break}a>l&&(o=!0)}return r&&o}calculateHypervolume(e){if(e.length===0)return;let t=e[0],n=Object.keys(t.scores);if(n.length===2){let[r,o]=n,s=0,a=[...e].sort((u,c)=>(c.scores[r]||0)-(u.scores[r]||0)),l=0;for(let u of a){let c=u.scores[r]||0,p=u.scores[o]||0;s+=c*(p-l),l=Math.max(l,p)}return s}}async saveCheckpoint(e,t,n,r,o={},s){let a=s?.overrideCheckpointSave||this.checkpointSave;if(!a)return;let l=Date.now(),u=!1,c;try{let p={version:"1.0.0",timestamp:Date.now(),optimizerType:e,optimizerConfig:t,currentRound:this.currentRound,totalRounds:this.stats.resourceUsage.totalTime>0?this.currentRound:0,bestScore:n,bestConfiguration:r,scoreHistory:[...this.scoreHistory],configurationHistory:[...this.configurationHistory],stats:{...this.stats},optimizerState:o,examples:[]};c=await a(p),u=!0}catch(p){throw u=!1,p}finally{let p=Date.now()-l;this.recordCheckpointMetrics("save",p,u,e,s)}return c}async loadCheckpoint(e,t){let n=t?.overrideCheckpointLoad||this.checkpointLoad;if(!n)return null;let r=Date.now(),o=!1,s=null;try{s=await n(e),o=s!==null}catch(a){throw o=!1,a}finally{let a=Date.now()-r;this.recordCheckpointMetrics("load",a,o,"unknown",t)}return s}restoreFromCheckpoint(e){this.currentRound=e.currentRound,this.scoreHistory=[...e.scoreHistory],this.configurationHistory=[...e.configurationHistory],this.stats={...e.stats}}shouldSaveCheckpoint(e,t){let n=t?.overrideCheckpointInterval||this.checkpointInterval;return n!==void 0&&e%n===0}async updateOptimizationProgress(e,t,n,r,o,s,a,l={},u){this.currentRound=e,this.scoreHistory.push(t),this.configurationHistory.push(n),this.shouldSaveCheckpoint(e,u)&&await this.saveCheckpoint(r,o,s,a,l,u),this.getOptimizerLogger(u)?.({name:"RoundProgress",value:{round:e,totalRounds:u?.maxIterations??0,currentScore:t,bestScore:s,configuration:n}})}async saveFinalCheckpoint(e,t,n,r,o={},s){s?.saveCheckpointOnComplete!==!1&&await this.saveCheckpoint(e,t,n,r,{...o,final:!0},s)}getLogger(e){if(this.isLoggingEnabled(e))return this.logger?this.logger:this.studentAI.getLogger()}isLoggingEnabled(e){return e?.verbose!==void 0?e.verbose:this.verbose??!0}recordOptimizationStart(e,t,n){if(!this.metricsInstruments)return;let r=this.getMergedCustomLabels(n);if(t){let o=(t.match(/input:/g)||[]).length,s=(t.match(/output:/g)||[]).length;bp(this.metricsInstruments,o,s,0,0,e,r)}Cp(this.metricsInstruments,e,this.targetScore,void 0,r)}recordOptimizationComplete(e,t,n,r,o){if(!this.metricsInstruments)return;let s=this.getMergedCustomLabels(o);dp(this.metricsInstruments,e,t,n,r,s),fp(this.metricsInstruments,e,n,s);let a=this.costTracker?.getCurrentCost()??0,l=this.costTracker?.getTotalTokens()??0;hp(this.metricsInstruments,l,a,n,void 0,s)}recordConvergenceMetrics(e,t,n,r,o,s){if(!this.metricsInstruments)return;let a=this.getMergedCustomLabels(s);mp(this.metricsInstruments,e,t,n,r,o,a)}recordEarlyStoppingMetrics(e,t,n){if(!this.metricsInstruments)return;let r=this.getMergedCustomLabels(n);gp(this.metricsInstruments,e,t,r)}recordTeacherStudentMetrics(e,t,n,r){if(!this.metricsInstruments)return;let o=this.getMergedCustomLabels(r);xp(this.metricsInstruments,e,t,n,o)}recordCheckpointMetrics(e,t,n,r,o){if(!this.metricsInstruments)return;let s=this.getMergedCustomLabels(o);Ap(this.metricsInstruments,e,t,n,r,s)}recordParetoMetrics(e,t,n,r,o){if(!this.metricsInstruments)return;let s=this.getMergedCustomLabels(o);yp(this.metricsInstruments,e,t,n,r,s)}recordPerformanceMetrics(e,t,n,r){if(!this.metricsInstruments)return;let o=this.getMergedCustomLabels(r);Ip(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??de.optimizerLogger??Tr}getStats(){return{...this.stats}}async explainOptimizationResults(e,t,n){let r=this.stats.convergenceInfo.converged,o=this.stats.totalCalls,s=o>0?this.stats.successfulDemos/o*100:0,a=`Optimization finished with best score ${e.toFixed(3)}${t?` using configuration ${JSON.stringify(t)}`:""}. Convergence: ${r?"yes":"no"}. Success rate: ${s.toFixed(1)}%.`,l=[];if(r||l.push("Increase numTrials or relax earlyStoppingTrials to allow further improvement."),typeof this.targetScore=="number"&&e<this.targetScore&&l.push("Tighten the metric or supply more/better-labeled examples to reach targetScore."),t&&"bootstrappedDemos"in t){let c=t.bootstrappedDemos;typeof c=="number"&&c===0&&l.push("Consider allowing a small number of bootstrapped demos to boost performance.")}l.length===0&&l.push("Re-run with more trials or different acquisition settings to explore more of the space.");let u=`Tokens used: ${this.stats.resourceUsage.totalTokens}, rounds: ${this.currentRound}, stagnationRounds: ${this.stats.convergenceInfo.stagnationRounds}.`;return{humanExplanation:a,recommendations:l,performanceAssessment:u}}async logOptimizationComplete(e,t,n,r,o){let s=this.getOptimizerLogger(r);s&&s(o?{name:"OptimizationComplete",value:{optimizerType:e,bestScore:t,bestConfiguration:n||{},totalCalls:this.stats.totalCalls,successRate:this.stats.totalCalls>0?`${(this.stats.successfulDemos/this.stats.totalCalls*100).toFixed(1)}%`:"N/A",explanation:o.humanExplanation,recommendations:o.recommendations,performanceAssessment:o.performanceAssessment,stats:this.stats}}:{name:"OptimizationComplete",value:{optimizerType:e,bestScore:t,bestConfiguration:n||{},totalCalls:this.stats.totalCalls,successRate:this.stats.totalCalls>0?`${(this.stats.successfulDemos/this.stats.totalCalls*100).toFixed(1)}%`:"N/A",stats:this.stats}})}reset(){this.stats=this.initializeStats(),this.costTracker?.reset(),this.currentRound=0,this.scoreHistory=[],this.configurationHistory=[]}};function su(i){return Oe.create(i)}function ze(i,e){let t=typeof i=="string"?Oe.create(i):i;return new Se(t,e)}function iu(i,e,t=0){let n=new Set([...Object.keys(i),...Object.keys(e)]),r=!0,o=!1;for(let s of n){let a=i[s]??0,l=e[s]??0;if(a+t<l){r=!1;break}a>l+t&&(o=!0)}return r&&o}function Tt(i,e=0){let t=[];for(let n=0;n<i.length;n++){let r=0,o=!1;for(let s=0;s<i.length;s++)if(n!==s){if(iu(i[s].scores,i[n].scores,e)){o=!0;break}iu(i[n].scores,i[s].scores,e)&&r++}o||t.push({idx:i[n].idx,scores:i[n].scores,dominated:r})}return t}function ut(i){if(i.length===0)return;let e=Object.keys(i[0]??{});if(e.length!==2)return;let[t,n]=e,r=[...i].sort((a,l)=>(l[t]??0)-(a[t]??0)),o=0,s=0;for(let a of r){let l=a[t]??0,u=a[n]??0,c=Math.max(u-s,0);o+=l*c,s=Math.max(s,u)}return o}function jt(i){if(i.length===0)return 0;let e=0;for(let t of i)e+=t;return e/i.length}function Vo(i){let e={},t={};for(let r of i)for(let[o,s]of Object.entries(r))e[o]=(e[o]||0)+(typeof s=="number"?s:0),t[o]=(t[o]||0)+1;let n={};for(let[r,o]of Object.entries(e))n[r]=o/Math.max(t[r]||1,1);return n}function Rr(i,e){let t=new Set;for(let c of i)for(let p of c)t.add(p);let r=[...Array.from(t)].sort((c,p)=>(e[c]??0)-(e[p]??0)),o=new Set,s=(c,p)=>{for(let d of i){if(!d.has(c))continue;let m=!1;for(let g of p)if(d.has(g)){m=!0;break}if(!m)return!1}return!0},a=!0;for(;a;){a=!1;for(let c of r){if(o.has(c))continue;let p=new Set(r.filter(d=>d!==c&&!o.has(d)));if(s(c,p)){o.add(c),a=!0;break}}}let l=r.filter(c=>!o.has(c)),u=new Set(l);return i.map(c=>{let p=new Set;for(let d of c)u.has(d)&&p.add(d);return p})}function Sr(i,e,t){let n=Rr(i,e),r={};for(let l of n)for(let u of l)r[u]=(r[u]||0)+1;let o=[];for(let[l,u]of Object.entries(r)){let c=Number(l);for(let p=0;p<u;p++)o.push(c)}if(o.length===0)return 0;let s=typeof t=="function"?t():Math.random(),a=Math.floor(s*o.length);return o[a]}var Fn=class i extends Ye{numTrials;minibatch;minibatchSize;earlyStoppingTrials;minImprovementThreshold;sampleCount;paretoSetSize;crossoverEvery;tieEpsilon;feedbackMemorySize;feedbackMemory=[];mergeMax;mergesUsed=0;mergesDue=0;totalMergesTested=0;lastIterFoundNewProgram=!1;mergeAttemptKeys=new Set;mergeCompositionKeys=new Set;static REFLECTION_PROMPT_TEMPLATE="I provided an assistant with the following instructions to perform a task for me:\n```\n<curr_instructions>\n```\n\nThe following are examples of different task inputs provided to the assistant along with the assistant's response for each of them, and some feedback on how the assistant's response could be better:\n```\n<inputs_outputs_feedback>\n```\n\nYour task is to write a new instruction for the assistant. Read the inputs carefully and identify the input format and infer detailed task description about the task I wish to solve with the assistant. Read all the assistant responses and the corresponding feedback. Identify all niche and domain specific factual information about the task and include it in the instruction, as a lot of it may not be available to the assistant in the future. The assistant may have utilized a generalizable strategy to solve the task, if so, include that in the instruction as well. Provide the new instructions within ``` blocks.";rngState=123456789;samplerState={epoch:-1,shuffled:[],freq:new Map};localScoreHistory=[];localConfigurationHistory=[];constructor(e){super(e);let t=e?.seed,n=Number.isFinite(t)?Math.floor(Number(t)):0;this.rngState=n&&n!==0?n:123456789,this.numTrials=e.numTrials??30,this.minibatch=e.minibatch??!0,this.minibatchSize=e.minibatchSize??20,this.earlyStoppingTrials=e.earlyStoppingTrials??5,this.minImprovementThreshold=e.minImprovementThreshold??0,this.sampleCount=e.sampleCount??1;let r=e?.paretoSetSize;this.paretoSetSize=r&&r>0?Math.min(1e3,Math.max(5,Math.floor(r))):Math.max(10,Math.min(200,this.minibatchSize*3));let o=e?.crossoverEvery;this.crossoverEvery=Math.max(0,Math.floor(o??Math.max(3,Math.floor(this.numTrials/4))));let s=e?.tieEpsilon;this.tieEpsilon=Number.isFinite(s)?s:0;let a=e?.feedbackMemorySize;this.feedbackMemorySize=Math.max(0,Math.floor(a??4));let l=e?.mergeMax;this.mergeMax=Math.max(0,Math.floor(l??5)),this.mergesUsed=0,this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold}reset(){super.reset(),this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold,this.localScoreHistory=[],this.localConfigurationHistory=[],this.feedbackMemory=[],this.mergesUsed=0,this.mergesDue=0,this.totalMergesTested=0,this.lastIterFoundNewProgram=!1,this.mergeAttemptKeys.clear(),this.mergeCompositionKeys.clear(),this.samplerState.epoch=-1,this.samplerState.shuffled=[],this.samplerState.freq.clear()}async compile(e,t,n,r){let o=Date.now();this.validateExamples(t),r?.auto&&this.configureAuto(r.auto);let s=r?.validationExamples,a=r?.feedbackExamples,l=(s&&s.length>0?s:t).slice(0,this.paretoSetSize),u=a&&a.length>0?a:t,c=async(M,L)=>{try{e.setInstruction?.(M);let X=await e.forward(this.studentAI,L,{sampleCount:this.sampleCount});return this.stats.totalCalls+=1,await n({prediction:X,example:L})||{}}catch{return{}}},p=async(M,L)=>{let X=[];for(let _ of L)X.push(await c(M,_));return Vo(X)},d=await this.getBaseInstruction(e),m=[{instruction:d,parent:void 0,scores:await p(d,l)}],g=M=>{let L=r?.paretoMetricKey,X=r?.paretoScalarize;if(typeof X=="function")return X(M);if(L)return Number.isFinite(M[L])?M[L]:0;let _=Object.values(M);return _.length?_.reduce((Z,Ae)=>Z+Ae,0)/_.length:0},h=[],f=async(M,L)=>{let X=[];for(let _ of L){let Z=await c(M,_);X.push(g(Z))}return X};h.push(await f(d,l));let I=()=>{let M=h[0]?.length??0,L=[];for(let _=0;_<M;_++){let Z=Number.NEGATIVE_INFINITY,Ae=new Set;for(let re=0;re<h.length;re++){let oe=h[re][_];oe>Z+this.tieEpsilon?(Z=oe,Ae.clear(),Ae.add(re)):Math.abs(oe-Z)<=this.tieEpsilon&&Ae.add(re)}L.push(Ae)}let X=h.map(_=>jt(_));return Sr(L,X)},x=r?.maxMetricCalls;if(!Number.isFinite(x)||x<=0)throw new Error("AxGEPA: options.maxMetricCalls must be set to a positive integer");let A=Math.floor(x),w=this.getOptimizerLogger(r),b=r?.verbose??this.verbose?M=>console.log(`[GEPA] ${M}`):M=>{};w?.({name:"OptimizationStart",value:{optimizerType:"GEPA",exampleCount:t.length,validationCount:l.length,config:{numTrials:this.numTrials,minibatch:this.minibatch,mergeMax:this.mergeMax}}}),b(`Starting GEPA optimization: ${t.length} train, ${l.length} validation, maxCalls=${A}`);let T=0,v=Tt(m.map((M,L)=>({idx:L,scores:M.scores})),this.tieEpsilon).map(M=>M.idx),F;for(let M=0;M<this.numTrials&&!(A!==void 0&&this.stats.totalCalls>=Math.max(1,Math.floor(A)));M++){let L=h[0]?.length??0,X=[];for(let E=0;E<L;E++){let U=Number.NEGATIVE_INFINITY,j=new Set;for(let K=0;K<h.length;K++){let q=h[K][E];q>U+this.tieEpsilon?(U=q,j.clear(),j.add(K)):Math.abs(q-U)<=this.tieEpsilon&&j.add(K)}X.push(j)}let _=h.map(E=>jt(E));if(this.mergeMax>0&&this.mergesDue>0&&this.lastIterFoundNewProgram){let E=z=>{let W=[],ee=z;for(;ee!==void 0;)W.push(ee),ee=m[ee]?.parent;return W},U=z=>z.length?z[Math.floor(this.rand()*z.length)]:void 0,j=Rr(X,_),K=new Set;for(let z of j)for(let W of z)K.add(W);let q=Array.from(K),H;for(let z=0;z<10&&!H&&!(q.length<2);z++){let W=U(q),ee=U(q);if(W===ee)continue;ee<W&&([W,ee]=[ee,W]);let ce=new Set(E(W)),ae=new Set(E(ee));if(ce.has(ee)||ae.has(W))continue;let xe=[...ce].filter(V=>ae.has(V));if(xe.length===0)continue;let ye=xe.map(V=>Math.max(1e-9,_[V])),P=this.rand()*ye.reduce((V,ne)=>V+ne,0),$=xe[xe.length-1];for(let V=0;V<xe.length;V++){if(P<ye[V]){$=xe[V];break}P-=ye[V]}H={i:W,j:ee,a:$}}if(this.lastIterFoundNewProgram=!1,H){let{i:z,j:W,a:ee}=H,ce=_[ee],ae=_[z],xe=_[W],ye=m[ee].instruction,P=m[z].instruction,$=m[W].instruction,V=P===ye&&$!==P||$===ye&&P!==$,ne=ce<=Math.min(ae,xe)&&V,he="",pe="i",ie=!1;if(ne){let Ge=`${z}|${W}|${ee}`;if(this.mergeAttemptKeys.has(Ge))ne=!1;else{P===ye&&$!==P?(he=$,pe="j"):$===ye&&P!==$?(he=P,pe="i"):P!==ye&&$!==ye&&P!==$?ae>xe||ae===xe&&this.rand()<.5?(he=P,pe="i"):(he=$,pe="j"):(he=P,pe="i");let we=`${Math.min(z,W)}|${Math.max(z,W)}|${pe}`;if(this.mergeCompositionKeys.has(we))ne=!1;else{this.mergeAttemptKeys.add(Ge),this.mergeCompositionKeys.add(we);let ve=h[z],He=h[W],Pe=Array.from({length:ve.length},(Ie,We)=>We),ct=Pe.filter(Ie=>(ve[Ie]??0)>(He[Ie]??0)),At=Pe.filter(Ie=>(He[Ie]??0)>(ve[Ie]??0)),wt=Pe.filter(Ie=>!(ct.includes(Ie)||At.includes(Ie))),ue=5,Te=Math.ceil(ue/3),Ze=(Ie,We)=>{if(We<=0||Ie.length===0)return[];if(Ie.length<=We)return[...Ie];let zn=[],Nr=new Set;for(;zn.length<We;){let pt=Math.floor(this.rand()*Ie.length);Nr.has(pt)||(Nr.add(pt),zn.push(Ie[pt]))}return zn},Qe=[];Qe.push(...Ze(ct,Math.min(Te,ct.length))),Qe.push(...Ze(At,Math.min(Te,At.length)));let Ke=ue-Qe.length;Qe.push(...Ze(wt,Math.max(0,Ke)));let _r=ue-Qe.length;if(_r>0){let Ie=Pe.filter(We=>!Qe.includes(We));Qe.push(...Ze(Ie,Math.min(_r,Ie.length)))}let gs=Qe.slice(0,Math.min(ue,Pe.length)),Gu=gs.map(Ie=>l[Ie]);ie=!0;let Du=(await f(he,Gu)).reduce((Ie,We)=>Ie+We,0),Uu=gs.reduce((Ie,We)=>Ie+(ve[We]??0),0),Bu=gs.reduce((Ie,We)=>Ie+(He[We]??0),0);if(Du>=Math.max(Uu,Bu)+this.tieEpsilon){b(`Iteration ${M+1}: Merge accepted (programs ${z} + ${W} via ancestor ${ee})`);let Ie=await p(he,l);m.push({instruction:he,parent:ee,scores:Ie}),h.push(await f(he,l));let We=v.length,zn=ut(v.map(pt=>m[pt].scores))??0;v=Tt(m.map((pt,ju)=>({idx:ju,scores:pt.scores})),this.tieEpsilon).map(pt=>pt.idx);let Nr=ut(v.map(pt=>m[pt].scores))??0;(v.length>We||Nr>zn+1e-6)&&(T=0),this.mergesDue-=1,this.totalMergesTested+=1}}}}if(ie)continue}}let Z=Sr(X,_,()=>this.rand()),Ae=this.minibatch?this.nextMinibatchIndices(u.length,M).map(E=>u[E]):u;if(r?.skipPerfectScore??!0){let E=Number(r?.perfectScore??1),U=await f(m[Z].instruction,Ae);if(U.length>0&&U.every(j=>j>=E))continue}let re=!1,oe=m[Z].instruction,J="reflective_mutation",se,Ee;if(re){let E=(Z+1)%m.length;oe=await this.mergeInstructions(m[Z].instruction,m[E].instruction,r),J="merge",this.mergesUsed+=1}else{let E=r?.gepaAdapter;if(E){try{let U={instruction:m[Z].instruction},j=await E.evaluate(Ae,U,!0);se=Array.isArray(j?.scores)?j.scores.reduce((z,W)=>z+(Number(W)||0),0):void 0;let K=E.make_reflective_dataset(U,j,["instruction"]),q=await E.propose_new_texts?.(U,K,["instruction"]),H=q?.instruction??(q?Object.values(q)[0]:void 0);typeof H=="string"&&H.length>0?oe=H:oe=await this.reflectInstruction(m[Z].instruction,e,Ae,async({prediction:z,example:W})=>{let ee=await n({prediction:z,example:W}),ce=Object.values(ee||{});return ce.length?ce.reduce((ae,xe)=>ae+xe,0)/ce.length:0},r)}catch{oe=await this.reflectInstruction(m[Z].instruction,e,Ae,async({prediction:U,example:j})=>{let K=await n({prediction:U,example:j}),q=Object.values(K||{});return q.length?q.reduce((H,z)=>H+z,0)/q.length:0},r)}if(se!==void 0)try{let U=await E.evaluate(Ae,{instruction:oe},!1);Ee=Array.isArray(U?.scores)?U.scores.reduce((j,K)=>j+(Number(K)||0),0):void 0}catch{}}else oe=await this.reflectInstruction(m[Z].instruction,e,Ae,async({prediction:U,example:j})=>{let K=await n({prediction:U,example:j}),q=Object.values(K||{});return q.length?q.reduce((H,z)=>H+z,0)/q.length:0},r)}let le=await f(m[Z].instruction,Ae),Ce=await f(oe,Ae),Xe=le.reduce((E,U)=>E+U,0),_e=Ce.reduce((E,U)=>E+U,0);if(this.currentRound=M+1,await this.updateOptimizationProgress(this.currentRound,_e,{instructionLen:oe.length,parent:Z,totalRounds:this.numTrials},"GEPA",{strategy:J,paretoSetSize:l.length},_e,{instructionLen:m[Z].instruction.length,idx:Z},{...r??{},maxIterations:this.numTrials}),!(_e>Xe+this.tieEpsilon&&(se===void 0||Ee===void 0||Ee>se+this.tieEpsilon))){if(b(`Iteration ${M+1}: Rejected (child=${_e.toFixed(3)} <= parent=${Xe.toFixed(3)})`),++T>=this.earlyStoppingTrials){b(`Early stopping: ${T} iterations without improvement`);break}continue}b(`Iteration ${M+1}: Accepted (child=${_e.toFixed(3)} > parent=${Xe.toFixed(3)})`);let C=await p(oe,l);m.push({instruction:oe,parent:Z,scores:C}),h.push(await f(oe,l));let k=v.length,R=ut(v.map(E=>m[E].scores))??0;v=Tt(m.map((E,U)=>({idx:U,scores:E.scores})),this.tieEpsilon).map(E=>E.idx);let y=ut(v.map(E=>m[E].scores))??0;if(v.length>k||y>R+1e-6)T=0,b(`Iteration ${M+1}: Archive improved (size=${v.length}, hv=${y.toFixed(4)})`);else if(T++,b(`Iteration ${M+1}: Archive unchanged (stagnation=${T}/${this.earlyStoppingTrials})`),T>=this.earlyStoppingTrials){b(`Early stopping: ${T} iterations without archive improvement`);break}this.lastIterFoundNewProgram=!0,this.mergeMax>0&&this.totalMergesTested<this.mergeMax&&(this.mergesDue+=1)}let O=Tt(m.map((M,L)=>({idx:L,scores:M.scores})),this.tieEpsilon),D=O.length>0?Math.max(...O.map(M=>g(M.scores))):0,B;if(O.length>0){let M=Number.NEGATIVE_INFINITY;for(let L of O){let X=g(L.scores);X>M&&(M=X,B=L.idx)}}let Q=ut(O.map(M=>M.scores));this.stats.convergenceInfo.converged=!0;let S=this.getMergedCustomLabels(r);this.recordParetoMetrics(O.length,m.length,"GEPA",Q,S);let G=Date.now()-o,N=typeof B=="number"?new Ct({bestScore:D,stats:this.stats,instruction:m[B].instruction,demos:[],examples:t,modelConfig:void 0,optimizerType:"GEPA",optimizationTime:G,totalRounds:this.numTrials,converged:this.stats.convergenceInfo.converged}):void 0,Y=this.generateOptimizationReport(O,Q,D);return{demos:[],stats:this.stats,bestScore:D,paretoFront:O.map(M=>({demos:[],scores:M.scores,configuration:{candidate:M.idx,instruction:m[M.idx].instruction},dominatedSolutions:M.dominated})),paretoFrontSize:O.length,hypervolume:Q,finalConfiguration:{strategy:"gepa",candidates:m.length},optimizedProgram:N,report:Y}}configureAuto(e){switch(e){case"light":this.numTrials=10,this.minibatch=!0,this.minibatchSize=15;break;case"medium":this.numTrials=20,this.minibatch=!0,this.minibatchSize=25;break;case"heavy":this.numTrials=35,this.minibatch=!0,this.minibatchSize=35;break}}async getBaseInstruction(e){let t=e.getInstruction?.();if(t&&t.length>0)return t;let r=e.getSignature?.()?.getDescription?.();return r&&r.length>0?r:"Follow the task precisely. Be concise, correct, and consistent."}async evaluateOnSet(e,t,n,r){let o=[];for(let s of n){let a=await this.evaluateOne(e,t,s,r);o.push(a)}return o}async evaluateAvg(e,t,n,r){let o=await this.evaluateOnSet(e,t,n,r);return o.length>0?jt(o):0}async evaluateOne(e,t,n,r){try{e.setInstruction?.(t);let o=await e.forward(this.studentAI,n,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let s=await r({prediction:o,example:n});if(typeof s=="number"&&!Number.isNaN(s)){let a=typeof this.targetScore=="number"?this.targetScore:.5;return s>=a&&(this.stats.successfulDemos+=1),s}return 0}catch(o){return this.getLogger()?.({name:"Notification",id:"gepa_eval",value:String(o)}),0}}async reflectInstruction(e,t,n,r,o,s){let a=s??[];if(a.length===0)for(let m of n)try{t.setInstruction?.(e);let g=await t.forward(this.studentAI,m,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let h=await r({prediction:g,example:m});a.push({input:m,prediction:g,score:typeof h=="number"?h:0})}catch{a.push({input:m,prediction:{},score:0})}let l=o?.overrideTeacherAI??this.teacherAI??this.studentAI,u=o?.feedbackFn,c=()=>{let m=[];for(let g=0;g<a.length;g++){let h=a[g],f=`# Example ${g+1}
510
510
  `;if(f+=`## Inputs
511
511
  `,typeof h.input=="object"&&h.input!==null)for(let[x,A]of Object.entries(h.input))f+=`### ${x}
512
512
  ${String(A).trim()}
@@ -520,16 +520,16 @@ ${String(A).trim()}
520
520
  `;else f+=`${String(h.prediction).trim()}
521
521
 
522
522
  `;f+=`## Feedback
523
- `;let b=`This trajectory got a score of ${h.score.toFixed(3)}.`;if(typeof u=="function")try{let x=u({prediction:h.prediction,example:h.input});x&&(b=Array.isArray(x)?x.join(`
524
- `):x)}catch{}f+=`${b}
523
+ `;let I=`This trajectory got a score of ${h.score.toFixed(3)}.`;if(typeof u=="function")try{let x=u({prediction:h.prediction,example:h.input});x&&(I=Array.isArray(x)?x.join(`
524
+ `):x)}catch{}f+=`${I}
525
525
  `,m.push(f)}return m.join(`
526
526
 
527
- `)},p=i.REFLECTION_PROMPT_TEMPLATE.replace("<curr_instructions>",e).replace("<inputs_outputs_feedback>",c());try{let m=await l.chat({chatPrompt:[{role:"user",content:p}],model:o?.reflectionModel},{stream:!1});if(typeof m.getReader=="function")throw new Error("Streaming response not expected for reflection");let h=m.results?.[0]?.content;if(typeof h=="string"){let f=this.extractInstructionFromBackticks(h);if(f&&f.length>16){let b=`Iteration feedback: ${a.map(x=>`score=${x.score.toFixed(2)}`).join(", ")}`;return this.feedbackMemory.unshift(b),this.feedbackMemory.length>this.feedbackMemorySize&&this.feedbackMemory.pop(),f}}}catch{}let d=We('currentInstruction:string "Current instruction", feedbackSummary?:string "Summarized feedback", recentFeedback?:string[] "Past feedback memory", minibatch:json "Array of {input,prediction,score}" -> newInstruction:string "Improved instruction within 1-6 sentences."');try{let g=(await d.forward(l,{currentInstruction:e,feedbackSummary:this.feedbackMemory[0]||"",recentFeedback:this.feedbackMemory,minibatch:a}))?.newInstruction?.trim();if(g&&g.length>16)return g}catch{}return`${e.trim()} Focus on step-by-step evidence-based reasoning. Avoid hallucinations.`.slice(0,2e3)}extractInstructionFromBackticks(e){let t=e.indexOf("```")+3,n=e.lastIndexOf("```");if(t>=n){let s=e.trim();if(s.startsWith("```")){let a=s.match(/^```\S*\n?/);if(a)return s.slice(a[0].length).trim()}else if(s.endsWith("```"))return s.slice(0,-3).trim();return s}let r=e.slice(t,n),o=r.match(/^\S*\n/);return o&&(r=r.slice(o[0].length)),r.trim()}updateSamplerShuffled(e){let t=Array.from({length:e},(l,u)=>u);for(let l=t.length-1;l>0;l--){let u=Math.floor(this.rand()*(l+1));[t[l],t[u]]=[t[u],t[l]]}for(let l of t)this.samplerState.freq.set(l,(this.samplerState.freq.get(l)??0)+1);let n=this.minibatchSize,r=e%n,o=r===0?0:n-r,s=Array.from({length:e},(l,u)=>u).sort((l,u)=>(this.samplerState.freq.get(l)??0)-(this.samplerState.freq.get(u)??0)),a=[...t];for(let l=0;l<o;l++){let u=s[l%s.length];a.push(u),this.samplerState.freq.set(u,(this.samplerState.freq.get(u)??0)+1)}this.samplerState.shuffled=a,this.samplerState.epoch+=1}nextMinibatchIndices(e,t){this.samplerState.epoch===-1&&(this.samplerState.epoch=0,this.updateSamplerShuffled(e));let n=this.minibatchSize,r=Math.max(1,Math.floor(this.samplerState.shuffled.length/n)),o=Math.floor(t/r);for(;o>=this.samplerState.epoch;)this.updateSamplerShuffled(e);let s=t*n%this.samplerState.shuffled.length;return this.samplerState.shuffled.slice(s,s+n)}rand(){return this.rngState^=this.rngState<<13,this.rngState^=this.rngState>>>17,this.rngState^=this.rngState<<5,(this.rngState>>>0)/4294967296}generateOptimizationReport(e,t,n){let r=e.length>0?e.reduce((u,c)=>{let p=Object.values(u.scores).reduce((m,g)=>m+g,0);return Object.values(c.scores).reduce((m,g)=>m+g,0)>p?c:u}):void 0,o={};if(r)for(let[u,c]of Object.entries(r.scores))o[u]={value:c,percentage:c*100};let s=[];if(e.length>1){let u=[...e].sort((c,p)=>p.dominated-c.dominated).slice(0,3);for(let c of u)s.push({...c.scores})}let a="good",l=[];if(e.length===1)a="single",l.push("Increase numTrials (current seems low)"),l.push("Add more training examples"),l.push("Adjust earlyStoppingTrials");else if(e.length<3)a="limited",l.push("More optimization trials"),l.push("Larger validation set");else{a="good";let u=Object.keys(e[0]?.scores||{});for(let c of u)l.push(`High ${c}: Choose solution with best ${c} score`);l.push("Balanced: Use provided bestScore (average)")}return this.stats.totalCalls<50&&(l.push("Quick run detected - use numTrials: 30+ for production"),l.push("Provide 50+ training examples"),l.push("Use 20+ validation examples")),{summary:"GEPA Multi-Objective Optimization Complete",bestSolution:{overallScore:n??0,objectives:o},paretoFrontier:{solutionCount:e.length,objectiveSpaceCoverage:(t??0)*100,hypervolume:t??0,tradeoffs:s.length>0?s:void 0},statistics:{totalEvaluations:this.stats.totalCalls,candidatesExplored:e.length,converged:this.stats.convergenceInfo?.converged??!1},recommendations:{status:a,suggestions:l}}}async mergeInstructions(e,t,n){let r=n?.overrideTeacherAI??this.teacherAI??this.studentAI,o=We(`instructionA:string "Parent A instruction",
527
+ `)},p=i.REFLECTION_PROMPT_TEMPLATE.replace("<curr_instructions>",e).replace("<inputs_outputs_feedback>",c());try{let m=await l.chat({chatPrompt:[{role:"user",content:p}],model:o?.reflectionModel},{stream:!1});if(typeof m.getReader=="function")throw new Error("Streaming response not expected for reflection");let h=m.results?.[0]?.content;if(typeof h=="string"){let f=this.extractInstructionFromBackticks(h);if(f&&f.length>16){let I=`Iteration feedback: ${a.map(x=>`score=${x.score.toFixed(2)}`).join(", ")}`;return this.feedbackMemory.unshift(I),this.feedbackMemory.length>this.feedbackMemorySize&&this.feedbackMemory.pop(),f}}}catch{}let d=ze('currentInstruction:string "Current instruction", feedbackSummary?:string "Summarized feedback", recentFeedback?:string[] "Past feedback memory", minibatch:json "Array of {input,prediction,score}" -> newInstruction:string "Improved instruction within 1-6 sentences."');try{let g=(await d.forward(l,{currentInstruction:e,feedbackSummary:this.feedbackMemory[0]||"",recentFeedback:this.feedbackMemory,minibatch:a}))?.newInstruction?.trim();if(g&&g.length>16)return g}catch{}return`${e.trim()} Focus on step-by-step evidence-based reasoning. Avoid hallucinations.`.slice(0,2e3)}extractInstructionFromBackticks(e){let t=e.indexOf("```")+3,n=e.lastIndexOf("```");if(t>=n){let s=e.trim();if(s.startsWith("```")){let a=s.match(/^```\S*\n?/);if(a)return s.slice(a[0].length).trim()}else if(s.endsWith("```"))return s.slice(0,-3).trim();return s}let r=e.slice(t,n),o=r.match(/^\S*\n/);return o&&(r=r.slice(o[0].length)),r.trim()}updateSamplerShuffled(e){let t=Array.from({length:e},(l,u)=>u);for(let l=t.length-1;l>0;l--){let u=Math.floor(this.rand()*(l+1));[t[l],t[u]]=[t[u],t[l]]}for(let l of t)this.samplerState.freq.set(l,(this.samplerState.freq.get(l)??0)+1);let n=this.minibatchSize,r=e%n,o=r===0?0:n-r,s=Array.from({length:e},(l,u)=>u).sort((l,u)=>(this.samplerState.freq.get(l)??0)-(this.samplerState.freq.get(u)??0)),a=[...t];for(let l=0;l<o;l++){let u=s[l%s.length];a.push(u),this.samplerState.freq.set(u,(this.samplerState.freq.get(u)??0)+1)}this.samplerState.shuffled=a,this.samplerState.epoch+=1}nextMinibatchIndices(e,t){this.samplerState.epoch===-1&&(this.samplerState.epoch=0,this.updateSamplerShuffled(e));let n=this.minibatchSize,r=Math.max(1,Math.floor(this.samplerState.shuffled.length/n)),o=Math.floor(t/r);for(;o>=this.samplerState.epoch;)this.updateSamplerShuffled(e);let s=t*n%this.samplerState.shuffled.length;return this.samplerState.shuffled.slice(s,s+n)}rand(){return this.rngState^=this.rngState<<13,this.rngState^=this.rngState>>>17,this.rngState^=this.rngState<<5,(this.rngState>>>0)/4294967296}generateOptimizationReport(e,t,n){let r=e.length>0?e.reduce((u,c)=>{let p=Object.values(u.scores).reduce((m,g)=>m+g,0);return Object.values(c.scores).reduce((m,g)=>m+g,0)>p?c:u}):void 0,o={};if(r)for(let[u,c]of Object.entries(r.scores))o[u]={value:c,percentage:c*100};let s=[];if(e.length>1){let u=[...e].sort((c,p)=>p.dominated-c.dominated).slice(0,3);for(let c of u)s.push({...c.scores})}let a="good",l=[];if(e.length===1)a="single",l.push("Increase numTrials (current seems low)"),l.push("Add more training examples"),l.push("Adjust earlyStoppingTrials");else if(e.length<3)a="limited",l.push("More optimization trials"),l.push("Larger validation set");else{a="good";let u=Object.keys(e[0]?.scores||{});for(let c of u)l.push(`High ${c}: Choose solution with best ${c} score`);l.push("Balanced: Use provided bestScore (average)")}return this.stats.totalCalls<50&&(l.push("Quick run detected - use numTrials: 30+ for production"),l.push("Provide 50+ training examples"),l.push("Use 20+ validation examples")),{summary:"GEPA Multi-Objective Optimization Complete",bestSolution:{overallScore:n??0,objectives:o},paretoFrontier:{solutionCount:e.length,objectiveSpaceCoverage:(t??0)*100,hypervolume:t??0,tradeoffs:s.length>0?s:void 0},statistics:{totalEvaluations:this.stats.totalCalls,candidatesExplored:e.length,converged:this.stats.convergenceInfo?.converged??!1},recommendations:{status:a,suggestions:l}}}async mergeInstructions(e,t,n){let r=n?.overrideTeacherAI??this.teacherAI??this.studentAI,o=ze(`instructionA:string "Parent A instruction",
528
528
  instructionB:string "Parent B instruction",
529
529
  recentFeedback?:string[] "Past feedback memory"
530
530
  -> 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 au(i){return i.map(e=>{let t=e.type?.name??"string",n=e.type?.isArray?"[]":"",r=e.isOptional?" (optional)":"",o=e.description?`: ${e.description}`:"";return`- ${e.name}: ${t}${n}${r}${o}`}).join(`
531
- `)}function Ip(i){return i.map(e=>{let t=e.type?.name??"string",n=e.type?.isArray?"[]":"",r=e.type?.options?` (options: ${e.type.options.join(", ")})`:"",o=e.description?`: ${e.description}`:"";return`- ${e.name}: ${t}${n}${r}${o}`}).join(`
532
- `)}var Nn=class{signature;options;constructor(e,t){this.signature=e,this.options={teacher:t.teacher,diversity:t.diversity??"none",temperature:t.temperature??.8,domain:t.domain,edgeCases:t.edgeCases,model:t.model}}async generate(e,t){let n=Date.now(),r=t?.batchSize??Math.min(e,10),o=[],s=0,a=0;for(let l=0;l<e;l+=r){let u=Math.min(r,e-l),c=await this.generateInputs(u);for(let p of c){a++;try{let d=await this.labelInput(p);o.push({input:p,expected:d,category:"normal"}),s++}catch(d){console.warn("AxSynth: Failed to label input:",d)}}}if(this.options.edgeCases&&this.options.edgeCases.length>0){let l=await this.generateEdgeCaseInputs(Math.ceil(e*.2));for(let u of l){a++;try{let c=await this.labelInput(u);o.push({input:u,expected:c,category:"edge_case"}),s++}catch(c){console.warn("AxSynth: Failed to label edge case input:",c)}}}return{examples:o,stats:{requested:e,generated:o.length,labelingSuccessRate:a>0?s/a:0,durationMs:Date.now()-n}}}async generateInputs(e){let t=this.signature.getInputFields(),n=this.signature.getOutputFields(),r=this.signature.getDescription(),o=`
531
+ `)}function Tp(i){return i.map(e=>{let t=e.type?.name??"string",n=e.type?.isArray?"[]":"",r=e.type?.options?` (options: ${e.type.options.join(", ")})`:"",o=e.description?`: ${e.description}`:"";return`- ${e.name}: ${t}${n}${r}${o}`}).join(`
532
+ `)}var _n=class{signature;options;constructor(e,t){this.signature=e,this.options={teacher:t.teacher,diversity:t.diversity??"none",temperature:t.temperature??.8,domain:t.domain,edgeCases:t.edgeCases,model:t.model}}async generate(e,t){let n=Date.now(),r=t?.batchSize??Math.min(e,10),o=[],s=0,a=0;for(let l=0;l<e;l+=r){let u=Math.min(r,e-l),c=await this.generateInputs(u);for(let p of c){a++;try{let d=await this.labelInput(p);o.push({input:p,expected:d,category:"normal"}),s++}catch(d){console.warn("AxSynth: Failed to label input:",d)}}}if(this.options.edgeCases&&this.options.edgeCases.length>0){let l=await this.generateEdgeCaseInputs(Math.ceil(e*.2));for(let u of l){a++;try{let c=await this.labelInput(u);o.push({input:u,expected:c,category:"edge_case"}),s++}catch(c){console.warn("AxSynth: Failed to label edge case input:",c)}}}return{examples:o,stats:{requested:e,generated:o.length,labelingSuccessRate:a>0?s/a:0,durationMs:Date.now()-n}}}async generateInputs(e){let t=this.signature.getInputFields(),n=this.signature.getOutputFields(),r=this.signature.getDescription(),o=`
533
533
  You are generating realistic input data for an AI system.
534
534
 
535
535
  ${r?`Task description: ${r}`:""}
@@ -539,14 +539,14 @@ The system expects these INPUT fields:
539
539
  ${au(t)}
540
540
 
541
541
  The system produces these OUTPUT fields:
542
- ${Ip(n)}
542
+ ${Tp(n)}
543
543
 
544
544
  Generate ${e} diverse, realistic input examples as a JSON array.
545
545
  Each example should be an object with the input fields defined above.
546
546
  Make the examples varied and realistic for the domain.
547
547
 
548
548
  Output ONLY the JSON array, no explanation.
549
- `.trim(),s=new ve("count:number -> examples:json");s.setInstruction(o);try{let a=await s.forward(this.options.teacher,{count:e},{model:this.options.model});return Array.isArray(a.examples)?a.examples.slice(0,e):[]}catch(a){return console.warn("AxSynth: Input generation failed:",a),[]}}async generateEdgeCaseInputs(e){let t=this.signature.getInputFields(),n=this.options.edgeCases??[],r=`
549
+ `.trim(),s=new Se("count:number -> examples:json");s.setInstruction(o);try{let a=await s.forward(this.options.teacher,{count:e},{model:this.options.model});return Array.isArray(a.examples)?a.examples.slice(0,e):[]}catch(a){return console.warn("AxSynth: Input generation failed:",a),[]}}async generateEdgeCaseInputs(e){let t=this.signature.getInputFields(),n=this.options.edgeCases??[],r=`
550
550
  You are generating challenging edge case input data to test an AI system's robustness.
551
551
 
552
552
  The system expects these INPUT fields:
@@ -558,7 +558,7 @@ ${n.map(s=>`- ${s}`).join(`
558
558
  `)}
559
559
 
560
560
  Output ONLY the JSON array, no explanation.
561
- `.trim(),o=new ve("count:number -> examples:json");o.setInstruction(r);try{let s=await o.forward(this.options.teacher,{count:e},{model:this.options.model});return Array.isArray(s.examples)?s.examples.slice(0,e):[]}catch(s){return console.warn("AxSynth: Edge case generation failed:",s),[]}}async labelInput(e){return await new ve(this.signature).forward(this.options.teacher,e,{model:this.options.model})}getSignature(){return this.signature}getTeacher(){return this.options.teacher}};var Jo=class i{gen;options;tracer;currentScore;constructor(e,t){this.gen=e,this.options={...t,enableTracing:t.enableTracing??!0},this.tracer=new Fn(e,{name:this.options.name,storage:this.options.storage,metadata:this.options.metadata,onTrace:this.options.onTrace}),this.loadLatestCheckpoint()}async forward(e,t,n){return this.options.enableTracing?this.tracer.forward(e,t,n):this.gen.forward(e,t,n)}async*streamingForward(e,t,n){this.options.enableTracing?yield*this.tracer.streamingForward(e,t,n):yield*this.gen.streamingForward(e,t,n)}getUsage(){return this.gen.getUsage()}resetUsage(){this.gen.resetUsage()}getSignature(){return this.gen.getSignature()}setInstruction(e){this.gen.setInstruction(e)}getInstruction(){return this.gen.getInstruction()}updateMeter(e){this.gen.updateMeter(e)}addAssert(e,t){this.gen.addAssert(e,t)}addStreamingAssert(e,t,n){this.gen.addStreamingAssert(e,t,n)}addFieldProcessor(e,t){this.gen.addFieldProcessor(e,t)}addStreamingFieldProcessor(e,t){this.gen.addStreamingFieldProcessor(e,t)}clone(){return new i(this.gen.clone(),this.options)}async optimize(e={}){let t=Date.now(),n={...this.options,...e},r=n.teacher,o=n.budget??20,s=[...n.examples??[]];if(n.useTraces!==!1){let T=(await this.getTraces()).filter(v=>v.input&&v.output).map(v=>({...v.input,...v.output}));s=[...s,...T]}if(n.generateExamples||s.length===0){let I=n.synthCount??20,T={teacher:r,...n.synthOptions},E=(await new Nn(this.gen.getSignature(),T).generate(I)).examples.map(D=>({...D.input,...D.expected}));s=[...s,...E]}if(s.length===0)throw new Error("No training examples available. Provide examples, enable trace usage, or enable example generation.");let a=n.validationSplit??.2,l=[...s].sort(()=>Math.random()-.5),u=Math.floor(l.length*(1-a)),c=l.slice(0,u),p=l.slice(u),d=n.metric;if(!d){let I={ai:r,criteria:n.criteria,...n.judgeOptions};d=new Pn(this.gen.getSignature(),I).toMetricFn()}let m=new _n({studentAI:r,numTrials:o,minibatch:!0,minibatchSize:10}),g=o*Math.max(c.length,10),h=await m.compile(this.gen,c,d,{validationExamples:p,maxMetricCalls:g});if(!h.optimizedProgram)throw new Error("GEPA optimization failed: no optimizedProgram returned");h.optimizedProgram.instruction&&this.gen.setInstruction(h.optimizedProgram.instruction),this.currentScore=h.optimizedProgram.bestScore,this.tracer=this.tracer.clone(this.gen);let f=this.currentScore,b=f,A=((await this.options.storage.load(this.options.name,{type:"checkpoint",limit:1}))[0]?.version??0)+1,w={type:"checkpoint",name:this.options.name,version:A,createdAt:new Date,instruction:this.gen.getInstruction(),score:f,optimizerType:"gepa",metadata:{budget:o,trainingExamples:c.length,durationMs:Date.now()-t}};return await this.options.storage.save(this.options.name,w),{score:f,improvement:b,checkpointVersion:A,stats:{trainingExamples:c.length,validationExamples:p.length,durationMs:Date.now()-t}}}async loadLatestCheckpoint(){try{let t=(await this.options.storage.load(this.options.name,{type:"checkpoint",limit:1}))[0];t?.instruction&&(this.gen.setInstruction(t.instruction),this.currentScore=t.score,this.tracer=this.tracer.clone(this.gen))}catch{}}getGen(){return this.gen}getStorage(){return this.options.storage}async getTraces(e){return this.options.storage.load(this.options.name,{type:"trace",...e})}async addFeedback(e,t){let n=await this.options.storage.load(this.options.name,{id:e,type:"trace",limit:1});if(n.length>0){let r=n[0];r.feedback={...r.feedback,...t},await this.options.storage.save(this.options.name,r)}}};var Qo=class{rows=[];baseUrl;dataset;split;config;options;constructor({dataset:e,split:t,config:n,options:r}){this.baseUrl="https://datasets-server.huggingface.co/rows",this.dataset=e,this.split=t,this.config=n,this.options=r}async fetchDataFromAPI(e){try{let t=await fetch(e);if(!t.ok)throw new Error(`Error fetching data: ${t.statusText}`);let n=await t.json();if(!n?.rows)throw new Error("Invalid data format");return n.rows}catch(t){throw console.error("Error fetching data from API:",t),t}}async loadData(){let e=this.options?.offset??0,t=this.options?.length??100,n=encodeURIComponent(this.dataset),r=`${this.baseUrl}?dataset=${n}&config=${this.config}&split=${this.split}&offset=${e}&length=${t}`;return this.rows=await this.fetchDataFromAPI(r),this.rows}setData(e){this.rows=e}getData(){return this.rows}async getRows({count:e,fields:t,renameMap:n}){if(this.rows.length===0)throw new Error("No data loaded, call loadData or setData first.");return this.rows.slice(0,e).map(o=>{let s={};return t.forEach(a=>{let l=a.split("."),u=o.row;for(let p of l)Object.hasOwn(u,p)&&(u=u[p]);if(!u)return;let c=n&&a in n?n[a]:a;if(!c)throw new Error(`Invalid field name: ${a}`);s[c]=u}),s}).filter(o=>Object.keys(o).length!==0)}};var lu=Kn(require("crypto"),1);function Si(i){let e=new Date().toISOString();return{version:1,sections:{},stats:{bulletCount:0,helpfulCount:0,harmfulCount:0,tokenEstimate:0},updatedAt:e,description:i}}function qt(i){return JSON.parse(JSON.stringify(i))}function Cp(i){return Math.ceil(i.length/4)}function vi(i,e,t){let n=[],r=[],{maxSectionSize:o=Number.POSITIVE_INFINITY,allowDynamicSections:s=!0,enableAutoPrune:a=!1,protectedBulletIds:l}=t??{},u=new Date().toISOString(),c=l??new Set;for(let p of e){if(!p.section)continue;if(!i.sections[p.section]){if(!s)continue;i.sections[p.section]=[]}let d=i.sections[p.section];switch(p.type){case"ADD":{if(d.length>=o){if(!a)continue;let h=wp(d,c);if(!h)continue;n.push(h.id),r.push({type:"REMOVE",section:p.section,bulletId:h.id,metadata:{...h.metadata??{},autoPruned:!0,removedAt:u}})}let m=p.bulletId??Tp(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 Mi(i),i.updatedAt=u,{updatedBulletIds:n,autoRemoved:r}}function ki(i,e,t){for(let n of Object.values(i.sections)){let r=n.find(o=>o.id===e);if(r){t==="helpful"?r.helpfulCount+=1:t==="harmful"&&(r.harmfulCount+=1),r.updatedAt=new Date().toISOString(),Mi(i);return}}}function kr(i){let e=i.description?`## Context Playbook
561
+ `.trim(),o=new Se("count:number -> examples:json");o.setInstruction(r);try{let s=await o.forward(this.options.teacher,{count:e},{model:this.options.model});return Array.isArray(s.examples)?s.examples.slice(0,e):[]}catch(s){return console.warn("AxSynth: Edge case generation failed:",s),[]}}async labelInput(e){return await new Se(this.signature).forward(this.options.teacher,e,{model:this.options.model})}getSignature(){return this.signature}getTeacher(){return this.options.teacher}};var Jo=class i{gen;options;tracer;currentScore;constructor(e,t){this.gen=e,this.options={...t,enableTracing:t.enableTracing??!0},this.tracer=new Pn(e,{name:this.options.name,storage:this.options.storage,metadata:this.options.metadata,onTrace:this.options.onTrace}),this.loadLatestCheckpoint()}async forward(e,t,n){return this.options.enableTracing?this.tracer.forward(e,t,n):this.gen.forward(e,t,n)}async*streamingForward(e,t,n){this.options.enableTracing?yield*this.tracer.streamingForward(e,t,n):yield*this.gen.streamingForward(e,t,n)}getUsage(){return this.gen.getUsage()}resetUsage(){this.gen.resetUsage()}getSignature(){return this.gen.getSignature()}setInstruction(e){this.gen.setInstruction(e)}getInstruction(){return this.gen.getInstruction()}updateMeter(e){this.gen.updateMeter(e)}addAssert(e,t){this.gen.addAssert(e,t)}addStreamingAssert(e,t,n){this.gen.addStreamingAssert(e,t,n)}addFieldProcessor(e,t){this.gen.addFieldProcessor(e,t)}addStreamingFieldProcessor(e,t){this.gen.addStreamingFieldProcessor(e,t)}clone(){return new i(this.gen.clone(),this.options)}async optimize(e={}){let t=Date.now(),n={...this.options,...e},r=n.teacher,o=n.budget??20,s=[...n.examples??[]];if(n.useTraces!==!1){let T=(await this.getTraces()).filter(v=>v.input&&v.output).map(v=>({...v.input,...v.output}));s=[...s,...T]}if(n.generateExamples||s.length===0){let b=n.synthCount??20,T={teacher:r,...n.synthOptions},O=(await new _n(this.gen.getSignature(),T).generate(b)).examples.map(D=>({...D.input,...D.expected}));s=[...s,...O]}if(s.length===0)throw new Error("No training examples available. Provide examples, enable trace usage, or enable example generation.");let a=n.validationSplit??.2,l=[...s].sort(()=>Math.random()-.5),u=Math.floor(l.length*(1-a)),c=l.slice(0,u),p=l.slice(u),d=n.metric;if(!d){let b={ai:r,criteria:n.criteria,...n.judgeOptions};d=new En(this.gen.getSignature(),b).toMetricFn()}let m=new Fn({studentAI:r,numTrials:o,minibatch:!0,minibatchSize:10}),g=o*Math.max(c.length,10),h=await m.compile(this.gen,c,d,{validationExamples:p,maxMetricCalls:g});if(!h.optimizedProgram)throw new Error("GEPA optimization failed: no optimizedProgram returned");h.optimizedProgram.instruction&&this.gen.setInstruction(h.optimizedProgram.instruction),this.currentScore=h.optimizedProgram.bestScore,this.tracer=this.tracer.clone(this.gen);let f=this.currentScore,I=f,A=((await this.options.storage.load(this.options.name,{type:"checkpoint",limit:1}))[0]?.version??0)+1,w={type:"checkpoint",name:this.options.name,version:A,createdAt:new Date,instruction:this.gen.getInstruction(),score:f,optimizerType:"gepa",metadata:{budget:o,trainingExamples:c.length,durationMs:Date.now()-t}};return await this.options.storage.save(this.options.name,w),{score:f,improvement:I,checkpointVersion:A,stats:{trainingExamples:c.length,validationExamples:p.length,durationMs:Date.now()-t}}}async loadLatestCheckpoint(){try{let t=(await this.options.storage.load(this.options.name,{type:"checkpoint",limit:1}))[0];t?.instruction&&(this.gen.setInstruction(t.instruction),this.currentScore=t.score,this.tracer=this.tracer.clone(this.gen))}catch{}}getGen(){return this.gen}getStorage(){return this.options.storage}async getTraces(e){return this.options.storage.load(this.options.name,{type:"trace",...e})}async addFeedback(e,t){let n=await this.options.storage.load(this.options.name,{id:e,type:"trace",limit:1});if(n.length>0){let r=n[0];r.feedback={...r.feedback,...t},await this.options.storage.save(this.options.name,r)}}};var Qo=class{rows=[];baseUrl;dataset;split;config;options;constructor({dataset:e,split:t,config:n,options:r}){this.baseUrl="https://datasets-server.huggingface.co/rows",this.dataset=e,this.split=t,this.config=n,this.options=r}async fetchDataFromAPI(e){try{let t=await fetch(e);if(!t.ok)throw new Error(`Error fetching data: ${t.statusText}`);let n=await t.json();if(!n?.rows)throw new Error("Invalid data format");return n.rows}catch(t){throw console.error("Error fetching data from API:",t),t}}async loadData(){let e=this.options?.offset??0,t=this.options?.length??100,n=encodeURIComponent(this.dataset),r=`${this.baseUrl}?dataset=${n}&config=${this.config}&split=${this.split}&offset=${e}&length=${t}`;return this.rows=await this.fetchDataFromAPI(r),this.rows}setData(e){this.rows=e}getData(){return this.rows}async getRows({count:e,fields:t,renameMap:n}){if(this.rows.length===0)throw new Error("No data loaded, call loadData or setData first.");return this.rows.slice(0,e).map(o=>{let s={};return t.forEach(a=>{let l=a.split("."),u=o.row;for(let p of l)Object.hasOwn(u,p)&&(u=u[p]);if(!u)return;let c=n&&a in n?n[a]:a;if(!c)throw new Error(`Invalid field name: ${a}`);s[c]=u}),s}).filter(o=>Object.keys(o).length!==0)}};var lu=Hn(require("crypto"),1);function Si(i){let e=new Date().toISOString();return{version:1,sections:{},stats:{bulletCount:0,helpfulCount:0,harmfulCount:0,tokenEstimate:0},updatedAt:e,description:i}}function qt(i){return JSON.parse(JSON.stringify(i))}function wp(i){return Math.ceil(i.length/4)}function vi(i,e,t){let n=[],r=[],{maxSectionSize:o=Number.POSITIVE_INFINITY,allowDynamicSections:s=!0,enableAutoPrune:a=!1,protectedBulletIds:l}=t??{},u=new Date().toISOString(),c=l??new Set;for(let p of e){if(!p.section)continue;if(!i.sections[p.section]){if(!s)continue;i.sections[p.section]=[]}let d=i.sections[p.section];switch(p.type){case"ADD":{if(d.length>=o){if(!a)continue;let h=Sp(d,c);if(!h)continue;n.push(h.id),r.push({type:"REMOVE",section:p.section,bulletId:h.id,metadata:{...h.metadata??{},autoPruned:!0,removedAt:u}})}let m=p.bulletId??Rp(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 Mi(i),i.updatedAt=u,{updatedBulletIds:n,autoRemoved:r}}function ki(i,e,t){for(let n of Object.values(i.sections)){let r=n.find(o=>o.id===e);if(r){t==="helpful"?r.helpfulCount+=1:t==="harmful"&&(r.harmfulCount+=1),r.updatedAt=new Date().toISOString(),Mi(i);return}}}function vr(i){let e=i.description?`## Context Playbook
562
562
  ${i.description.trim()}
563
563
  `:`## Context Playbook
564
564
  `,t=Object.entries(i.sections).map(([n,r])=>{let o=r.map(s=>`- [${s.id}] ${s.content}`).join(`
@@ -567,11 +567,11 @@ ${o}`:`### ${n}
567
567
  _(empty)_`}).join(`
568
568
 
569
569
  `);return`${e}
570
- ${t}`.trim()}function Tp(i){let e=i.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"").slice(0,6),t=lu.default.randomBytes(4).toString("hex");return`${e||"ctx"}-${t}`}function wp(i,e){let t=-1,n;for(let o=0;o<i.length;o+=1){let s=i[o];if(e.has(s.id))continue;let a=s.helpfulCount??0,l=s.harmfulCount??0,u=a-l*2,c=Date.parse(s.updatedAt??s.createdAt),p=[u,a,Number.isFinite(c)?c:Number.POSITIVE_INFINITY];if(!n){t=o,n=p;continue}let d=i[t],m=d.helpfulCount??0,g=d.harmfulCount??0,h=m-g*2,f=Date.parse(d.updatedAt??d.createdAt),b=[h,m,Number.isFinite(f)?f: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]=i.splice(t,1);return r}function Oi(i,e=.95){for(let[t,n]of Object.entries(i.sections)){let r=new Map,o=[];for(let s of n){let a=s.content.trim().toLowerCase(),l=r.get(a);l?(l.helpfulCount+=s.helpfulCount,l.harmfulCount+=s.harmfulCount,l.updatedAt=s.updatedAt):(r.set(a,s),o.push(s))}i.sections[t]=o}Mi(i)}function Mi(i){let e=0,t=0,n=0,r=0;for(let o of Object.values(i.sections))for(let s of o)e+=1,t+=s.helpfulCount,n+=s.harmfulCount,r+=Cp(s.content);i.stats={bulletCount:e,helpfulCount:t,harmfulCount:n,tokenEstimate:r}}var Rp={maxEpochs:1,maxReflectorRounds:2,maxSectionSize:25,similarityThreshold:.95,allowDynamicSections:!0},Or=class extends It{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=qt(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(),"",kr(this.playbook)].filter(o=>o&&o.trim().length>0).join(`
570
+ ${t}`.trim()}function Rp(i){let e=i.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"").slice(0,6),t=lu.default.randomBytes(4).toString("hex");return`${e||"ctx"}-${t}`}function Sp(i,e){let t=-1,n;for(let o=0;o<i.length;o+=1){let s=i[o];if(e.has(s.id))continue;let a=s.helpfulCount??0,l=s.harmfulCount??0,u=a-l*2,c=Date.parse(s.updatedAt??s.createdAt),p=[u,a,Number.isFinite(c)?c:Number.POSITIVE_INFINITY];if(!n){t=o,n=p;continue}let d=i[t],m=d.helpfulCount??0,g=d.harmfulCount??0,h=m-g*2,f=Date.parse(d.updatedAt??d.createdAt),I=[h,m,Number.isFinite(f)?f:Number.POSITIVE_INFINITY];(p[0]<I[0]||p[0]===I[0]&&p[1]<I[1]||p[0]===I[0]&&p[1]===I[1]&&p[2]<I[2])&&(t=o,n=p)}if(t===-1)return;let[r]=i.splice(t,1);return r}function Oi(i,e=.95){for(let[t,n]of Object.entries(i.sections)){let r=new Map,o=[];for(let s of n){let a=s.content.trim().toLowerCase(),l=r.get(a);l?(l.helpfulCount+=s.helpfulCount,l.harmfulCount+=s.harmfulCount,l.updatedAt=s.updatedAt):(r.set(a,s),o.push(s))}i.sections[t]=o}Mi(i)}function Mi(i){let e=0,t=0,n=0,r=0;for(let o of Object.values(i.sections))for(let s of o)e+=1,t+=s.helpfulCount,n+=s.harmfulCount,r+=wp(s.content);i.stats={bulletCount:e,helpfulCount:t,harmfulCount:n,tokenEstimate:r}}var vp={maxEpochs:1,maxReflectorRounds:2,maxSectionSize:25,similarityThreshold:.95,allowDynamicSections:!0},kr=class extends Ct{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=qt(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(),"",vr(this.playbook)].filter(o=>o&&o.trim().length>0).join(`
571
571
 
572
- `);e.setDescription(r)}},Yo=class extends Ze{aceConfig;playbook;generatorHistory=[];deltaHistory=[];reflectorProgram;curatorProgram;program;constructor(e,t){super(e),this.aceConfig={...Rp,...t},this.playbook=t?.initialPlaybook!==void 0?qt(t.initialPlaybook):Si()}reset(){super.reset(),this.playbook=this.aceConfig.initialPlaybook!==void 0?qt(this.aceConfig.initialPlaybook):Si(),this.generatorHistory=[],this.deltaHistory=[]}configureAuto(e){switch(e){case"light":this.aceConfig.maxEpochs=1,this.aceConfig.maxReflectorRounds=1;break;case"medium":this.aceConfig.maxEpochs=2,this.aceConfig.maxReflectorRounds=2;break;case"heavy":this.aceConfig.maxEpochs=3,this.aceConfig.maxReflectorRounds=3;break}}async compile(e,t,n,r){let o=r?.aceOptions;o&&(Object.assign(this.aceConfig,{maxEpochs:o.maxEpochs??this.aceConfig.maxEpochs,maxReflectorRounds:o.maxReflectorRounds??this.aceConfig.maxReflectorRounds,maxSectionSize:o.maxSectionSize??this.aceConfig.maxSectionSize,similarityThreshold:o.similarityThreshold??this.aceConfig.similarityThreshold,allowDynamicSections:o.allowDynamicSections??this.aceConfig.allowDynamicSections}),o.initialPlaybook&&(this.playbook=qt(o.initialPlaybook)));let s=Date.now();this.validateExamples(t),this.program=e;let a=await this.extractProgramInstruction(e),l=e.getSignature().getDescription()??"";this.generatorHistory=[],this.deltaHistory=[];let u=Number.NEGATIVE_INFINITY,c=0,p=Math.max(this.aceConfig.maxEpochs,1),d=p*t.length;try{for(let b=0;b<p;b++)for(let x=0;x<t.length;x++){let A=t[x],w=this.composeInstruction(a??l,this.playbook);e.setDescription?.(w);let I=await e.forward(this.studentAI,A);this.stats.totalCalls+=1;let T=await n({prediction:I,example:A});typeof T=="number"&&(this.stats.bestScore=Math.max(this.stats.bestScore,T),u=Math.max(u,T));let v=I?.severity,F=A?.severity,E=this.createGeneratorOutput(I,A),D=F&&v&&F!==v,U=await this.runReflectionRounds({example:A,generatorOutput:E,feedback:F&&v&&F!==v?`Expected severity "${F}" but model predicted "${v}".`:void 0}),V=await this.runCurator({program:e,example:A,reflection:U,playbook:this.playbook}),k=this.normalizeCuratorOperations(V?.operations);k.length===0&&D&&(k=this.inferOperationsFromReflection(U)),k=this.resolveCuratorOperationTargets(k,this.playbook,U,E);let $=V||k.length>0?{...V??{},operations:k}:void 0,N=[];if(k.length>0){let _=this.collectProtectedBulletIds(k),X=vi(this.playbook,k,{maxSectionSize:this.aceConfig.maxSectionSize,allowDynamicSections:this.aceConfig.allowDynamicSections,enableAutoPrune:!0,protectedBulletIds:_});N=X.updatedBulletIds,X.autoRemoved.length>0&&(k.push(...X.autoRemoved),$&&($.operations=k))}if(U?.bulletTags)for(let _ of U.bulletTags)ki(this.playbook,_.id,_.tag);k.length>0&&N.length>0&&Oi(this.playbook,this.aceConfig.similarityThreshold);let Q={example:A,prediction:I,score:typeof T=="number"?T:0,generatorOutput:E,reflection:U,curator:$,timestamp:new Date().toISOString()};this.generatorHistory.push(Q),N.length>0&&$?.operations?.length&&this.deltaHistory.push({epoch:b,exampleIndex:x,operations:$.operations}),c+=1,this.currentRound=c;let M=typeof T=="number"&&Number.isFinite(T)?T:0,L=Number.isFinite(u)?u:M,Y={...r??{},maxIterations:d};await this.updateOptimizationProgress(c,M,{epoch:b,exampleIndex:x,playbookBullets:this.playbook.stats.bulletCount},"ACE",{epochs:p,totalRounds:d},L,{playbookBullets:this.playbook.stats.bulletCount},void 0,Y),this.stats.convergenceInfo.finalImprovement=Math.max(this.stats.convergenceInfo.finalImprovement,M)}}finally{e.setDescription?.(l)}let m=Date.now()-s;this.stats.resourceUsage.totalTime=m,this.stats.convergenceInfo.converged=!0,this.stats.bestScore=Number.isFinite(u)?u:0;let g={playbook:qt(this.playbook),feedback:[...this.generatorHistory],history:[...this.deltaHistory]},h=new Or({baseInstruction:a??l,playbook:this.playbook,artifact:g,bestScore:Number.isFinite(u)?u:0,stats:this.stats,optimizerType:"ACE",optimizationTime:m,totalRounds:c,converged:this.stats.convergenceInfo.converged});return{stats:this.stats,bestScore:Number.isFinite(u)?u:0,finalConfiguration:{strategy:"ace",epochs:p},optimizedProgram:h,playbook:qt(this.playbook),artifact:g}}async applyOnlineUpdate(e){if(!this.program)throw new Error("AxACE: `compile` must be run before `applyOnlineUpdate`");let t=this.createGeneratorOutput(e.prediction,e.example),n=e.prediction?.severity,r=e.example?.severity,o=await this.runReflectionRounds({example:e.example,generatorOutput:t,feedback:e.feedback??(r&&n&&r!==n?`Expected severity "${r}" but model predicted "${n}".`:void 0)}),s=await this.runCurator({program:this.program,example:e.example,reflection:o,playbook:this.playbook}),a=this.normalizeCuratorOperations(s?.operations),l=r&&n&&r!==n;a.length===0&&l&&(a=this.inferOperationsFromReflection(o)),a=this.resolveCuratorOperationTargets(a,this.playbook,o,t);let u=s||a.length>0?{...s??{},operations:a}:void 0;if(o?.bulletTags)for(let p of o.bulletTags)ki(this.playbook,p.id,p.tag);if(a.length>0){let p=this.collectProtectedBulletIds(a),d=vi(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)),Oi(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(),"",kr(t)].filter(r=>r.trim().length>0).join(`
572
+ `);e.setDescription(r)}},Yo=class extends Ye{aceConfig;playbook;generatorHistory=[];deltaHistory=[];reflectorProgram;curatorProgram;program;constructor(e,t){super(e),this.aceConfig={...vp,...t},this.playbook=t?.initialPlaybook!==void 0?qt(t.initialPlaybook):Si()}reset(){super.reset(),this.playbook=this.aceConfig.initialPlaybook!==void 0?qt(this.aceConfig.initialPlaybook):Si(),this.generatorHistory=[],this.deltaHistory=[]}configureAuto(e){switch(e){case"light":this.aceConfig.maxEpochs=1,this.aceConfig.maxReflectorRounds=1;break;case"medium":this.aceConfig.maxEpochs=2,this.aceConfig.maxReflectorRounds=2;break;case"heavy":this.aceConfig.maxEpochs=3,this.aceConfig.maxReflectorRounds=3;break}}async compile(e,t,n,r){let o=r?.aceOptions;o&&(Object.assign(this.aceConfig,{maxEpochs:o.maxEpochs??this.aceConfig.maxEpochs,maxReflectorRounds:o.maxReflectorRounds??this.aceConfig.maxReflectorRounds,maxSectionSize:o.maxSectionSize??this.aceConfig.maxSectionSize,similarityThreshold:o.similarityThreshold??this.aceConfig.similarityThreshold,allowDynamicSections:o.allowDynamicSections??this.aceConfig.allowDynamicSections}),o.initialPlaybook&&(this.playbook=qt(o.initialPlaybook)));let s=Date.now();this.validateExamples(t),this.program=e;let a=await this.extractProgramInstruction(e),l=e.getSignature().getDescription()??"";this.generatorHistory=[],this.deltaHistory=[];let u=Number.NEGATIVE_INFINITY,c=0,p=Math.max(this.aceConfig.maxEpochs,1),d=p*t.length;try{for(let I=0;I<p;I++)for(let x=0;x<t.length;x++){let A=t[x],w=this.composeInstruction(a??l,this.playbook);e.setDescription?.(w);let b=await e.forward(this.studentAI,A);this.stats.totalCalls+=1;let T=await n({prediction:b,example:A});typeof T=="number"&&(this.stats.bestScore=Math.max(this.stats.bestScore,T),u=Math.max(u,T));let v=b?.severity,F=A?.severity,O=this.createGeneratorOutput(b,A),D=F&&v&&F!==v,B=await this.runReflectionRounds({example:A,generatorOutput:O,feedback:F&&v&&F!==v?`Expected severity "${F}" but model predicted "${v}".`:void 0}),Q=await this.runCurator({program:e,example:A,reflection:B,playbook:this.playbook}),S=this.normalizeCuratorOperations(Q?.operations);S.length===0&&D&&(S=this.inferOperationsFromReflection(B)),S=this.resolveCuratorOperationTargets(S,this.playbook,B,O);let G=Q||S.length>0?{...Q??{},operations:S}:void 0,N=[];if(S.length>0){let _=this.collectProtectedBulletIds(S),Z=vi(this.playbook,S,{maxSectionSize:this.aceConfig.maxSectionSize,allowDynamicSections:this.aceConfig.allowDynamicSections,enableAutoPrune:!0,protectedBulletIds:_});N=Z.updatedBulletIds,Z.autoRemoved.length>0&&(S.push(...Z.autoRemoved),G&&(G.operations=S))}if(B?.bulletTags)for(let _ of B.bulletTags)ki(this.playbook,_.id,_.tag);S.length>0&&N.length>0&&Oi(this.playbook,this.aceConfig.similarityThreshold);let Y={example:A,prediction:b,score:typeof T=="number"?T:0,generatorOutput:O,reflection:B,curator:G,timestamp:new Date().toISOString()};this.generatorHistory.push(Y),N.length>0&&G?.operations?.length&&this.deltaHistory.push({epoch:I,exampleIndex:x,operations:G.operations}),c+=1,this.currentRound=c;let M=typeof T=="number"&&Number.isFinite(T)?T:0,L=Number.isFinite(u)?u:M,X={...r??{},maxIterations:d};await this.updateOptimizationProgress(c,M,{epoch:I,exampleIndex:x,playbookBullets:this.playbook.stats.bulletCount},"ACE",{epochs:p,totalRounds:d},L,{playbookBullets:this.playbook.stats.bulletCount},void 0,X),this.stats.convergenceInfo.finalImprovement=Math.max(this.stats.convergenceInfo.finalImprovement,M)}}finally{e.setDescription?.(l)}let m=Date.now()-s;this.stats.resourceUsage.totalTime=m,this.stats.convergenceInfo.converged=!0,this.stats.bestScore=Number.isFinite(u)?u:0;let g={playbook:qt(this.playbook),feedback:[...this.generatorHistory],history:[...this.deltaHistory]},h=new kr({baseInstruction:a??l,playbook:this.playbook,artifact:g,bestScore:Number.isFinite(u)?u:0,stats:this.stats,optimizerType:"ACE",optimizationTime:m,totalRounds:c,converged:this.stats.convergenceInfo.converged});return{stats:this.stats,bestScore:Number.isFinite(u)?u:0,finalConfiguration:{strategy:"ace",epochs:p},optimizedProgram:h,playbook:qt(this.playbook),artifact:g}}async applyOnlineUpdate(e){if(!this.program)throw new Error("AxACE: `compile` must be run before `applyOnlineUpdate`");let t=this.createGeneratorOutput(e.prediction,e.example),n=e.prediction?.severity,r=e.example?.severity,o=await this.runReflectionRounds({example:e.example,generatorOutput:t,feedback:e.feedback??(r&&n&&r!==n?`Expected severity "${r}" but model predicted "${n}".`:void 0)}),s=await this.runCurator({program:this.program,example:e.example,reflection:o,playbook:this.playbook}),a=this.normalizeCuratorOperations(s?.operations),l=r&&n&&r!==n;a.length===0&&l&&(a=this.inferOperationsFromReflection(o)),a=this.resolveCuratorOperationTargets(a,this.playbook,o,t);let u=s||a.length>0?{...s??{},operations:a}:void 0;if(o?.bulletTags)for(let p of o.bulletTags)ki(this.playbook,p.id,p.tag);if(a.length>0){let p=this.collectProtectedBulletIds(a),d=vi(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)),Oi(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(),"",vr(t)].filter(r=>r.trim().length>0).join(`
573
573
 
574
- `)}async extractProgramInstruction(e){try{return e.getSignature().getDescription()??void 0}catch{return}}createGeneratorOutput(e,t){let n=e?.thought?.toString()??"",r=Array.isArray(e?.bullet_ids)?e?.bullet_ids:[];return{reasoning:n,answer:e,bulletIds:r,trajectory:JSON.stringify({example:t,prediction:e}),metadata:{predictedSeverity:e?.severity,expectedSeverity:t?.severity}}}resolveCuratorOperationTargets(e,t,n,r){if(!e.length)return e;let o=[],s=new Set(e.map(c=>c.bulletId).filter(c=>typeof c=="string")),a=new Map,l=(c,p)=>{if(s.has(c))return;let d=this.locateBullet(t,c);if(!d)return;let m=a.get(d.section)??{harmful:[],primary:[],generator:[]};m[p].push(d.id),a.set(d.section,m)};for(let c of n?.bulletTags??[]){let p=c.tag==="harmful"?"harmful":"primary";l(c.id,p)}if(r?.bulletIds)for(let c of r.bulletIds)l(c,"generator");let u=c=>{let p=a.get(c);if(!p)return this.locateFallbackBullet(t,c,s);let d=g=>{for(;g.length>0;){let h=g.shift();if(!s.has(h))return h}},m=d(p.harmful)??d(p.primary)??d(p.generator);return m||this.locateFallbackBullet(t,c,s)};for(let c of e){if((c.type==="UPDATE"||c.type==="REMOVE")&&!c.bulletId){let p=u(c.section);p&&(c.bulletId=p,s.add(p))}(c.type==="UPDATE"||c.type==="REMOVE")&&!c.bulletId||o.push(c)}return o}locateBullet(e,t){for(let n of Object.values(e.sections)){let r=n.find(o=>o.id===t);if(r)return r}}locateFallbackBullet(e,t,n){let r=e.sections[t]??[];for(let o of r)if(!n.has(o.id))return o.id}collectProtectedBulletIds(e){let t=new Set;for(let n of e)n.type==="UPDATE"&&n.bulletId&&t.add(n.bulletId);return t}normalizeCuratorOperations(e){if(!e)return[];if(Array.isArray(e)){let t=[],n=new Set;for(let r of e){if(!r||typeof r!="object")continue;let o=r.type??"ADD",s=typeof o=="string"?o.toUpperCase():"ADD",a=s==="UPDATE"?"UPDATE":s==="REMOVE"?"REMOVE":"ADD",l=r.section??"Guidelines",u=typeof l=="string"&&l.trim().length>0?l.trim():"Guidelines",c=r.content??"",p=typeof c=="string"?c.trim():"";if(a!=="REMOVE"&&p.length===0)continue;let d=r.bulletId??r.id,m=typeof d=="string"&&d.trim().length>0?d.trim():void 0,h=[a,u,p,m??""].join(":");if(n.has(h))continue;n.add(h);let f={type:a,section:u};a!=="REMOVE"&&(f.content=p),m&&(f.bulletId=m);let b=r.metadata;b&&typeof b=="object"&&(f.metadata={...b}),t.push(f)}return t}if(typeof e=="string")try{let t=JSON.parse(e);return this.normalizeCuratorOperations(t)}catch{return[]}if(typeof e=="object"){let t=e;if(t&&Array.isArray(t.operations))return this.normalizeCuratorOperations(t.operations);if(t&&typeof t.operations=="string")try{let n=JSON.parse(t.operations);return this.normalizeCuratorOperations(n)}catch{return[]}return[]}return[]}inferOperationsFromReflection(e){if(!e)return[];let t=[],n=new Set,r=(o,s)=>{if(!s)return;let a=s.trim();if(!a||a.toLowerCase().startsWith("no error"))return;let u=`${o}:${a}`;n.has(u)||(n.add(u),t.push({type:"ADD",section:o,content:a}))};return r("Guidelines",e.keyInsight),r("Response Strategies",e.correctApproach),r("Common Pitfalls",e.errorIdentification),r("Root Cause Notes",e.rootCauseAnalysis),t}async runReflectionRounds({example:e,generatorOutput:t,feedback:n}){let r=Math.max(this.aceConfig.maxReflectorRounds,1),o;for(let s=0;s<r;s++){let a=await this.runReflector({example:e,generatorOutput:t,feedback:n,previousReflection:o});if(!a)break;o=a;let l=a.errorIdentification?.toLowerCase().trim()??"";if(a.metadata?.resolved===!0||l.length===0||l.startsWith("no error")||l.startsWith("resolved"))break}return o}async runReflector({example:e,generatorOutput:t,feedback:n,previousReflection:r}){let o=this.getOrCreateReflectorProgram(),s=this.teacherAI??this.studentAI;try{let a={severity:e?.severity,policyHint:e?.policyHint},c=(this.program?.getSignature()?.getInputFields()??[]).reduce((d,m)=>(m.name in e&&(d[m.name]=e[m.name]),d),{});return await o.forward(s,{question:JSON.stringify(c),generator_answer:JSON.stringify(t.answer),generator_reasoning:t.reasoning,playbook:JSON.stringify({markdown:kr(this.playbook),structured:this.playbook}),expected_answer:a.severity||a.policyHint?JSON.stringify(a):void 0,feedback:n,previous_reflection:r?JSON.stringify(r):void 0})}catch(a){this.verbose&&console.warn("[AxACE] Reflector error:",a instanceof Error?a.message:a);return}}async runCurator({program:e,example:t,reflection:n,playbook:r}){if(!n)return;let o=this.getOrCreateCuratorProgram(),s=this.teacherAI??this.studentAI,u=e.getSignature().getInputFields().reduce((c,p)=>(p.name in t&&(c[p.name]=t[p.name]),c),{});try{return await o.forward(s,{playbook:JSON.stringify({markdown:kr(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=ge().input("question",ge.string("Original task input serialized as JSON")).input("generator_answer",ge.string("Generator output serialized as JSON")).input("generator_reasoning",ge.string("Generator reasoning trace").optional()).input("playbook",ge.string("Current context playbook rendered as markdown")).input("expected_answer",ge.string("Expected output when ground truth is available").optional()).input("feedback",ge.string("External feedback or reward signal").optional()).input("previous_reflection",ge.string("Most recent reflection JSON when running multi-round refinement").optional()).output("reasoning",ge.string("Step-by-step analysis of generator performance")).output("errorIdentification",ge.string("Specific mistakes detected")).output("rootCauseAnalysis",ge.string("Underlying cause of the error")).output("correctApproach",ge.string("What the generator should do differently")).output("keyInsight",ge.string("Reusable insight to remember")).output("bulletTags",ge.json("Array of {id, tag} entries referencing playbook bullets")).build();this.reflectorProgram=We(e)}return this.reflectorProgram}getOrCreateCuratorProgram(){if(!this.curatorProgram){let e=ge().input("playbook",ge.string("Current playbook serialized as JSON")).input("reflection",ge.string("Latest reflection output serialized as JSON")).input("question_context",ge.string("Original task input serialized as JSON")).input("token_budget",ge.number("Approximate token budget for curator response").optional()).output("reasoning",ge.string("Justification for the proposed updates")).output("operations",ge.json("List of operations with type/section/content fields")).build();this.curatorProgram=We(e)}return this.curatorProgram}};var Ln=class extends Ze{maxRounds;maxDemos;maxExamples;batchSize;earlyStoppingPatience;costMonitoring;maxTokensPerGeneration;verboseMode;debugMode;traces=[];constructor(e){super(e);let t=e.options||{};this.maxRounds=t.maxRounds??3,this.maxDemos=t.maxDemos??4,this.maxExamples=t.maxExamples??16,this.batchSize=t.batchSize??1,this.earlyStoppingPatience=t.earlyStoppingPatience??0,this.costMonitoring=t.costMonitoring??!1,this.maxTokensPerGeneration=t.maxTokensPerGeneration??0,this.verboseMode=t.verboseMode??!0,this.debugMode=t.debugMode??!1}async compileRound(e,t,n,r,o){let s=Date.now(),a=o?.maxDemos??this.maxDemos,l={modelConfig:{temperature:.7}};this.maxTokensPerGeneration>0&&(l.modelConfig.max_tokens=this.maxTokensPerGeneration);let u=vp([...t],this.maxExamples),c=this.traces.length,p=e.getId();for(let d=0;d<u.length;d+=this.batchSize){d>0&&(l.modelConfig.temperature=.7+.001*d);let m=u.slice(d,d+this.batchSize);for(let g of m){if(!g||typeof g!="object")continue;let h=t.filter(x=>x!==g);e.setDemos([{traces:h,programId:p}]);let f=this.getTeacherOrStudentAI();this.stats.totalCalls++;let b;try{let x={...l,maxRetries:1};b=await e.forward(f,g,x),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(x){(this.verboseMode||this.debugMode)&&console.warn(`Student model failed during bootstrap: ${x instanceof Error?x.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 s=Sp(this.traces),a=0;return this.traces.length>0&&(a=this.stats.successfulDemos/Math.max(1,this.stats.totalCalls)),await this.logOptimizationComplete("BootstrapFewShot",a,{maxRounds:this.maxRounds,maxDemos:this.maxDemos,batchSize:this.batchSize,successRate:a,demosGenerated:s.length,tracesCollected:this.traces.length},r),{demos:s,stats:this.stats,bestScore:a,finalConfiguration:{maxRounds:this.maxRounds,maxDemos:this.maxDemos,batchSize:this.batchSize,successRate:a}}}};function Sp(i){let e=new Map;for(let n of i)if(e.has(n.programId)){let r=e.get(n.programId);r&&r.push(n.trace)}else e.set(n.programId,[n.trace]);let t=[];return e.forEach((n,r)=>{t.push({traces:n,programId:r})}),t}var vp=(i,e)=>{let t=[...i];for(let n=t.length-1;n>0;n--){let r=Math.floor(Math.random()*(n+1)),o=t[n],s=t[r];if(!o||!s)throw new Error("Invalid array elements");[t[n],t[r]]=[s,o]}return t.slice(0,e)};var Xo=class extends Ze{numTrials;minibatch;minibatchSize;earlyStoppingTrials;minImprovementThreshold;sampleCount;crossoverEvery;tieEpsilon;paretoSetSize;mergeMax;mergesUsed=0;mergesDue=0;totalMergesTested=0;lastIterFoundNewProgram=!1;rngState;mergeAttemptKeys=new Set;mergeCompositionKeys=new Set;samplerState={epoch:-1,shuffled:[],freq:new Map};constructor(e){super(e),this.numTrials=e.numTrials??24,this.minibatch=e.minibatch??!0,this.minibatchSize=e.minibatchSize??8,this.earlyStoppingTrials=e.earlyStoppingTrials??5,this.minImprovementThreshold=e.minImprovementThreshold??0,this.sampleCount=e.sampleCount??1,this.crossoverEvery=Math.max(0,Math.floor(e?.crossoverEvery??Math.max(3,Math.floor(this.numTrials/3)))),this.tieEpsilon=Number.isFinite(e?.tieEpsilon)?Number(e?.tieEpsilon):0;let t=e?.seed,n=Number.isFinite(t)?Math.floor(Number(t)):0;this.rngState=n&&n!==0?n:123456789;let r=e?.paretoSetSize;this.paretoSetSize=r&&r>0?Math.min(1e3,Math.max(5,Math.floor(r))):Math.max(10,Math.min(200,this.minibatchSize*3));let o=e?.mergeMax;this.mergeMax=Math.max(0,Math.floor(o??5)),this.mergesUsed=0,this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold}reset(){super.reset(),this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold,this.mergesUsed=0,this.mergesDue=0,this.totalMergesTested=0,this.lastIterFoundNewProgram=!1,this.mergeAttemptKeys.clear(),this.mergeCompositionKeys.clear(),this.samplerState.epoch=-1,this.samplerState.shuffled=[],this.samplerState.freq.clear()}configureAuto(e){switch(e){case"light":this.numTrials=8,this.minibatch=!0,this.minibatchSize=6;break;case"medium":this.numTrials=16,this.minibatch=!0,this.minibatchSize=10;break;case"heavy":this.numTrials=28,this.minibatch=!0,this.minibatchSize=14;break}}async compile(e,t,n,r){let o=Date.now(),s=e;this.validateExamples(t),r?.auto&&this.configureAuto(r.auto);let a=s.getNodePrograms?.();if(!a||a.length===0)throw new Error("AxGEPAFlow: flow has no nodes to optimize");let l=r?.validationExamples,u=r?.feedbackExamples,c=(l&&l.length>0?l:t).slice(0,this.paretoSetSize),p=u&&u.length>0?u:t;this.getOptimizerLogger(r)?.({name:"OptimizationStart",value:{optimizerType:"GEPA-Flow",exampleCount:t.length,validationCount:c.length,config:{numTrials:this.numTrials,minibatch:this.minibatch}}});let m=async(k,$)=>{try{s.setAllNodeInstructions?.(k);let N=await s.forward(this.studentAI,$,{sampleCount:this.sampleCount});return this.stats.totalCalls+=1,await n({prediction:N,example:$})||{}}catch{return{}}},g=async(k,$)=>{let N=[];for(let Q of $)N.push(await m(k,Q));return Vo(N)},h={};for(let k of a)h[k.name]=await this.getBaseInstruction(k.program);let f=[{cfg:{...h},parent:void 0,scores:await g(h,c)}],b=k=>{let $=r?.paretoMetricKey,N=r?.paretoScalarize;if(typeof N=="function")return N(k);if($)return Number.isFinite(k[$])?k[$]:0;let Q=Object.values(k);return Q.length?Q.reduce((M,L)=>M+L,0)/Q.length:0},x=[],A=async(k,$)=>{let N=[];for(let Q of $){let M=await m(k,Q);N.push(b(M))}return N};x.push(await A(h,c));let w=Ct(f.map((k,$)=>({idx:$,scores:k.scores})),this.tieEpsilon).map(k=>k.idx),I=0,T=new Set,v=r?.maxMetricCalls;if(!Number.isFinite(v)||v<=0)throw new Error("AxGEPA-Flow: options.maxMetricCalls must be set to a positive integer");let F=Math.floor(v);for(let k=0;k<this.numTrials&&!(F!==void 0&&this.stats.totalCalls>=Math.max(1,Math.floor(F)));k++){let $=x[0]?.length??0,N=[];for(let R=0;R<$;R++){let y=Number.NEGATIVE_INFINITY,P=new Set;for(let B=0;B<x.length;B++){let j=x[B][R];j>y+this.tieEpsilon?(y=j,P.clear(),P.add(B)):Math.abs(j-y)<=this.tieEpsilon&&P.add(B)}N.push(P)}let Q=x.map(R=>jt(R));if(this.mergeMax>0&&this.mergesDue>0&&this.lastIterFoundNewProgram){let R=Sr(N,Q),y=new Set;for(let q of R)for(let K of q)y.add(K);let P=Array.from(y),B=q=>{let K=[],z=q;for(;z!==void 0;)K.push(z),z=f[z]?.parent;return K},j=q=>q.length?q[Math.floor(this.rand()*q.length)]:void 0,H;for(let q=0;q<10&&!H&&!(P.length<2);q++){let K=j(P),z=j(P);if(K===z)continue;z<K&&([K,z]=[z,K]);let W=new Set(B(K)),ee=new Set(B(z));if(W.has(z)||ee.has(K))continue;let ie=[...W].filter(ae=>ee.has(ae));if(ie.length===0)continue;let de=[];for(let ae of ie){let Re=f[ae].cfg,Be=f[K].cfg,Ie=f[z].cfg,Ne=!1,Ve=new Set([...Object.keys(Re),...Object.keys(Be),...Object.keys(Ie)]);for(let Me of Ve){let xt=Re[Me],wt=Be[Me],le=Ie[Me];if(wt===xt&&le!==wt||le===xt&&wt!==le){Ne=!0;break}}Ne&&de.push(ae)}if(de.length===0)continue;let fe=de.map(ae=>Math.max(1e-9,Q[ae])),Ae=this.rand()*fe.reduce((ae,Re)=>ae+Re,0),ye=de[de.length-1];for(let ae=0;ae<de.length;ae++){if(Ae<fe[ae]){ye=de[ae];break}Ae-=fe[ae]}let S=Q[ye],G=Q[K],Z=Q[z];if(S>Math.min(G,Z))continue;let he=`${K}|${z}|${ye}`;if(this.mergeAttemptKeys.has(he))continue;this.mergeAttemptKeys.add(he);let ue=`${K}|${z}|${ye}`;T.has(ue)||(H={i:K,j:z,a:ye})}if(this.lastIterFoundNewProgram=!1,H){let{i:q,j:K,a:z}=H,{cfg:W,descSig:ee}=this.systemAwareMergeWithSig(f,q,K,(le,we)=>Q[le]>=Q[we]?le:we),ie=`${Math.min(q,K)}|${Math.max(q,K)}|${ee}`;if(this.mergeCompositionKeys.has(ie))continue;this.mergeCompositionKeys.add(ie);let de=x[q],fe=x[K],Ae=Array.from({length:de.length},(le,we)=>we),ye=Ae.filter(le=>(de[le]??0)>(fe[le]??0)),S=Ae.filter(le=>(fe[le]??0)>(de[le]??0)),G=Ae.filter(le=>!(ye.includes(le)||S.includes(le))),Z=5,he=Math.ceil(Z/3),ue=(le,we)=>{if(we<=0||le.length===0)return[];if(le.length<=we)return[...le];let je=[],qe=new Set;for(;je.length<we;){let Xe=Math.floor(this.rand()*le.length);qe.has(Xe)||(qe.add(Xe),je.push(le[Xe]))}return je},ae=[];ae.push(...ue(ye,Math.min(he,ye.length))),ae.push(...ue(S,Math.min(he,S.length)));let Re=Z-ae.length;ae.push(...ue(G,Math.max(0,Re)));let Be=Z-ae.length;if(Be>0){let le=Ae.filter(we=>!ae.includes(we));ae.push(...ue(le,Math.min(Be,le.length)))}let Ie=ae.slice(0,Math.min(Z,Ae.length)),Ne=Ie.map(le=>c[le]),Me=(await A(W,Ne)).reduce((le,we)=>le+we,0),xt=Ie.reduce((le,we)=>le+(de[we]??0),0),wt=Ie.reduce((le,we)=>le+(fe[we]??0),0);if(Me>=Math.max(xt,wt)+this.tieEpsilon){let le=await g(W,c);f.push({cfg:W,parent:z,scores:le}),x.push(await A(W,c));let we=w.length,je=ct(w.map(Xe=>f[Xe].scores))??0;w=Ct(f.map((Xe,Ht)=>({idx:Ht,scores:Xe.scores})),this.tieEpsilon).map(Xe=>Xe.idx);let qe=ct(w.map(Xe=>f[Xe].scores))??0;(w.length>we||qe>je+1e-6)&&(I=0),this.mergesDue-=1,this.totalMergesTested+=1,T.add(`${Math.min(q,K)}|${Math.max(q,K)}|${z}`)}continue}}let M=vr(N,Q,()=>this.rand());this.lastIterFoundNewProgram=!1;let L=this.minibatch?this.nextMinibatchIndices(p.length,k).map(R=>p[R]):p;if(r?.skipPerfectScore??!0){let R=Number(r?.perfectScore??1),y=await A(f[M].cfg,L);if(y.length>0&&y.every(P=>P>=R))continue}let Y=!1,_={...f[M].cfg},X="reflective_mutation",xe=k%a.length,ne=a[xe],re,J;if(Y&&this.mergesUsed<this.mergeMax){let R=(M+1)%f.length,y=q=>{let K=[],z=q;for(;z!==void 0;)K.push(z),z=f[z]?.parent;return K},P=y(M),B=y(R),j=P.find(q=>B.includes(q)),H=!0;if(j||(H=!1),(B.includes(M)||P.includes(R))&&(H=!1),H){let q=f[j].cfg,K=f[M].cfg,z=f[R].cfg,W=!1,ee=new Set([...Object.keys(q),...Object.keys(K),...Object.keys(z)]);for(let ie of ee){let de=q[ie],fe=K[ie],Ae=z[ie];if(fe===de&&Ae!==fe||Ae===de&&fe!==Ae){W=!0;break}}W||(H=!1)}if(H){let q=Math.min(M,R),K=Math.max(M,R),z=`${q}|${K}|${j}`;if(!T.has(z)){let W=b(f[j].scores),ee=b(f[M].scores),ie=b(f[R].scores);W<=Math.min(ee,ie)&&(_=this.systemAwareMerge(f,M,R,(de,fe)=>{let Ae=b(f[de].scores),ye=b(f[fe].scores);return Ae>=ye?de:fe}),X="system_merge",this.mergesUsed+=1,T.add(z))}}else{let q=f[M].cfg[ne.name],K=r?.gepaAdapter,z;if(K)try{let W=await K.evaluate(L,{...f[M].cfg},!0);re=Array.isArray(W?.scores)?W.scores.reduce((fe,Ae)=>fe+(Number(Ae)||0),0):void 0;let ee=K.make_reflective_dataset({...f[M].cfg},W,[ne.name]),de=(await K.propose_new_texts?.({...f[M].cfg},ee,[ne.name]))?.[ne.name];typeof de=="string"&&de.length>0&&(z=de)}catch{}if(z||(z=await this.reflectModuleInstruction(ne.name,q,s,a,{...f[M].cfg},L,async({prediction:W,example:ee})=>{let ie=await n({prediction:W,example:ee}),de=Object.values(ie||{});return de.length?de.reduce((fe,Ae)=>fe+Ae,0)/de.length:0},r)),_[ne.name]=z,K&&re!==void 0)try{let W=await K.evaluate(L,_,!1);J=Array.isArray(W?.scores)?W.scores.reduce((ee,ie)=>ee+(Number(ie)||0),0):void 0}catch{}}}else{let R=f[M].cfg[ne.name],y=r?.gepaAdapter,P;if(y)try{let B=await y.evaluate(L,{...f[M].cfg},!0);re=Array.isArray(B?.scores)?B.scores.reduce((K,z)=>K+(Number(z)||0),0):void 0;let j=y.make_reflective_dataset({...f[M].cfg},B,[ne.name]),q=(await y.propose_new_texts?.({...f[M].cfg},j,[ne.name]))?.[ne.name];typeof q=="string"&&q.length>0&&(P=q)}catch{}if(P||(P=await this.reflectModuleInstruction(ne.name,R,s,a,{...f[M].cfg},L,async({prediction:B,example:j})=>{let H=await n({prediction:B,example:j}),q=Object.values(H||{});return q.length?q.reduce((K,z)=>K+z,0)/q.length:0},r)),_[ne.name]=P,y&&re!==void 0)try{let B=await y.evaluate(L,_,!1);J=Array.isArray(B?.scores)?B.scores.reduce((j,H)=>j+(Number(H)||0),0):void 0}catch{}}let oe=await A(f[M].cfg,L),Pe=await A(_,L),se=oe.reduce((R,y)=>R+y,0),Te=Pe.reduce((R,y)=>R+y,0);if(this.currentRound=k+1,await this.updateOptimizationProgress(this.currentRound,Te,{modules:a.length,mutatedModule:ne.name,totalRounds:this.numTrials},"GEPA-Flow",{strategy:X,paretoSetSize:c.length},Te,{idx:M},{...r??{},maxIterations:this.numTrials}),!(Te>se+this.tieEpsilon&&(re===void 0||J===void 0||J>re+this.tieEpsilon))){if(++I>=this.earlyStoppingTrials)break;continue}let _e=await g(_,c);f.push({cfg:_,parent:M,scores:_e}),x.push(await A(_,c));let Ge=w.length,C=ct(w.map(R=>f[R].scores))??0;w=Ct(f.map((R,y)=>({idx:y,scores:R.scores})),this.tieEpsilon).map(R=>R.idx);let O=ct(w.map(R=>f[R].scores))??0;if(w.length>Ge||O>C+1e-6)I=0;else if(I++,I>=this.earlyStoppingTrials)break;this.lastIterFoundNewProgram=!0,this.mergeMax>0&&this.totalMergesTested<this.mergeMax&&(this.mergesDue+=1)}let E=Ct(f.map((k,$)=>({idx:$,scores:k.scores})),this.tieEpsilon),D=E.length>0?Math.max(...E.map(k=>b(k.scores))):0,U=ct(E.map(k=>k.scores));this.stats.convergenceInfo.converged=!0;let V=this.getMergedCustomLabels(r);return this.recordParetoMetrics(E.length,f.length,"GEPA-Flow",U,V),{demos:[],stats:this.stats,bestScore:D,paretoFront:E.map(k=>({demos:[],scores:k.scores,configuration:{candidate:k.idx},dominatedSolutions:k.dominated})),paretoFrontSize:E.length,hypervolume:U,finalConfiguration:{strategy:"gepa_flow_pareto",candidates:f.length}}}async getBaseInstruction(e){let t=e?.getInstruction?.();if(t&&t.length>0)return t;let r=e?.getSignature?.()?.getDescription?.();return r&&r.length>0?r:"Follow the task precisely. Be concise, correct, and consistent."}async evaluateOnSet(e,t,n,r){let o=[];for(let s of n){let a=await this.evaluateOne(e,t,s,r);o.push(a)}return o}async evaluateAvg(e,t,n,r){let o=await this.evaluateOnSet(e,t,n,r);return o.length>0?jt(o):0}async evaluateOne(e,t,n,r){try{e.setAllNodeInstructions?.(t);let o=await e.forward(this.studentAI,n,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let s=await r({prediction:o,example:n});return typeof s=="number"&&!Number.isNaN(s)?((typeof this.targetScore=="number"?s>=this.targetScore:s>=.5)&&(this.stats.successfulDemos+=1),s):0}catch{return 0}}async reflectModuleInstruction(e,t,n,r,o,s,a,l){let u=[],c=[];for(let b of s)try{o[e]=t,n.setAllNodeInstructions?.(o);let x=await n.forward(this.studentAI,b,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let A=await a({prediction:x,example:b});c.push({input:b,prediction:x,score:typeof A=="number"?A:0})}catch{c.push({input:b,prediction:{},score:0})}let p=l?.overrideTeacherAI??this.teacherAI??this.studentAI,d=We('moduleName:string "Target module", minibatch:json "Array of {input,prediction,score}", evalFeedback?:string[] "Evaluator feedback when available" -> feedbackSummary:string "Concise module-focused feedback"'),m=[],g=l?.feedbackFn;if(typeof g=="function")for(let b of c){let x=g({prediction:b.prediction,example:b.input});x&&(Array.isArray(x)?m.push(...x):m.push(x))}let h="";try{h=(await d.forward(p,{moduleName:e,minibatch:c,evalFeedback:m}))?.feedbackSummary?.trim()||""}catch{}let f=We('moduleName:string "Target module", currentInstruction:string "Current instruction", feedbackSummary?:string "Summarized feedback", minibatch:json "Array of {input,prediction,score}" -> newInstruction:string "Improved instruction (1-6 sentences) for the module"');try{let x=(await f.forward(p,{moduleName:e,currentInstruction:t,feedbackSummary:h,minibatch:c}))?.newInstruction?.trim();if(x&&x.length>16)return x}catch{}return`${t.trim()} Focus on step-by-step, module-specific reasoning and factual grounding.`.slice(0,2e3)}updateSamplerShuffled(e){let t=Array.from({length:e},(l,u)=>u);for(let l=t.length-1;l>0;l--){let u=Math.floor(this.rand()*(l+1));[t[l],t[u]]=[t[u],t[l]]}for(let l of t)this.samplerState.freq.set(l,(this.samplerState.freq.get(l)??0)+1);let n=this.minibatchSize,r=e%n,o=r===0?0:n-r,s=Array.from({length:e},(l,u)=>u).sort((l,u)=>(this.samplerState.freq.get(l)??0)-(this.samplerState.freq.get(u)??0)),a=[...t];for(let l=0;l<o;l++){let u=s[l%s.length];a.push(u),this.samplerState.freq.set(u,(this.samplerState.freq.get(u)??0)+1)}this.samplerState.shuffled=a,this.samplerState.epoch+=1}nextMinibatchIndices(e,t){this.samplerState.epoch===-1&&(this.samplerState.epoch=0,this.updateSamplerShuffled(e));let n=this.minibatchSize,r=Math.max(1,Math.floor(this.samplerState.shuffled.length/n)),o=Math.floor(t/r);for(;o>=this.samplerState.epoch;)this.updateSamplerShuffled(e);let s=t*n%this.samplerState.shuffled.length;return this.samplerState.shuffled.slice(s,s+n)}systemAwareMergeWithSig(e,t,n,r){let o=f=>{let b=[],x=f;for(;x!==void 0;)b.push(x),x=e[x]?.parent;return b},s=o(t),a=o(n),u=s.find(f=>a.includes(f))??t,c=e[u].cfg,p=e[t].cfg,d=e[n].cfg,m={},g=[],h=Array.from(new Set([...Object.keys(c),...Object.keys(p),...Object.keys(d)])).sort();for(let f of h){let b=c[f],x=p[f],A=d[f];if(x===b&&A!==x)m[f]=A,g.push("j");else if(A===b&&x!==A)m[f]=x,g.push("i");else if(x!==A&&x!==b&&A!==b){let w=r(t,n);m[f]=w===t?x:A,g.push(w===t?"i":"j")}else m[f]=x??A??b,g.push("i")}return{cfg:m,descSig:g.join("|")}}rand(){return this.rngState^=this.rngState<<13,this.rngState^=this.rngState>>>17,this.rngState^=this.rngState<<5,(this.rngState>>>0)/4294967296}systemAwareMerge(e,t,n,r){let o=h=>{let f=[],b=h;for(;b!==void 0;)f.push(b),b=e[b]?.parent;return f},s=o(t),a=o(n),u=s.find(h=>a.includes(h))??t,c=e[u].cfg,p=e[t].cfg,d=e[n].cfg,m={},g=new Set([...Object.keys(c),...Object.keys(p),...Object.keys(d)]);for(let h of g){let f=c[h],b=p[h],x=d[h];if(b===f&&x!==b)m[h]=x;else if(x===f&&b!==x)m[h]=b;else if(b!==x&&b!==f&&x!==f){let A=r(t,n);m[h]=A===t?b:x}else m[h]=b??x??f}return m}};var Zo=class{endpoint;timeout;retryAttempts;retryDelay;logger;constructor(e){this.endpoint=e.endpoint.replace(/\/$/,""),this.timeout=e.timeout??3e4,this.retryAttempts=e.retryAttempts??3,this.retryDelay=e.retryDelay??1e3,this.logger=e.logger}async healthCheck(){try{return(await this.fetchWithRetry("/health",{method:"GET"})).ok}catch(e){return this.logger?.({name:"Notification",id:"health_check_failed",value:`Health check failed: ${e}`}),!1}}async createOptimizationJob(e){let t=await this.fetchWithRetry("/optimize",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!t.ok){let n=await t.text();throw new Error(`Failed to create optimization job: ${n}`)}return t.json()}async getJobStatus(e){let t=await this.fetchWithRetry(`/jobs/${e}`,{method:"GET"});if(!t.ok){let n=await t.text();throw new Error(`Failed to get job status: ${n}`)}return t.json()}async cancelJob(e){let t=await this.fetchWithRetry(`/jobs/${e}`,{method:"DELETE"});if(!t.ok){let n=await t.text();throw new Error(`Failed to cancel job: ${n}`)}}async suggestParameters(e){let t=await this.fetchWithRetry(`/studies/${e}/suggest`,{method:"POST",headers:{"Content-Type":"application/json"}});if(!t.ok){let n=await t.text();throw new Error(`Failed to suggest parameters: ${n}`)}return t.json()}async evaluateTrial(e){let t=await this.fetchWithRetry(`/studies/${e.study_name}/evaluate`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!t.ok){let n=await t.text();throw new Error(`Failed to evaluate trial: ${n}`)}}async getStudyResults(e){let t=await this.fetchWithRetry(`/studies/${e}/results`,{method:"GET"});if(!t.ok){let n=await t.text();throw new Error(`Failed to get study results: ${n}`)}return t.json()}async deleteStudy(e){let t=await this.fetchWithRetry(`/studies/${e}`,{method:"DELETE"});if(!t.ok){let n=await t.text();throw new Error(`Failed to delete study: ${n}`)}}async listStudies(){let e=await this.fetchWithRetry("/studies",{method:"GET"});if(!e.ok){let t=await e.text();throw new Error(`Failed to list studies: ${t}`)}return e.json()}async waitForJobCompletion(e,t=2e3,n=3e5){let r=Date.now();for(;Date.now()-r<n;){let o=await this.getJobStatus(e);if(["completed","failed","cancelled"].includes(o.status))return o;this.logger?.({name:"Notification",id:"job_status",value:`Job ${e} status: ${o.status}, waiting...`}),await this.sleep(t)}throw new Error(`Job ${e} did not complete within ${n}ms`)}async fetchWithRetry(e,t){let n=`${this.endpoint}${e}`,r=null;for(let o=0;o<this.retryAttempts;o++)try{let s=new AbortController,a=setTimeout(()=>s.abort(),this.timeout),l=await fetch(n,{...t,signal:s.signal});return clearTimeout(a),l}catch(s){r=s,this.logger?.({name:"Notification",id:"retry_attempt",value:`Attempt ${o+1} failed: ${s}`}),o<this.retryAttempts-1&&await this.sleep(this.retryDelay*Math.pow(2,o))}throw new Error(`Request failed after ${this.retryAttempts} attempts: ${r?.message}`)}sleep(e){return new Promise(t=>setTimeout(t,e))}};var es=class extends Ze{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 Zo(t)}this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold}defaultResultPicker=async e=>{if(e.type==="function"){let o=e.results.findIndex(s=>!s.isError);return o>=0?o:0}let t=new Map;for(let o of e.results){let s=JSON.stringify(o.sample??{}),a=t.get(s);a?a.count+=1:t.set(s,{count:1,firstIndex:o.index})}let n="",r={count:-1,firstIndex:0};for(let[o,s]of t.entries())s.count>r.count&&(r=s,n=o);return t.get(n)?.firstIndex??0};configureAuto(e){switch(e){case"light":this.numCandidates=3,this.numTrials=10,this.minibatch=!0,this.minibatchSize=20;break;case"medium":this.numCandidates=5,this.numTrials=20,this.minibatch=!0,this.minibatchSize=25;break;case"heavy":this.numCandidates=7,this.numTrials=30,this.minibatch=!0,this.minibatchSize=30;break}}generateTips(){return["Be very specific and detailed in your instructions.","Focus on step-by-step reasoning in your instructions.","Provide clear constraints and guidelines in your instructions.","Keep your instructions concise and to the point.","Emphasize accuracy and precision in your instructions.","Include examples of good outputs in your instructions.","Focus on handling edge cases in your instructions.","Explicitly outline the reasoning process in your instructions."]}async generateProgramSummary(e,t){let r=`
574
+ `)}async extractProgramInstruction(e){try{return e.getSignature().getDescription()??void 0}catch{return}}createGeneratorOutput(e,t){let n=e?.thought?.toString()??"",r=Array.isArray(e?.bullet_ids)?e?.bullet_ids:[];return{reasoning:n,answer:e,bulletIds:r,trajectory:JSON.stringify({example:t,prediction:e}),metadata:{predictedSeverity:e?.severity,expectedSeverity:t?.severity}}}resolveCuratorOperationTargets(e,t,n,r){if(!e.length)return e;let o=[],s=new Set(e.map(c=>c.bulletId).filter(c=>typeof c=="string")),a=new Map,l=(c,p)=>{if(s.has(c))return;let d=this.locateBullet(t,c);if(!d)return;let m=a.get(d.section)??{harmful:[],primary:[],generator:[]};m[p].push(d.id),a.set(d.section,m)};for(let c of n?.bulletTags??[]){let p=c.tag==="harmful"?"harmful":"primary";l(c.id,p)}if(r?.bulletIds)for(let c of r.bulletIds)l(c,"generator");let u=c=>{let p=a.get(c);if(!p)return this.locateFallbackBullet(t,c,s);let d=g=>{for(;g.length>0;){let h=g.shift();if(!s.has(h))return h}},m=d(p.harmful)??d(p.primary)??d(p.generator);return m||this.locateFallbackBullet(t,c,s)};for(let c of e){if((c.type==="UPDATE"||c.type==="REMOVE")&&!c.bulletId){let p=u(c.section);p&&(c.bulletId=p,s.add(p))}(c.type==="UPDATE"||c.type==="REMOVE")&&!c.bulletId||o.push(c)}return o}locateBullet(e,t){for(let n of Object.values(e.sections)){let r=n.find(o=>o.id===t);if(r)return r}}locateFallbackBullet(e,t,n){let r=e.sections[t]??[];for(let o of r)if(!n.has(o.id))return o.id}collectProtectedBulletIds(e){let t=new Set;for(let n of e)n.type==="UPDATE"&&n.bulletId&&t.add(n.bulletId);return t}normalizeCuratorOperations(e){if(!e)return[];if(Array.isArray(e)){let t=[],n=new Set;for(let r of e){if(!r||typeof r!="object")continue;let o=r.type??"ADD",s=typeof o=="string"?o.toUpperCase():"ADD",a=s==="UPDATE"?"UPDATE":s==="REMOVE"?"REMOVE":"ADD",l=r.section??"Guidelines",u=typeof l=="string"&&l.trim().length>0?l.trim():"Guidelines",c=r.content??"",p=typeof c=="string"?c.trim():"";if(a!=="REMOVE"&&p.length===0)continue;let d=r.bulletId??r.id,m=typeof d=="string"&&d.trim().length>0?d.trim():void 0,h=[a,u,p,m??""].join(":");if(n.has(h))continue;n.add(h);let f={type:a,section:u};a!=="REMOVE"&&(f.content=p),m&&(f.bulletId=m);let I=r.metadata;I&&typeof I=="object"&&(f.metadata={...I}),t.push(f)}return t}if(typeof e=="string")try{let t=JSON.parse(e);return this.normalizeCuratorOperations(t)}catch{return[]}if(typeof e=="object"){let t=e;if(t&&Array.isArray(t.operations))return this.normalizeCuratorOperations(t.operations);if(t&&typeof t.operations=="string")try{let n=JSON.parse(t.operations);return this.normalizeCuratorOperations(n)}catch{return[]}return[]}return[]}inferOperationsFromReflection(e){if(!e)return[];let t=[],n=new Set,r=(o,s)=>{if(!s)return;let a=s.trim();if(!a||a.toLowerCase().startsWith("no error"))return;let u=`${o}:${a}`;n.has(u)||(n.add(u),t.push({type:"ADD",section:o,content:a}))};return r("Guidelines",e.keyInsight),r("Response Strategies",e.correctApproach),r("Common Pitfalls",e.errorIdentification),r("Root Cause Notes",e.rootCauseAnalysis),t}async runReflectionRounds({example:e,generatorOutput:t,feedback:n}){let r=Math.max(this.aceConfig.maxReflectorRounds,1),o;for(let s=0;s<r;s++){let a=await this.runReflector({example:e,generatorOutput:t,feedback:n,previousReflection:o});if(!a)break;o=a;let l=a.errorIdentification?.toLowerCase().trim()??"";if(a.metadata?.resolved===!0||l.length===0||l.startsWith("no error")||l.startsWith("resolved"))break}return o}async runReflector({example:e,generatorOutput:t,feedback:n,previousReflection:r}){let o=this.getOrCreateReflectorProgram(),s=this.teacherAI??this.studentAI;try{let a={severity:e?.severity,policyHint:e?.policyHint},c=(this.program?.getSignature()?.getInputFields()??[]).reduce((d,m)=>(m.name in e&&(d[m.name]=e[m.name]),d),{});return await o.forward(s,{question:JSON.stringify(c),generator_answer:JSON.stringify(t.answer),generator_reasoning:t.reasoning,playbook:JSON.stringify({markdown:vr(this.playbook),structured:this.playbook}),expected_answer:a.severity||a.policyHint?JSON.stringify(a):void 0,feedback:n,previous_reflection:r?JSON.stringify(r):void 0})}catch(a){this.verbose&&console.warn("[AxACE] Reflector error:",a instanceof Error?a.message:a);return}}async runCurator({program:e,example:t,reflection:n,playbook:r}){if(!n)return;let o=this.getOrCreateCuratorProgram(),s=this.teacherAI??this.studentAI,u=e.getSignature().getInputFields().reduce((c,p)=>(p.name in t&&(c[p.name]=t[p.name]),c),{});try{return await o.forward(s,{playbook:JSON.stringify({markdown:vr(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=fe().input("question",fe.string("Original task input serialized as JSON")).input("generator_answer",fe.string("Generator output serialized as JSON")).input("generator_reasoning",fe.string("Generator reasoning trace").optional()).input("playbook",fe.string("Current context playbook rendered as markdown")).input("expected_answer",fe.string("Expected output when ground truth is available").optional()).input("feedback",fe.string("External feedback or reward signal").optional()).input("previous_reflection",fe.string("Most recent reflection JSON when running multi-round refinement").optional()).output("reasoning",fe.string("Step-by-step analysis of generator performance")).output("errorIdentification",fe.string("Specific mistakes detected")).output("rootCauseAnalysis",fe.string("Underlying cause of the error")).output("correctApproach",fe.string("What the generator should do differently")).output("keyInsight",fe.string("Reusable insight to remember")).output("bulletTags",fe.json("Array of {id, tag} entries referencing playbook bullets")).build();this.reflectorProgram=ze(e)}return this.reflectorProgram}getOrCreateCuratorProgram(){if(!this.curatorProgram){let e=fe().input("playbook",fe.string("Current playbook serialized as JSON")).input("reflection",fe.string("Latest reflection output serialized as JSON")).input("question_context",fe.string("Original task input serialized as JSON")).input("token_budget",fe.number("Approximate token budget for curator response").optional()).output("reasoning",fe.string("Justification for the proposed updates")).output("operations",fe.json("List of operations with type/section/content fields")).build();this.curatorProgram=ze(e)}return this.curatorProgram}};var Nn=class extends Ye{maxRounds;maxDemos;maxExamples;batchSize;earlyStoppingPatience;costMonitoring;maxTokensPerGeneration;verboseMode;debugMode;traces=[];constructor(e){super(e);let t=e.options||{};this.maxRounds=t.maxRounds??3,this.maxDemos=t.maxDemos??4,this.maxExamples=t.maxExamples??16,this.batchSize=t.batchSize??1,this.earlyStoppingPatience=t.earlyStoppingPatience??0,this.costMonitoring=t.costMonitoring??!1,this.maxTokensPerGeneration=t.maxTokensPerGeneration??0,this.verboseMode=t.verboseMode??!0,this.debugMode=t.debugMode??!1}async compileRound(e,t,n,r,o){let s=Date.now(),a=o?.maxDemos??this.maxDemos,l={modelConfig:{temperature:.7}};this.maxTokensPerGeneration>0&&(l.modelConfig.max_tokens=this.maxTokensPerGeneration);let u=Op([...t],this.maxExamples),c=this.traces.length,p=e.getId();for(let d=0;d<u.length;d+=this.batchSize){d>0&&(l.modelConfig.temperature=.7+.001*d);let m=u.slice(d,d+this.batchSize);for(let g of m){if(!g||typeof g!="object")continue;let h=t.filter(x=>x!==g);e.setDemos([{traces:h,programId:p}]);let f=this.getTeacherOrStudentAI();this.stats.totalCalls++;let I;try{let x={...l,maxRetries:1};I=await e.forward(f,g,x),this.costMonitoring&&(this.stats.estimatedTokenUsage+=JSON.stringify(g).length/4+JSON.stringify(I).length/4),await r({prediction:I,example:g})>=.5&&(this.traces=[...this.traces,...e.getTraces()],this.stats.successfulDemos++)}catch(x){(this.verboseMode||this.debugMode)&&console.warn(`Student model failed during bootstrap: ${x instanceof Error?x.message:"Unknown error"}`),I={}}if(this.traces.length>=a)return}}if(this.earlyStoppingPatience>0){let m=this.traces.length-c;if(!this.stats.earlyStopping)this.stats.earlyStopping={bestScoreRound:m>0?n:0,patienceExhausted:!1,reason:"No improvement detected"};else if(m>0)this.stats.earlyStopping.bestScoreRound=n;else if(n-this.stats.earlyStopping.bestScoreRound>=this.earlyStoppingPatience){this.stats.earlyStopping.patienceExhausted=!0,this.stats.earlyStopped=!0,this.stats.earlyStopping.reason=`No improvement for ${this.earlyStoppingPatience} rounds`;return}}}async compile(e,t,n,r){this.validateExamples(t,!1);let o=r?.maxIterations??this.maxRounds;this.traces=[],this.reset();for(let l=0;l<o&&(await this.compileRound(e,t,l,n,r),!this.stats.earlyStopped);l++);if(this.traces.length===0)throw new Error("No demonstrations found. Either provide more examples or improve the existing ones.");let s=kp(this.traces),a=0;return this.traces.length>0&&(a=this.stats.successfulDemos/Math.max(1,this.stats.totalCalls)),await this.logOptimizationComplete("BootstrapFewShot",a,{maxRounds:this.maxRounds,maxDemos:this.maxDemos,batchSize:this.batchSize,successRate:a,demosGenerated:s.length,tracesCollected:this.traces.length},r),{demos:s,stats:this.stats,bestScore:a,finalConfiguration:{maxRounds:this.maxRounds,maxDemos:this.maxDemos,batchSize:this.batchSize,successRate:a}}}};function kp(i){let e=new Map;for(let n of i)if(e.has(n.programId)){let r=e.get(n.programId);r&&r.push(n.trace)}else e.set(n.programId,[n.trace]);let t=[];return e.forEach((n,r)=>{t.push({traces:n,programId:r})}),t}var Op=(i,e)=>{let t=[...i];for(let n=t.length-1;n>0;n--){let r=Math.floor(Math.random()*(n+1)),o=t[n],s=t[r];if(!o||!s)throw new Error("Invalid array elements");[t[n],t[r]]=[s,o]}return t.slice(0,e)};var Xo=class extends Ye{numTrials;minibatch;minibatchSize;earlyStoppingTrials;minImprovementThreshold;sampleCount;crossoverEvery;tieEpsilon;paretoSetSize;mergeMax;mergesUsed=0;mergesDue=0;totalMergesTested=0;lastIterFoundNewProgram=!1;rngState;mergeAttemptKeys=new Set;mergeCompositionKeys=new Set;samplerState={epoch:-1,shuffled:[],freq:new Map};constructor(e){super(e),this.numTrials=e.numTrials??24,this.minibatch=e.minibatch??!0,this.minibatchSize=e.minibatchSize??8,this.earlyStoppingTrials=e.earlyStoppingTrials??5,this.minImprovementThreshold=e.minImprovementThreshold??0,this.sampleCount=e.sampleCount??1,this.crossoverEvery=Math.max(0,Math.floor(e?.crossoverEvery??Math.max(3,Math.floor(this.numTrials/3)))),this.tieEpsilon=Number.isFinite(e?.tieEpsilon)?Number(e?.tieEpsilon):0;let t=e?.seed,n=Number.isFinite(t)?Math.floor(Number(t)):0;this.rngState=n&&n!==0?n:123456789;let r=e?.paretoSetSize;this.paretoSetSize=r&&r>0?Math.min(1e3,Math.max(5,Math.floor(r))):Math.max(10,Math.min(200,this.minibatchSize*3));let o=e?.mergeMax;this.mergeMax=Math.max(0,Math.floor(o??5)),this.mergesUsed=0,this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold}reset(){super.reset(),this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold,this.mergesUsed=0,this.mergesDue=0,this.totalMergesTested=0,this.lastIterFoundNewProgram=!1,this.mergeAttemptKeys.clear(),this.mergeCompositionKeys.clear(),this.samplerState.epoch=-1,this.samplerState.shuffled=[],this.samplerState.freq.clear()}configureAuto(e){switch(e){case"light":this.numTrials=8,this.minibatch=!0,this.minibatchSize=6;break;case"medium":this.numTrials=16,this.minibatch=!0,this.minibatchSize=10;break;case"heavy":this.numTrials=28,this.minibatch=!0,this.minibatchSize=14;break}}async compile(e,t,n,r){let o=Date.now(),s=e;this.validateExamples(t),r?.auto&&this.configureAuto(r.auto);let a=s.getNodePrograms?.();if(!a||a.length===0)throw new Error("AxGEPAFlow: flow has no nodes to optimize");let l=r?.validationExamples,u=r?.feedbackExamples,c=(l&&l.length>0?l:t).slice(0,this.paretoSetSize),p=u&&u.length>0?u:t;this.getOptimizerLogger(r)?.({name:"OptimizationStart",value:{optimizerType:"GEPA-Flow",exampleCount:t.length,validationCount:c.length,config:{numTrials:this.numTrials,minibatch:this.minibatch}}});let m=async(S,G)=>{try{s.setAllNodeInstructions?.(S);let N=await s.forward(this.studentAI,G,{sampleCount:this.sampleCount});return this.stats.totalCalls+=1,await n({prediction:N,example:G})||{}}catch{return{}}},g=async(S,G)=>{let N=[];for(let Y of G)N.push(await m(S,Y));return Vo(N)},h={};for(let S of a)h[S.name]=await this.getBaseInstruction(S.program);let f=[{cfg:{...h},parent:void 0,scores:await g(h,c)}],I=S=>{let G=r?.paretoMetricKey,N=r?.paretoScalarize;if(typeof N=="function")return N(S);if(G)return Number.isFinite(S[G])?S[G]:0;let Y=Object.values(S);return Y.length?Y.reduce((M,L)=>M+L,0)/Y.length:0},x=[],A=async(S,G)=>{let N=[];for(let Y of G){let M=await m(S,Y);N.push(I(M))}return N};x.push(await A(h,c));let w=Tt(f.map((S,G)=>({idx:G,scores:S.scores})),this.tieEpsilon).map(S=>S.idx),b=0,T=new Set,v=r?.maxMetricCalls;if(!Number.isFinite(v)||v<=0)throw new Error("AxGEPA-Flow: options.maxMetricCalls must be set to a positive integer");let F=Math.floor(v);for(let S=0;S<this.numTrials&&!(F!==void 0&&this.stats.totalCalls>=Math.max(1,Math.floor(F)));S++){let G=x[0]?.length??0,N=[];for(let R=0;R<G;R++){let y=Number.NEGATIVE_INFINITY,E=new Set;for(let U=0;U<x.length;U++){let j=x[U][R];j>y+this.tieEpsilon?(y=j,E.clear(),E.add(U)):Math.abs(j-y)<=this.tieEpsilon&&E.add(U)}N.push(E)}let Y=x.map(R=>jt(R));if(this.mergeMax>0&&this.mergesDue>0&&this.lastIterFoundNewProgram){let R=Rr(N,Y),y=new Set;for(let q of R)for(let H of q)y.add(H);let E=Array.from(y),U=q=>{let H=[],z=q;for(;z!==void 0;)H.push(z),z=f[z]?.parent;return H},j=q=>q.length?q[Math.floor(this.rand()*q.length)]:void 0,K;for(let q=0;q<10&&!K&&!(E.length<2);q++){let H=j(E),z=j(E);if(H===z)continue;z<H&&([H,z]=[z,H]);let W=new Set(U(H)),ee=new Set(U(z));if(W.has(z)||ee.has(H))continue;let ce=[...W].filter(ie=>ee.has(ie));if(ce.length===0)continue;let ae=[];for(let ie of ce){let Ge=f[ie].cfg,we=f[H].cfg,ve=f[z].cfg,He=!1,Pe=new Set([...Object.keys(Ge),...Object.keys(we),...Object.keys(ve)]);for(let ct of Pe){let At=Ge[ct],wt=we[ct],ue=ve[ct];if(wt===At&&ue!==wt||ue===At&&wt!==ue){He=!0;break}}He&&ae.push(ie)}if(ae.length===0)continue;let xe=ae.map(ie=>Math.max(1e-9,Y[ie])),ye=this.rand()*xe.reduce((ie,Ge)=>ie+Ge,0),P=ae[ae.length-1];for(let ie=0;ie<ae.length;ie++){if(ye<xe[ie]){P=ae[ie];break}ye-=xe[ie]}let $=Y[P],V=Y[H],ne=Y[z];if($>Math.min(V,ne))continue;let he=`${H}|${z}|${P}`;if(this.mergeAttemptKeys.has(he))continue;this.mergeAttemptKeys.add(he);let pe=`${H}|${z}|${P}`;T.has(pe)||(K={i:H,j:z,a:P})}if(this.lastIterFoundNewProgram=!1,K){let{i:q,j:H,a:z}=K,{cfg:W,descSig:ee}=this.systemAwareMergeWithSig(f,q,H,(ue,Te)=>Y[ue]>=Y[Te]?ue:Te),ce=`${Math.min(q,H)}|${Math.max(q,H)}|${ee}`;if(this.mergeCompositionKeys.has(ce))continue;this.mergeCompositionKeys.add(ce);let ae=x[q],xe=x[H],ye=Array.from({length:ae.length},(ue,Te)=>Te),P=ye.filter(ue=>(ae[ue]??0)>(xe[ue]??0)),$=ye.filter(ue=>(xe[ue]??0)>(ae[ue]??0)),V=ye.filter(ue=>!(P.includes(ue)||$.includes(ue))),ne=5,he=Math.ceil(ne/3),pe=(ue,Te)=>{if(Te<=0||ue.length===0)return[];if(ue.length<=Te)return[...ue];let Ze=[],Qe=new Set;for(;Ze.length<Te;){let Ke=Math.floor(this.rand()*ue.length);Qe.has(Ke)||(Qe.add(Ke),Ze.push(ue[Ke]))}return Ze},ie=[];ie.push(...pe(P,Math.min(he,P.length))),ie.push(...pe($,Math.min(he,$.length)));let Ge=ne-ie.length;ie.push(...pe(V,Math.max(0,Ge)));let we=ne-ie.length;if(we>0){let ue=ye.filter(Te=>!ie.includes(Te));ie.push(...pe(ue,Math.min(we,ue.length)))}let ve=ie.slice(0,Math.min(ne,ye.length)),He=ve.map(ue=>c[ue]),ct=(await A(W,He)).reduce((ue,Te)=>ue+Te,0),At=ve.reduce((ue,Te)=>ue+(ae[Te]??0),0),wt=ve.reduce((ue,Te)=>ue+(xe[Te]??0),0);if(ct>=Math.max(At,wt)+this.tieEpsilon){let ue=await g(W,c);f.push({cfg:W,parent:z,scores:ue}),x.push(await A(W,c));let Te=w.length,Ze=ut(w.map(Ke=>f[Ke].scores))??0;w=Tt(f.map((Ke,_r)=>({idx:_r,scores:Ke.scores})),this.tieEpsilon).map(Ke=>Ke.idx);let Qe=ut(w.map(Ke=>f[Ke].scores))??0;(w.length>Te||Qe>Ze+1e-6)&&(b=0),this.mergesDue-=1,this.totalMergesTested+=1,T.add(`${Math.min(q,H)}|${Math.max(q,H)}|${z}`)}continue}}let M=Sr(N,Y,()=>this.rand());this.lastIterFoundNewProgram=!1;let L=this.minibatch?this.nextMinibatchIndices(p.length,S).map(R=>p[R]):p;if(r?.skipPerfectScore??!0){let R=Number(r?.perfectScore??1),y=await A(f[M].cfg,L);if(y.length>0&&y.every(E=>E>=R))continue}let X=!1,_={...f[M].cfg},Z="reflective_mutation",Ae=S%a.length,re=a[Ae],oe,J;if(X&&this.mergesUsed<this.mergeMax){let R=(M+1)%f.length,y=q=>{let H=[],z=q;for(;z!==void 0;)H.push(z),z=f[z]?.parent;return H},E=y(M),U=y(R),j=E.find(q=>U.includes(q)),K=!0;if(j||(K=!1),(U.includes(M)||E.includes(R))&&(K=!1),K){let q=f[j].cfg,H=f[M].cfg,z=f[R].cfg,W=!1,ee=new Set([...Object.keys(q),...Object.keys(H),...Object.keys(z)]);for(let ce of ee){let ae=q[ce],xe=H[ce],ye=z[ce];if(xe===ae&&ye!==xe||ye===ae&&xe!==ye){W=!0;break}}W||(K=!1)}if(K){let q=Math.min(M,R),H=Math.max(M,R),z=`${q}|${H}|${j}`;if(!T.has(z)){let W=I(f[j].scores),ee=I(f[M].scores),ce=I(f[R].scores);W<=Math.min(ee,ce)&&(_=this.systemAwareMerge(f,M,R,(ae,xe)=>{let ye=I(f[ae].scores),P=I(f[xe].scores);return ye>=P?ae:xe}),Z="system_merge",this.mergesUsed+=1,T.add(z))}}else{let q=f[M].cfg[re.name],H=r?.gepaAdapter,z;if(H)try{let W=await H.evaluate(L,{...f[M].cfg},!0);oe=Array.isArray(W?.scores)?W.scores.reduce((xe,ye)=>xe+(Number(ye)||0),0):void 0;let ee=H.make_reflective_dataset({...f[M].cfg},W,[re.name]),ae=(await H.propose_new_texts?.({...f[M].cfg},ee,[re.name]))?.[re.name];typeof ae=="string"&&ae.length>0&&(z=ae)}catch{}if(z||(z=await this.reflectModuleInstruction(re.name,q,s,a,{...f[M].cfg},L,async({prediction:W,example:ee})=>{let ce=await n({prediction:W,example:ee}),ae=Object.values(ce||{});return ae.length?ae.reduce((xe,ye)=>xe+ye,0)/ae.length:0},r)),_[re.name]=z,H&&oe!==void 0)try{let W=await H.evaluate(L,_,!1);J=Array.isArray(W?.scores)?W.scores.reduce((ee,ce)=>ee+(Number(ce)||0),0):void 0}catch{}}}else{let R=f[M].cfg[re.name],y=r?.gepaAdapter,E;if(y)try{let U=await y.evaluate(L,{...f[M].cfg},!0);oe=Array.isArray(U?.scores)?U.scores.reduce((H,z)=>H+(Number(z)||0),0):void 0;let j=y.make_reflective_dataset({...f[M].cfg},U,[re.name]),q=(await y.propose_new_texts?.({...f[M].cfg},j,[re.name]))?.[re.name];typeof q=="string"&&q.length>0&&(E=q)}catch{}if(E||(E=await this.reflectModuleInstruction(re.name,R,s,a,{...f[M].cfg},L,async({prediction:U,example:j})=>{let K=await n({prediction:U,example:j}),q=Object.values(K||{});return q.length?q.reduce((H,z)=>H+z,0)/q.length:0},r)),_[re.name]=E,y&&oe!==void 0)try{let U=await y.evaluate(L,_,!1);J=Array.isArray(U?.scores)?U.scores.reduce((j,K)=>j+(Number(K)||0),0):void 0}catch{}}let se=await A(f[M].cfg,L),Ee=await A(_,L),le=se.reduce((R,y)=>R+y,0),Ce=Ee.reduce((R,y)=>R+y,0);if(this.currentRound=S+1,await this.updateOptimizationProgress(this.currentRound,Ce,{modules:a.length,mutatedModule:re.name,totalRounds:this.numTrials},"GEPA-Flow",{strategy:Z,paretoSetSize:c.length},Ce,{idx:M},{...r??{},maxIterations:this.numTrials}),!(Ce>le+this.tieEpsilon&&(oe===void 0||J===void 0||J>oe+this.tieEpsilon))){if(++b>=this.earlyStoppingTrials)break;continue}let _e=await g(_,c);f.push({cfg:_,parent:M,scores:_e}),x.push(await A(_,c));let $e=w.length,C=ut(w.map(R=>f[R].scores))??0;w=Tt(f.map((R,y)=>({idx:y,scores:R.scores})),this.tieEpsilon).map(R=>R.idx);let k=ut(w.map(R=>f[R].scores))??0;if(w.length>$e||k>C+1e-6)b=0;else if(b++,b>=this.earlyStoppingTrials)break;this.lastIterFoundNewProgram=!0,this.mergeMax>0&&this.totalMergesTested<this.mergeMax&&(this.mergesDue+=1)}let O=Tt(f.map((S,G)=>({idx:G,scores:S.scores})),this.tieEpsilon),D=O.length>0?Math.max(...O.map(S=>I(S.scores))):0,B=ut(O.map(S=>S.scores));this.stats.convergenceInfo.converged=!0;let Q=this.getMergedCustomLabels(r);return this.recordParetoMetrics(O.length,f.length,"GEPA-Flow",B,Q),{demos:[],stats:this.stats,bestScore:D,paretoFront:O.map(S=>({demos:[],scores:S.scores,configuration:{candidate:S.idx},dominatedSolutions:S.dominated})),paretoFrontSize:O.length,hypervolume:B,finalConfiguration:{strategy:"gepa_flow_pareto",candidates:f.length}}}async getBaseInstruction(e){let t=e?.getInstruction?.();if(t&&t.length>0)return t;let r=e?.getSignature?.()?.getDescription?.();return r&&r.length>0?r:"Follow the task precisely. Be concise, correct, and consistent."}async evaluateOnSet(e,t,n,r){let o=[];for(let s of n){let a=await this.evaluateOne(e,t,s,r);o.push(a)}return o}async evaluateAvg(e,t,n,r){let o=await this.evaluateOnSet(e,t,n,r);return o.length>0?jt(o):0}async evaluateOne(e,t,n,r){try{e.setAllNodeInstructions?.(t);let o=await e.forward(this.studentAI,n,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let s=await r({prediction:o,example:n});return typeof s=="number"&&!Number.isNaN(s)?((typeof this.targetScore=="number"?s>=this.targetScore:s>=.5)&&(this.stats.successfulDemos+=1),s):0}catch{return 0}}async reflectModuleInstruction(e,t,n,r,o,s,a,l){let u=[],c=[];for(let I of s)try{o[e]=t,n.setAllNodeInstructions?.(o);let x=await n.forward(this.studentAI,I,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let A=await a({prediction:x,example:I});c.push({input:I,prediction:x,score:typeof A=="number"?A:0})}catch{c.push({input:I,prediction:{},score:0})}let p=l?.overrideTeacherAI??this.teacherAI??this.studentAI,d=ze('moduleName:string "Target module", minibatch:json "Array of {input,prediction,score}", evalFeedback?:string[] "Evaluator feedback when available" -> feedbackSummary:string "Concise module-focused feedback"'),m=[],g=l?.feedbackFn;if(typeof g=="function")for(let I of c){let x=g({prediction:I.prediction,example:I.input});x&&(Array.isArray(x)?m.push(...x):m.push(x))}let h="";try{h=(await d.forward(p,{moduleName:e,minibatch:c,evalFeedback:m}))?.feedbackSummary?.trim()||""}catch{}let f=ze('moduleName:string "Target module", currentInstruction:string "Current instruction", feedbackSummary?:string "Summarized feedback", minibatch:json "Array of {input,prediction,score}" -> newInstruction:string "Improved instruction (1-6 sentences) for the module"');try{let x=(await f.forward(p,{moduleName:e,currentInstruction:t,feedbackSummary:h,minibatch:c}))?.newInstruction?.trim();if(x&&x.length>16)return x}catch{}return`${t.trim()} Focus on step-by-step, module-specific reasoning and factual grounding.`.slice(0,2e3)}updateSamplerShuffled(e){let t=Array.from({length:e},(l,u)=>u);for(let l=t.length-1;l>0;l--){let u=Math.floor(this.rand()*(l+1));[t[l],t[u]]=[t[u],t[l]]}for(let l of t)this.samplerState.freq.set(l,(this.samplerState.freq.get(l)??0)+1);let n=this.minibatchSize,r=e%n,o=r===0?0:n-r,s=Array.from({length:e},(l,u)=>u).sort((l,u)=>(this.samplerState.freq.get(l)??0)-(this.samplerState.freq.get(u)??0)),a=[...t];for(let l=0;l<o;l++){let u=s[l%s.length];a.push(u),this.samplerState.freq.set(u,(this.samplerState.freq.get(u)??0)+1)}this.samplerState.shuffled=a,this.samplerState.epoch+=1}nextMinibatchIndices(e,t){this.samplerState.epoch===-1&&(this.samplerState.epoch=0,this.updateSamplerShuffled(e));let n=this.minibatchSize,r=Math.max(1,Math.floor(this.samplerState.shuffled.length/n)),o=Math.floor(t/r);for(;o>=this.samplerState.epoch;)this.updateSamplerShuffled(e);let s=t*n%this.samplerState.shuffled.length;return this.samplerState.shuffled.slice(s,s+n)}systemAwareMergeWithSig(e,t,n,r){let o=f=>{let I=[],x=f;for(;x!==void 0;)I.push(x),x=e[x]?.parent;return I},s=o(t),a=o(n),u=s.find(f=>a.includes(f))??t,c=e[u].cfg,p=e[t].cfg,d=e[n].cfg,m={},g=[],h=Array.from(new Set([...Object.keys(c),...Object.keys(p),...Object.keys(d)])).sort();for(let f of h){let I=c[f],x=p[f],A=d[f];if(x===I&&A!==x)m[f]=A,g.push("j");else if(A===I&&x!==A)m[f]=x,g.push("i");else if(x!==A&&x!==I&&A!==I){let w=r(t,n);m[f]=w===t?x:A,g.push(w===t?"i":"j")}else m[f]=x??A??I,g.push("i")}return{cfg:m,descSig:g.join("|")}}rand(){return this.rngState^=this.rngState<<13,this.rngState^=this.rngState>>>17,this.rngState^=this.rngState<<5,(this.rngState>>>0)/4294967296}systemAwareMerge(e,t,n,r){let o=h=>{let f=[],I=h;for(;I!==void 0;)f.push(I),I=e[I]?.parent;return f},s=o(t),a=o(n),u=s.find(h=>a.includes(h))??t,c=e[u].cfg,p=e[t].cfg,d=e[n].cfg,m={},g=new Set([...Object.keys(c),...Object.keys(p),...Object.keys(d)]);for(let h of g){let f=c[h],I=p[h],x=d[h];if(I===f&&x!==I)m[h]=x;else if(x===f&&I!==x)m[h]=I;else if(I!==x&&I!==f&&x!==f){let A=r(t,n);m[h]=A===t?I:x}else m[h]=I??x??f}return m}};var Zo=class{endpoint;timeout;retryAttempts;retryDelay;logger;constructor(e){this.endpoint=e.endpoint.replace(/\/$/,""),this.timeout=e.timeout??3e4,this.retryAttempts=e.retryAttempts??3,this.retryDelay=e.retryDelay??1e3,this.logger=e.logger}async healthCheck(){try{return(await this.fetchWithRetry("/health",{method:"GET"})).ok}catch(e){return this.logger?.({name:"Notification",id:"health_check_failed",value:`Health check failed: ${e}`}),!1}}async createOptimizationJob(e){let t=await this.fetchWithRetry("/optimize",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!t.ok){let n=await t.text();throw new Error(`Failed to create optimization job: ${n}`)}return t.json()}async getJobStatus(e){let t=await this.fetchWithRetry(`/jobs/${e}`,{method:"GET"});if(!t.ok){let n=await t.text();throw new Error(`Failed to get job status: ${n}`)}return t.json()}async cancelJob(e){let t=await this.fetchWithRetry(`/jobs/${e}`,{method:"DELETE"});if(!t.ok){let n=await t.text();throw new Error(`Failed to cancel job: ${n}`)}}async suggestParameters(e){let t=await this.fetchWithRetry(`/studies/${e}/suggest`,{method:"POST",headers:{"Content-Type":"application/json"}});if(!t.ok){let n=await t.text();throw new Error(`Failed to suggest parameters: ${n}`)}return t.json()}async evaluateTrial(e){let t=await this.fetchWithRetry(`/studies/${e.study_name}/evaluate`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!t.ok){let n=await t.text();throw new Error(`Failed to evaluate trial: ${n}`)}}async getStudyResults(e){let t=await this.fetchWithRetry(`/studies/${e}/results`,{method:"GET"});if(!t.ok){let n=await t.text();throw new Error(`Failed to get study results: ${n}`)}return t.json()}async deleteStudy(e){let t=await this.fetchWithRetry(`/studies/${e}`,{method:"DELETE"});if(!t.ok){let n=await t.text();throw new Error(`Failed to delete study: ${n}`)}}async listStudies(){let e=await this.fetchWithRetry("/studies",{method:"GET"});if(!e.ok){let t=await e.text();throw new Error(`Failed to list studies: ${t}`)}return e.json()}async waitForJobCompletion(e,t=2e3,n=3e5){let r=Date.now();for(;Date.now()-r<n;){let o=await this.getJobStatus(e);if(["completed","failed","cancelled"].includes(o.status))return o;this.logger?.({name:"Notification",id:"job_status",value:`Job ${e} status: ${o.status}, waiting...`}),await this.sleep(t)}throw new Error(`Job ${e} did not complete within ${n}ms`)}async fetchWithRetry(e,t){let n=`${this.endpoint}${e}`,r=null;for(let o=0;o<this.retryAttempts;o++)try{let s=new AbortController,a=setTimeout(()=>s.abort(),this.timeout),l=await fetch(n,{...t,signal:s.signal});return clearTimeout(a),l}catch(s){r=s,this.logger?.({name:"Notification",id:"retry_attempt",value:`Attempt ${o+1} failed: ${s}`}),o<this.retryAttempts-1&&await this.sleep(this.retryDelay*Math.pow(2,o))}throw new Error(`Request failed after ${this.retryAttempts} attempts: ${r?.message}`)}sleep(e){return new Promise(t=>setTimeout(t,e))}};var es=class extends Ye{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 Zo(t)}this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold}defaultResultPicker=async e=>{if(e.type==="function"){let o=e.results.findIndex(s=>!s.isError);return o>=0?o:0}let t=new Map;for(let o of e.results){let s=JSON.stringify(o.sample??{}),a=t.get(s);a?a.count+=1:t.set(s,{count:1,firstIndex:o.index})}let n="",r={count:-1,firstIndex:0};for(let[o,s]of t.entries())s.count>r.count&&(r=s,n=o);return t.get(n)?.firstIndex??0};configureAuto(e){switch(e){case"light":this.numCandidates=3,this.numTrials=10,this.minibatch=!0,this.minibatchSize=20;break;case"medium":this.numCandidates=5,this.numTrials=20,this.minibatch=!0,this.minibatchSize=25;break;case"heavy":this.numCandidates=7,this.numTrials=30,this.minibatch=!0,this.minibatchSize=30;break}}generateTips(){return["Be very specific and detailed in your instructions.","Focus on step-by-step reasoning in your instructions.","Provide clear constraints and guidelines in your instructions.","Keep your instructions concise and to the point.","Emphasize accuracy and precision in your instructions.","Include examples of good outputs in your instructions.","Focus on handling edge cases in your instructions.","Explicitly outline the reasoning process in your instructions."]}async generateProgramSummary(e,t){let r=`
575
575
  Analyze this language model program and provide a concise summary of its purpose and structure.
576
576
 
577
577
  Program Signature: ${e.getSignature()}
@@ -611,12 +611,12 @@ Requirements:
611
611
  5. Keep it concise but comprehensive
612
612
 
613
613
  Generate a single, well-crafted instruction:
614
- Instruction:`;try{let m=(await We('programSummary?:string "Program context" , datasetSummary?:string "Dataset context" , tip?:string "Generation tip" -> instructionText:string "Well-crafted instruction for the program"').forward(n,{programSummary:r??"",datasetSummary:o??"",tip:e??""})).instructionText;if(m&&m.trim().length>10)return m.trim()}catch{}let u=["Analyze the input systematically and provide a precise, well-reasoned response.","Think through this step-by-step, considering all relevant factors before responding.","Examine the input carefully and generate an accurate, detailed answer.","Process the information methodically and deliver a clear, comprehensive response.","Consider the context thoroughly and provide a thoughtful, accurate answer."],c=u[t%u.length]||u[0];return e&&(c=`${c} ${e}`),c}async proposeInstructionCandidates(e,t,n=[]){let r=[],o=this.getTeacherOrStudentAI(t),s,a;this.programAwareProposer&&(s=await this.generateProgramSummary(e,o)),this.dataAwareProposer&&(a=await this.generateDatasetSummary([...n],o));let l=this.tipAwareProposer?this.generateTips():[];for(let u=0;u<this.numCandidates;u++){let c=l.length>0?u%l.length:-1,p=c>=0?l[c]:void 0,d=await this.generateInstruction({tip:p,candidateIndex:u,ai:o,programSummary:s,datasetSummary:a,previousInstructions:r});r.push(d)}return r}async bootstrapFewShotExamples(e,t,n){return(await new Ln({studentAI:this.studentAI,options:{maxDemos:this.maxBootstrappedDemos,maxRounds:3,verboseMode:this.verbose??!1}}).compile(e,n,t,{maxDemos:this.maxBootstrappedDemos})).demos||[]}selectLabeledExamples(e){let t=[],n=new Set;for(;n.size<this.maxLabeledDemos&&n.size<e.length;){let r=Math.floor(Math.random()*e.length);if(!n.has(r)){n.add(r);let o=e[r];o&&t.push(o)}}return t}applyConfigToProgram(e,t,n,r){if(e.setInstruction&&e.setInstruction(t.instruction),e.setDemos){let o=[...n.slice(0,t.bootstrappedDemos)];if(t.labeledExamples>0&&e.getId){let s=e.getId();o.push({traces:r.slice(0,t.labeledExamples),programId:s})}o.length>0&&e.setDemos(o)}}async compile(e,t,n,r){let o=Date.now();if(this.validateExamples(t),this.setupRandomSeed(),r?.auto&&this.configureAuto(r.auto),!this.pythonClient)throw new Error("AxMiPRO v2 requires the Python optimizer service. Please configure optimizerEndpoint.");if(!await this.pythonClient.healthCheck())throw new Error("Python optimizer service is not available or unhealthy");return await this.compilePython(e,t,n,r)}applyConfigToAxGen(e,t,n,r){"setInstruction"in e&&typeof e.setInstruction=="function"&&e.setInstruction(t.instruction);let o=[...n.slice(0,t.bootstrappedDemos)];if(t.labeledExamples>0){let s=e.getId();o.push({traces:r.slice(0,t.labeledExamples),programId:s})}o.length>0&&e.setDemos(o)}getConfiguration(){return{numCandidates:this.numCandidates,initTemperature:this.initTemperature,maxBootstrappedDemos:this.maxBootstrappedDemos,maxLabeledDemos:this.maxLabeledDemos,numTrials:this.numTrials,minibatch:this.minibatch,minibatchSize:this.minibatchSize,minibatchFullEvalSteps:this.minibatchFullEvalSteps,programAwareProposer:this.programAwareProposer,dataAwareProposer:this.dataAwareProposer,tipAwareProposer:this.tipAwareProposer,fewshotAwareProposer:this.fewshotAwareProposer,earlyStoppingTrials:this.earlyStoppingTrials,minImprovementThreshold:this.minImprovementThreshold,bayesianOptimization:this.bayesianOptimization,acquisitionFunction:this.acquisitionFunction,explorationWeight:this.explorationWeight,sampleCount:this.sampleCount}}updateConfiguration(e){e.numCandidates!==void 0&&(this.numCandidates=e.numCandidates),e.initTemperature!==void 0&&(this.initTemperature=e.initTemperature),e.maxBootstrappedDemos!==void 0&&(this.maxBootstrappedDemos=e.maxBootstrappedDemos),e.maxLabeledDemos!==void 0&&(this.maxLabeledDemos=e.maxLabeledDemos),e.numTrials!==void 0&&(this.numTrials=e.numTrials),e.minibatch!==void 0&&(this.minibatch=e.minibatch),e.minibatchSize!==void 0&&(this.minibatchSize=e.minibatchSize),e.earlyStoppingTrials!==void 0&&(this.earlyStoppingTrials=e.earlyStoppingTrials),e.minImprovementThreshold!==void 0&&(this.minImprovementThreshold=e.minImprovementThreshold),e.sampleCount!==void 0&&(this.sampleCount=e.sampleCount)}reset(){super.reset(),this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold}validateProgram(e){let t=[],n=[];return{isValid:t.length===0,issues:t,suggestions:n}}async compilePython(e,t,n,r){if(!this.pythonClient)throw new Error("Python client not initialized");let o=Date.now();this.localScoreHistory=[],this.localConfigurationHistory=[];let s=await this.proposeInstructionCandidates(e,r,t),a=`mipro_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,l={study_name:a,parameters:[{name:"temperature",type:"float",low:.1,high:2},{name:"bootstrappedDemos",type:"int",low:0,high:this.maxBootstrappedDemos},{name:"instruction",type:"categorical",choices:s},{name:"labeledExamples",type:"int",low:0,high:this.maxLabeledDemos},...this.optimizeTopP?[{name:"topP",type:"float",low:.7,high:1}]:[]],objective:{name:"score",direction:"maximize"},n_trials:this.numTrials,sampler:"TPESampler",pruner:this.minibatch?"MedianPruner":void 0},u=await this.pythonClient.createOptimizationJob(l);this.getOptimizerLogger()?.({name:"OptimizationStart",value:{optimizerType:"MiPRO (Python)",exampleCount:t.length,validationCount:0,config:{jobId:u.job_id,numTrials:this.numTrials}}});let p=Number.NEGATIVE_INFINITY,d,m=0,g=0;for(let T=0;T<this.numTrials;T++)try{let v=await this.pythonClient.suggestParameters(a),F=v.params.temperature,E=v.params.bootstrappedDemos,D=v.params.instruction,U=v.params.labeledExamples,V=this.optimizeTopP?v.params.topP:void 0;if(F===void 0)throw new Error(`Missing temperature parameter in suggestion: ${JSON.stringify(v)}`);if(E===void 0)throw new Error(`Missing bootstrappedDemos parameter in suggestion: ${JSON.stringify(v)}`);if(D===void 0)throw new Error(`Missing instruction parameter in suggestion: ${JSON.stringify(v)}`);if(U===void 0)throw new Error(`Missing labeledExamples parameter in suggestion: ${JSON.stringify(v)}`);let k=e.clone();k.setInstruction(D);let $=k.getId();k.setDemos([{traces:this.selectLabeledExamples(t).slice(0,U),programId:$}]);let Q=!this.minibatch||this.minibatchFullEvalSteps>0&&T%this.minibatchFullEvalSteps===this.minibatchFullEvalSteps-1?[...t]:(()=>{let Y=Math.min(this.minibatchSize,t.length),_=new Set;for(;_.size<Y;)_.add(Math.floor(Math.random()*t.length));return Array.from(_).map(X=>t[X])})(),M=await this.evaluateConfiguration(k,n,{temperature:F,bootstrappedDemos:E,topP:V},Q);m++,await this.pythonClient.evaluateTrial({study_name:a,trial_number:v.trial_number,value:M}),M>p+this.minImprovementThreshold?(p=M,d={temperature:F,bootstrappedDemos:E,...V!==void 0?{topP:V}:{},trialNumber:v.trial_number},g=0):g+=1,this.currentRound=T+1;let L={temperature:F,bootstrappedDemos:E,...V!==void 0?{topP:V}:{},trialNumber:v.trial_number};if(this.localScoreHistory.push(M),this.localConfigurationHistory.push(L),await this.updateOptimizationProgress(this.currentRound,M,L,"MiPRO (Python)",{sampler:"TPESampler"},p,d),this.onProgress?.({round:T+1,totalRounds:this.numTrials,currentScore:M,bestScore:p,tokensUsed:this.stats.estimatedTokenUsage,timeElapsed:Date.now()-o,successfulExamples:m,totalExamples:t.length}),this.earlyStoppingTrials>0&&g>=this.earlyStoppingTrials){this.getOptimizerLogger()?.({name:"EarlyStopping",value:{reason:`No improvement \u2265 ${this.minImprovementThreshold} for ${this.earlyStoppingTrials} trials`,finalScore:p,round:this.currentRound}}),this.onEarlyStop?.(`No improvement for ${this.earlyStoppingTrials} trials`,this.stats);break}}catch{}let h=p,f={},b=[];try{let T=await this.pythonClient.getStudyResults(a);if(h=T.best_value||p,f=T.best_params||{},f&&Object.keys(f).length>0){let v=f.bootstrappedDemos||0;v>0&&(b=await this.bootstrapFewShotExamples(e,n,t.slice(0,Math.floor(t.length*.8))),b=b.slice(0,v))}}catch{}let x;try{let v=await We('optimizerType:string "Optimizer name" , bestScore:number "Final best score" , totalCalls:number "Total eval calls" , successfulDemos:number "Successful evals" , bestConfig:json "Best configuration" -> humanExplanation:string "Readable summary", recommendations:string[] "Next steps", performanceAssessment:string "Performance notes"').forward(this.studentAI,{optimizerType:"MiPRO (Python)",bestScore:h,totalCalls:this.stats.totalCalls,successfulDemos:this.stats.successfulDemos,bestConfig:f||{}});x={humanExplanation:v.humanExplanation??"",recommendations:v.recommendations??[],performanceAssessment:v.performanceAssessment??""}}catch{}await this.logOptimizationComplete("MiPRO (Python)",h,f,r,x);try{await this.pythonClient.deleteStudy(a)}catch{}this.stats.bestScore=h;let A=new ve(e.getSignature());f.instruction&&A.setInstruction(f.instruction);let w=f.temperature?{temperature:f.temperature}:void 0;A.setDemos(b,{modelConfig:w});let I=new It({bestScore:h,stats:this.stats,instruction:f.instruction,demos:b,examples:[],modelConfig:{temperature:f.temperature},optimizerType:"MiPRO (Python)",optimizationTime:Date.now()-o,totalRounds:this.numTrials,converged:this.stats.convergenceInfo.converged,scoreHistory:[...this.localScoreHistory],configurationHistory:[...this.localConfigurationHistory]});return this.generateOptimizationReport(h,b.length),{bestScore:h,demos:b,stats:this.stats,optimizedGen:A,optimizedProgram:I,finalConfiguration:{temperature:f.temperature,bootstrappedDemos:f.bootstrappedDemos||0,...f}}}generateOptimizationReport(e,t){console.log(`
614
+ Instruction:`;try{let m=(await ze('programSummary?:string "Program context" , datasetSummary?:string "Dataset context" , tip?:string "Generation tip" -> instructionText:string "Well-crafted instruction for the program"').forward(n,{programSummary:r??"",datasetSummary:o??"",tip:e??""})).instructionText;if(m&&m.trim().length>10)return m.trim()}catch{}let u=["Analyze the input systematically and provide a precise, well-reasoned response.","Think through this step-by-step, considering all relevant factors before responding.","Examine the input carefully and generate an accurate, detailed answer.","Process the information methodically and deliver a clear, comprehensive response.","Consider the context thoroughly and provide a thoughtful, accurate answer."],c=u[t%u.length]||u[0];return e&&(c=`${c} ${e}`),c}async proposeInstructionCandidates(e,t,n=[]){let r=[],o=this.getTeacherOrStudentAI(t),s,a;this.programAwareProposer&&(s=await this.generateProgramSummary(e,o)),this.dataAwareProposer&&(a=await this.generateDatasetSummary([...n],o));let l=this.tipAwareProposer?this.generateTips():[];for(let u=0;u<this.numCandidates;u++){let c=l.length>0?u%l.length:-1,p=c>=0?l[c]:void 0,d=await this.generateInstruction({tip:p,candidateIndex:u,ai:o,programSummary:s,datasetSummary:a,previousInstructions:r});r.push(d)}return r}async bootstrapFewShotExamples(e,t,n){return(await new Nn({studentAI:this.studentAI,options:{maxDemos:this.maxBootstrappedDemos,maxRounds:3,verboseMode:this.verbose??!1}}).compile(e,n,t,{maxDemos:this.maxBootstrappedDemos})).demos||[]}selectLabeledExamples(e){let t=[],n=new Set;for(;n.size<this.maxLabeledDemos&&n.size<e.length;){let r=Math.floor(Math.random()*e.length);if(!n.has(r)){n.add(r);let o=e[r];o&&t.push(o)}}return t}applyConfigToProgram(e,t,n,r){if(e.setInstruction&&e.setInstruction(t.instruction),e.setDemos){let o=[...n.slice(0,t.bootstrappedDemos)];if(t.labeledExamples>0&&e.getId){let s=e.getId();o.push({traces:r.slice(0,t.labeledExamples),programId:s})}o.length>0&&e.setDemos(o)}}async compile(e,t,n,r){let o=Date.now();if(this.validateExamples(t),this.setupRandomSeed(),r?.auto&&this.configureAuto(r.auto),!this.pythonClient)throw new Error("AxMiPRO v2 requires the Python optimizer service. Please configure optimizerEndpoint.");if(!await this.pythonClient.healthCheck())throw new Error("Python optimizer service is not available or unhealthy");return await this.compilePython(e,t,n,r)}applyConfigToAxGen(e,t,n,r){"setInstruction"in e&&typeof e.setInstruction=="function"&&e.setInstruction(t.instruction);let o=[...n.slice(0,t.bootstrappedDemos)];if(t.labeledExamples>0){let s=e.getId();o.push({traces:r.slice(0,t.labeledExamples),programId:s})}o.length>0&&e.setDemos(o)}getConfiguration(){return{numCandidates:this.numCandidates,initTemperature:this.initTemperature,maxBootstrappedDemos:this.maxBootstrappedDemos,maxLabeledDemos:this.maxLabeledDemos,numTrials:this.numTrials,minibatch:this.minibatch,minibatchSize:this.minibatchSize,minibatchFullEvalSteps:this.minibatchFullEvalSteps,programAwareProposer:this.programAwareProposer,dataAwareProposer:this.dataAwareProposer,tipAwareProposer:this.tipAwareProposer,fewshotAwareProposer:this.fewshotAwareProposer,earlyStoppingTrials:this.earlyStoppingTrials,minImprovementThreshold:this.minImprovementThreshold,bayesianOptimization:this.bayesianOptimization,acquisitionFunction:this.acquisitionFunction,explorationWeight:this.explorationWeight,sampleCount:this.sampleCount}}updateConfiguration(e){e.numCandidates!==void 0&&(this.numCandidates=e.numCandidates),e.initTemperature!==void 0&&(this.initTemperature=e.initTemperature),e.maxBootstrappedDemos!==void 0&&(this.maxBootstrappedDemos=e.maxBootstrappedDemos),e.maxLabeledDemos!==void 0&&(this.maxLabeledDemos=e.maxLabeledDemos),e.numTrials!==void 0&&(this.numTrials=e.numTrials),e.minibatch!==void 0&&(this.minibatch=e.minibatch),e.minibatchSize!==void 0&&(this.minibatchSize=e.minibatchSize),e.earlyStoppingTrials!==void 0&&(this.earlyStoppingTrials=e.earlyStoppingTrials),e.minImprovementThreshold!==void 0&&(this.minImprovementThreshold=e.minImprovementThreshold),e.sampleCount!==void 0&&(this.sampleCount=e.sampleCount)}reset(){super.reset(),this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold}validateProgram(e){let t=[],n=[];return{isValid:t.length===0,issues:t,suggestions:n}}async compilePython(e,t,n,r){if(!this.pythonClient)throw new Error("Python client not initialized");let o=Date.now();this.localScoreHistory=[],this.localConfigurationHistory=[];let s=await this.proposeInstructionCandidates(e,r,t),a=`mipro_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,l={study_name:a,parameters:[{name:"temperature",type:"float",low:.1,high:2},{name:"bootstrappedDemos",type:"int",low:0,high:this.maxBootstrappedDemos},{name:"instruction",type:"categorical",choices:s},{name:"labeledExamples",type:"int",low:0,high:this.maxLabeledDemos},...this.optimizeTopP?[{name:"topP",type:"float",low:.7,high:1}]:[]],objective:{name:"score",direction:"maximize"},n_trials:this.numTrials,sampler:"TPESampler",pruner:this.minibatch?"MedianPruner":void 0},u=await this.pythonClient.createOptimizationJob(l);this.getOptimizerLogger()?.({name:"OptimizationStart",value:{optimizerType:"MiPRO (Python)",exampleCount:t.length,validationCount:0,config:{jobId:u.job_id,numTrials:this.numTrials}}});let p=Number.NEGATIVE_INFINITY,d,m=0,g=0;for(let T=0;T<this.numTrials;T++)try{let v=await this.pythonClient.suggestParameters(a),F=v.params.temperature,O=v.params.bootstrappedDemos,D=v.params.instruction,B=v.params.labeledExamples,Q=this.optimizeTopP?v.params.topP:void 0;if(F===void 0)throw new Error(`Missing temperature parameter in suggestion: ${JSON.stringify(v)}`);if(O===void 0)throw new Error(`Missing bootstrappedDemos parameter in suggestion: ${JSON.stringify(v)}`);if(D===void 0)throw new Error(`Missing instruction parameter in suggestion: ${JSON.stringify(v)}`);if(B===void 0)throw new Error(`Missing labeledExamples parameter in suggestion: ${JSON.stringify(v)}`);let S=e.clone();S.setInstruction(D);let G=S.getId();S.setDemos([{traces:this.selectLabeledExamples(t).slice(0,B),programId:G}]);let Y=!this.minibatch||this.minibatchFullEvalSteps>0&&T%this.minibatchFullEvalSteps===this.minibatchFullEvalSteps-1?[...t]:(()=>{let X=Math.min(this.minibatchSize,t.length),_=new Set;for(;_.size<X;)_.add(Math.floor(Math.random()*t.length));return Array.from(_).map(Z=>t[Z])})(),M=await this.evaluateConfiguration(S,n,{temperature:F,bootstrappedDemos:O,topP:Q},Y);m++,await this.pythonClient.evaluateTrial({study_name:a,trial_number:v.trial_number,value:M}),M>p+this.minImprovementThreshold?(p=M,d={temperature:F,bootstrappedDemos:O,...Q!==void 0?{topP:Q}:{},trialNumber:v.trial_number},g=0):g+=1,this.currentRound=T+1;let L={temperature:F,bootstrappedDemos:O,...Q!==void 0?{topP:Q}:{},trialNumber:v.trial_number};if(this.localScoreHistory.push(M),this.localConfigurationHistory.push(L),await this.updateOptimizationProgress(this.currentRound,M,L,"MiPRO (Python)",{sampler:"TPESampler"},p,d),this.onProgress?.({round:T+1,totalRounds:this.numTrials,currentScore:M,bestScore:p,tokensUsed:this.stats.estimatedTokenUsage,timeElapsed:Date.now()-o,successfulExamples:m,totalExamples:t.length}),this.earlyStoppingTrials>0&&g>=this.earlyStoppingTrials){this.getOptimizerLogger()?.({name:"EarlyStopping",value:{reason:`No improvement \u2265 ${this.minImprovementThreshold} for ${this.earlyStoppingTrials} trials`,finalScore:p,round:this.currentRound}}),this.onEarlyStop?.(`No improvement for ${this.earlyStoppingTrials} trials`,this.stats);break}}catch{}let h=p,f={},I=[];try{let T=await this.pythonClient.getStudyResults(a);if(h=T.best_value||p,f=T.best_params||{},f&&Object.keys(f).length>0){let v=f.bootstrappedDemos||0;v>0&&(I=await this.bootstrapFewShotExamples(e,n,t.slice(0,Math.floor(t.length*.8))),I=I.slice(0,v))}}catch{}let x;try{let v=await ze('optimizerType:string "Optimizer name" , bestScore:number "Final best score" , totalCalls:number "Total eval calls" , successfulDemos:number "Successful evals" , bestConfig:json "Best configuration" -> humanExplanation:string "Readable summary", recommendations:string[] "Next steps", performanceAssessment:string "Performance notes"').forward(this.studentAI,{optimizerType:"MiPRO (Python)",bestScore:h,totalCalls:this.stats.totalCalls,successfulDemos:this.stats.successfulDemos,bestConfig:f||{}});x={humanExplanation:v.humanExplanation??"",recommendations:v.recommendations??[],performanceAssessment:v.performanceAssessment??""}}catch{}await this.logOptimizationComplete("MiPRO (Python)",h,f,r,x);try{await this.pythonClient.deleteStudy(a)}catch{}this.stats.bestScore=h;let A=new Se(e.getSignature());f.instruction&&A.setInstruction(f.instruction);let w=f.temperature?{temperature:f.temperature}:void 0;A.setDemos(I,{modelConfig:w});let b=new Ct({bestScore:h,stats:this.stats,instruction:f.instruction,demos:I,examples:[],modelConfig:{temperature:f.temperature},optimizerType:"MiPRO (Python)",optimizationTime:Date.now()-o,totalRounds:this.numTrials,converged:this.stats.convergenceInfo.converged,scoreHistory:[...this.localScoreHistory],configurationHistory:[...this.localConfigurationHistory]});return this.generateOptimizationReport(h,I.length),{bestScore:h,demos:I,stats:this.stats,optimizedGen:A,optimizedProgram:b,finalConfiguration:{temperature:f.temperature,bootstrappedDemos:f.bootstrappedDemos||0,...f}}}generateOptimizationReport(e,t){console.log(`
615
615
  \u{1F389} MiPRO Optimization Complete!
616
616
  `),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
617
617
  `),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
618
618
  `),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
619
- `),console.log("\u{1F4A1} What This Means:"),console.log("\u2022 MiPRO successfully automated prompt engineering"),console.log("\u2022 Optimized instructions and examples improve model performance"),console.log("\u2022 Framework reduces manual prompt engineering effort"),console.log("\u2022 More training data and iterations would likely improve results")}async evaluateConfiguration(e,t,n,r){let o=0,s=0,a=0,l=r;for(let u of l)try{let c=await e.forward(this.studentAI,u,{modelConfig:{temperature:n.temperature,...n.topP!==void 0?{topP:n.topP}:{}},sampleCount:this.sampleCount,resultPicker:this.sampleCount>1?this.customResultPicker??this.defaultResultPicker:void 0});this.stats.totalCalls+=1;let p=await t({prediction:c,example:u});if(typeof p=="number"&&!Number.isNaN(p)){o+=p,s++;let d=typeof this.targetScore=="number"?this.targetScore:.5;p>=d&&a++}}catch(c){this.getLogger()?.({name:"Notification",id:"mipro_evaluate",value:typeof c=="string"?c:String(c)})}return this.stats.successfulDemos+=a,s>0?o/s:0}};var $n=class{analyzeMappingDependencies(e,t){if(!e||typeof e!="function")return[];let n=[];try{let r=e.toString(),o=Array.from(r.matchAll(/state\.(\w+)/g));for(let s of o)s[1]&&!n.includes(s[1])&&n.push(s[1]);if(n.length===0)try{let s=this.createDependencyTracker(n);e(s)}catch{}}catch(r){console.debug("Dependency analysis failed:",r)}return n}createTrackingProxy(e,t){let n=this;return new Proxy(e,{get(r,o){typeof o=="string"&&!t.includes(o)&&t.push(o);let s=r[o];return s&&typeof s=="object"?n.createTrackingProxy(s,t):s},has(r,o){return typeof o=="string"&&!t.includes(o)&&t.push(o),o in r}})}parseStaticDependencies(e){let t=[];try{let n=Array.from(e.matchAll(/state\.(\w+)/g));for(let s of n)s[1]&&!t.includes(s[1])&&t.push(s[1]);let r=Array.from(e.matchAll(/\$\{state\.(\w+)\}/g));for(let s of r)s[1]&&!t.includes(s[1])&&t.push(s[1]);let o=Array.from(e.matchAll(/\{\s*(\w+)(?:\s*,\s*(\w+))*\s*\}\s*=\s*state/g));for(let s of o)for(let a=1;a<s.length;a++)s[a]&&!t.includes(s[a])&&t.push(s[a])}catch(n){console.debug("Static dependency parsing failed:",n)}return t}createDependencyTracker(e){return new Proxy({},{get(t,n){return typeof n=="string"&&!e.includes(n)&&e.push(n),new Proxy({},{get:()=>{}})}})}};async function Gn(i,e,t){if(!t||t<=0||t>=i.length){let r=i.map((o,s)=>e(o,s));return Promise.all(r)}let n=new Array(i.length);for(let r=0;r<i.length;r+=t){let s=i.slice(r,r+t).map((l,u)=>{let c=r+u;return e(l,c).then(p=>({result:p,originalIndex:c}))}),a=await Promise.all(s);for(let{result:l,originalIndex:u}of a)n[u]=l}return n}var Dn=class{steps=[];parallelGroups=[];analyzer=new $n;initialFields=new Set;addExecutionStep(e,t,n,r,o,s,a){let l=[],u=[],c=r||"map";if(t&&n)c="execute",l=this.analyzer.analyzeMappingDependencies(n,t),u=[`${t}Result`];else if(c==="map"&&o)u=this.analyzeMapTransformation(o),l=this.getAllProducedFields();else if(c==="parallel-map"){if(Array.isArray(o)){let d=new Set;for(let m of o)this.analyzeMapTransformation(m).forEach(h=>d.add(h));u=Array.from(d)}else o?u=this.analyzeMapTransformation(o):u=["_parallelMapResult"];l=this.getAllProducedFields()}else if(c==="merge"){if(s?.resultKey)u=[s.resultKey];else{let m=this.analyzeBranchMergeFields();u=m.length>0?m:["_mergedResult"]}e.toString().includes("_parallelResults")?l=["_parallelResults"]:l=this.getAllProducedFields()}else if(c==="parallel")u=["_parallelResults"],l=this.getAllProducedFields();else if(c==="derive")if(a?.outputFieldName&&a?.inputFieldName){u=[a.outputFieldName];let d=o?this.analyzer.analyzeMappingDependencies(o,"derive"):[];l=[a.inputFieldName,...d].filter((m,g,h)=>h.indexOf(m)===g)}else u=["_deriveResult"],l=this.getAllProducedFields();else e.toString().includes("transform(")?(c="map",l=this.getAllProducedFields(),u=["_mapResult"]):e.toString().includes("_parallelResults")&&(u=["_parallelResults"],l=this.getAllProducedFields());for(let d of l)this.getAllProducedFields().includes(d)||this.initialFields.add(d);let p={type:c,nodeName:t,dependencies:l,produces:u,stepFunction:e,stepIndex:this.steps.length};this.steps.push(p)}analyzeStepFunctionProduction(e){try{let t=this.analyzeStepFunctionSource(e);if(t.length>0&&!t.includes("_stepResult"))return t}catch(t){console.debug("Step function source analysis failed:",t)}try{let t=this.createMockState(),n=Object.keys(t),o=e(t,{mainAi:{getOptions:()=>({trace:!1}),forward:()=>Promise.resolve({text:"mock"})},mainOptions:void 0});if(o&&typeof o=="object"&&"then"in o)return this.analyzeStepFunctionSource(e);if(o&&typeof o=="object"&&!Array.isArray(o)){let a=Object.keys(o).filter(l=>!n.includes(l));if(a.length>0)return a}}catch(t){console.debug("Step function dynamic analysis failed:",t)}return this.analyzeStepFunctionSource(e)}analyzeStepFunctionSource(e){try{let t=e.toString(),n=t.match(/\{\s*\.\.\.state\s*,\s*(\w+)\s*:/g);if(n){let o=n.map(s=>{let a=s.match(/(\w+)\s*:/);return a?a[1]:null}).filter(Boolean);if(o.length>0)return o}let r=t.match(/state\.(\w+)\s*=/g);if(r){let o=r.map(s=>{let a=s.match(/state\.(\w+)\s*=/);return a?a[1]:null}).filter(Boolean);if(o.length>0)return o}}catch(t){console.debug("Step function source analysis failed:",t)}return["_stepResult"]}analyzeMapTransformation(e){try{let n=e.toString().split(/\{[\s\S]*?\}/).flatMap(()=>[])}catch{}return["_mapResult"]}createMockState(){let e={};for(let t of this.initialFields)e[t]=this.createMockValue(t);for(let t of this.steps)for(let n of t.produces)n.endsWith("Result")?e[n]={text:"mockText",value:"mockValue",result:"mockResult",data:"mockData",processedText:"mockProcessedText",sentimentValue:"mockSentiment",confidenceScore:.8,isComplex:!1,mockValue:"mockValue",responseText:"mockResponseText",inputText:"mockInputText"}:e[n]=this.createMockValue(n);return e}createMockValue(e){return e.includes("List")||e.includes("Array")||e.endsWith("s")?["mockItem1","mockItem2"]:e.includes("count")||e.includes("Count")||e.includes("index")||e.includes("Index")?0:e.includes("is")||e.includes("has")||e.includes("can")?!1:"mockValue"}analyzeBranchMergeFields(){let e=this.steps.slice(-5).filter(t=>t.type==="execute"&&t.nodeName).flatMap(t=>t.produces);return e.length>0?e:this.steps.filter(t=>t.type==="execute"&&t.nodeName).flatMap(t=>t.produces)}setInitialFields(e){this.initialFields=new Set(e),this.rebuildParallelGroups()}rebuildParallelGroups(){this.parallelGroups=[];let e=new Set,t=new Set(this.initialFields),n=0;for(;e.size<this.steps.length;){let r=[];for(let o of this.steps){if(e.has(o.stepIndex))continue;if(o.dependencies.length===0||o.dependencies.every(a=>t.has(a))){if(o.type==="merge"&&r.length>0)continue;if(r.push(o),e.add(o.stepIndex),o.type==="merge")break}}if(r.length>0){for(let o of r)o.produces.forEach(s=>t.add(s));this.parallelGroups.push({level:n,steps:r}),n++}else{let o=this.steps.filter(s=>!e.has(s.stepIndex));if(o.length>0){let s=o[0];e.add(s.stepIndex),s.produces.forEach(a=>t.add(a)),this.parallelGroups.push({level:n,steps:[s]}),n++}else break}}}getAllProducedFields(){let e=[];for(let t of this.steps)e.push(...t.produces);return e}createOptimizedExecution(e){let t=[];for(let n of this.parallelGroups)if(n.steps.length===1){let r=n.steps[0];r&&t.push(r.stepFunction)}else if(n.steps.length>1){let r=async(o,s)=>{let a=await Gn(n.steps,async c=>await c.stepFunction(o,s),e);if(a.some(c=>c&&typeof c=="object"&&"_parallelResults"in c)){let c=a.find(p=>p&&typeof p=="object"&&"_parallelResults"in p);return c||o}let u=o;for(let c of a)u={...u,...c};return u};t.push(r)}return t}getOptimizedExecutionSteps(){return this.parallelGroups.length===0&&this.steps.length>0&&this.rebuildParallelGroups(),this.createOptimizedExecution()}getExecutionPlan(){return this.parallelGroups.length===0&&this.steps.length>0&&this.rebuildParallelGroups(),{totalSteps:this.steps.length,parallelGroups:this.parallelGroups.length,maxParallelism:this.steps.length===0?1:Math.max(...this.parallelGroups.map(e=>e.steps.length),0),steps:this.steps,groups:this.parallelGroups}}};var qn=require("@opentelemetry/api");var uu=i=>{console.log(i)},Un=(i,e=!1)=>{if(e)return"[State hidden]";let t={};for(let[n,r]of Object.entries(i))if(typeof r=="string"&&r.length>100)t[n]=`${r.substring(0,100)}...`;else if(Array.isArray(r)&&r.length>3)t[n]=[...r.slice(0,3),`... (${r.length-3} more)`];else if(typeof r=="object"&&r!==null){let o=JSON.stringify(r);o.length>200?t[n]=`${o.substring(0,200)}...`:t[n]=r}else t[n]=r;return JSON.stringify(t,null,2)},Bn=i=>i<1e3?`${i.toFixed(1)}ms`:i<6e4?`${(i/1e3).toFixed(2)}s`:`${(i/6e4).toFixed(2)}min`,Mr=(i=uu)=>{let e=new Ye,t=e.gray(`${"\u2501".repeat(80)}
619
+ `),console.log("\u{1F4A1} What This Means:"),console.log("\u2022 MiPRO successfully automated prompt engineering"),console.log("\u2022 Optimized instructions and examples improve model performance"),console.log("\u2022 Framework reduces manual prompt engineering effort"),console.log("\u2022 More training data and iterations would likely improve results")}async evaluateConfiguration(e,t,n,r){let o=0,s=0,a=0,l=r;for(let u of l)try{let c=await e.forward(this.studentAI,u,{modelConfig:{temperature:n.temperature,...n.topP!==void 0?{topP:n.topP}:{}},sampleCount:this.sampleCount,resultPicker:this.sampleCount>1?this.customResultPicker??this.defaultResultPicker:void 0});this.stats.totalCalls+=1;let p=await t({prediction:c,example:u});if(typeof p=="number"&&!Number.isNaN(p)){o+=p,s++;let d=typeof this.targetScore=="number"?this.targetScore:.5;p>=d&&a++}}catch(c){this.getLogger()?.({name:"Notification",id:"mipro_evaluate",value:typeof c=="string"?c:String(c)})}return this.stats.successfulDemos+=a,s>0?o/s:0}};var Ln=class{analyzeMappingDependencies(e,t){if(!e||typeof e!="function")return[];let n=[];try{let r=e.toString(),o=Array.from(r.matchAll(/state\.(\w+)/g));for(let s of o)s[1]&&!n.includes(s[1])&&n.push(s[1]);if(n.length===0)try{let s=this.createDependencyTracker(n);e(s)}catch{}}catch(r){console.debug("Dependency analysis failed:",r)}return n}createTrackingProxy(e,t){let n=this;return new Proxy(e,{get(r,o){typeof o=="string"&&!t.includes(o)&&t.push(o);let s=r[o];return s&&typeof s=="object"?n.createTrackingProxy(s,t):s},has(r,o){return typeof o=="string"&&!t.includes(o)&&t.push(o),o in r}})}parseStaticDependencies(e){let t=[];try{let n=Array.from(e.matchAll(/state\.(\w+)/g));for(let s of n)s[1]&&!t.includes(s[1])&&t.push(s[1]);let r=Array.from(e.matchAll(/\$\{state\.(\w+)\}/g));for(let s of r)s[1]&&!t.includes(s[1])&&t.push(s[1]);let o=Array.from(e.matchAll(/\{\s*(\w+)(?:\s*,\s*(\w+))*\s*\}\s*=\s*state/g));for(let s of o)for(let a=1;a<s.length;a++)s[a]&&!t.includes(s[a])&&t.push(s[a])}catch(n){console.debug("Static dependency parsing failed:",n)}return t}createDependencyTracker(e){return new Proxy({},{get(t,n){return typeof n=="string"&&!e.includes(n)&&e.push(n),new Proxy({},{get:()=>{}})}})}};async function $n(i,e,t){if(!t||t<=0||t>=i.length){let r=i.map((o,s)=>e(o,s));return Promise.all(r)}let n=new Array(i.length);for(let r=0;r<i.length;r+=t){let s=i.slice(r,r+t).map((l,u)=>{let c=r+u;return e(l,c).then(p=>({result:p,originalIndex:c}))}),a=await Promise.all(s);for(let{result:l,originalIndex:u}of a)n[u]=l}return n}var Gn=class{steps=[];parallelGroups=[];analyzer=new Ln;initialFields=new Set;addExecutionStep(e,t,n,r,o,s,a){let l=[],u=[],c=r||"map";if(t&&n)c="execute",l=this.analyzer.analyzeMappingDependencies(n,t),u=[`${t}Result`];else if(c==="map"&&o)u=this.analyzeMapTransformation(o),l=this.getAllProducedFields();else if(c==="parallel-map"){if(Array.isArray(o)){let d=new Set;for(let m of o)this.analyzeMapTransformation(m).forEach(h=>d.add(h));u=Array.from(d)}else o?u=this.analyzeMapTransformation(o):u=["_parallelMapResult"];l=this.getAllProducedFields()}else if(c==="merge"){if(s?.resultKey)u=[s.resultKey];else{let m=this.analyzeBranchMergeFields();u=m.length>0?m:["_mergedResult"]}e.toString().includes("_parallelResults")?l=["_parallelResults"]:l=this.getAllProducedFields()}else if(c==="parallel")u=["_parallelResults"],l=this.getAllProducedFields();else if(c==="derive")if(a?.outputFieldName&&a?.inputFieldName){u=[a.outputFieldName];let d=o?this.analyzer.analyzeMappingDependencies(o,"derive"):[];l=[a.inputFieldName,...d].filter((m,g,h)=>h.indexOf(m)===g)}else u=["_deriveResult"],l=this.getAllProducedFields();else e.toString().includes("transform(")?(c="map",l=this.getAllProducedFields(),u=["_mapResult"]):e.toString().includes("_parallelResults")&&(u=["_parallelResults"],l=this.getAllProducedFields());for(let d of l)this.getAllProducedFields().includes(d)||this.initialFields.add(d);let p={type:c,nodeName:t,dependencies:l,produces:u,stepFunction:e,stepIndex:this.steps.length};this.steps.push(p)}analyzeStepFunctionProduction(e){try{let t=this.analyzeStepFunctionSource(e);if(t.length>0&&!t.includes("_stepResult"))return t}catch(t){console.debug("Step function source analysis failed:",t)}try{let t=this.createMockState(),n=Object.keys(t),o=e(t,{mainAi:{getOptions:()=>({trace:!1}),forward:()=>Promise.resolve({text:"mock"})},mainOptions:void 0});if(o&&typeof o=="object"&&"then"in o)return this.analyzeStepFunctionSource(e);if(o&&typeof o=="object"&&!Array.isArray(o)){let a=Object.keys(o).filter(l=>!n.includes(l));if(a.length>0)return a}}catch(t){console.debug("Step function dynamic analysis failed:",t)}return this.analyzeStepFunctionSource(e)}analyzeStepFunctionSource(e){try{let t=e.toString(),n=t.match(/\{\s*\.\.\.state\s*,\s*(\w+)\s*:/g);if(n){let o=n.map(s=>{let a=s.match(/(\w+)\s*:/);return a?a[1]:null}).filter(Boolean);if(o.length>0)return o}let r=t.match(/state\.(\w+)\s*=/g);if(r){let o=r.map(s=>{let a=s.match(/state\.(\w+)\s*=/);return a?a[1]:null}).filter(Boolean);if(o.length>0)return o}}catch(t){console.debug("Step function source analysis failed:",t)}return["_stepResult"]}analyzeMapTransformation(e){try{let n=e.toString().split(/\{[\s\S]*?\}/).flatMap(()=>[])}catch{}return["_mapResult"]}createMockState(){let e={};for(let t of this.initialFields)e[t]=this.createMockValue(t);for(let t of this.steps)for(let n of t.produces)n.endsWith("Result")?e[n]={text:"mockText",value:"mockValue",result:"mockResult",data:"mockData",processedText:"mockProcessedText",sentimentValue:"mockSentiment",confidenceScore:.8,isComplex:!1,mockValue:"mockValue",responseText:"mockResponseText",inputText:"mockInputText"}:e[n]=this.createMockValue(n);return e}createMockValue(e){return e.includes("List")||e.includes("Array")||e.endsWith("s")?["mockItem1","mockItem2"]:e.includes("count")||e.includes("Count")||e.includes("index")||e.includes("Index")?0:e.includes("is")||e.includes("has")||e.includes("can")?!1:"mockValue"}analyzeBranchMergeFields(){let e=this.steps.slice(-5).filter(t=>t.type==="execute"&&t.nodeName).flatMap(t=>t.produces);return e.length>0?e:this.steps.filter(t=>t.type==="execute"&&t.nodeName).flatMap(t=>t.produces)}setInitialFields(e){this.initialFields=new Set(e),this.rebuildParallelGroups()}rebuildParallelGroups(){this.parallelGroups=[];let e=new Set,t=new Set(this.initialFields),n=0;for(;e.size<this.steps.length;){let r=[];for(let o of this.steps){if(e.has(o.stepIndex))continue;if(o.dependencies.length===0||o.dependencies.every(a=>t.has(a))){if(o.type==="merge"&&r.length>0)continue;if(r.push(o),e.add(o.stepIndex),o.type==="merge")break}}if(r.length>0){for(let o of r)o.produces.forEach(s=>t.add(s));this.parallelGroups.push({level:n,steps:r}),n++}else{let o=this.steps.filter(s=>!e.has(s.stepIndex));if(o.length>0){let s=o[0];e.add(s.stepIndex),s.produces.forEach(a=>t.add(a)),this.parallelGroups.push({level:n,steps:[s]}),n++}else break}}}getAllProducedFields(){let e=[];for(let t of this.steps)e.push(...t.produces);return e}createOptimizedExecution(e){let t=[];for(let n of this.parallelGroups)if(n.steps.length===1){let r=n.steps[0];r&&t.push(r.stepFunction)}else if(n.steps.length>1){let r=async(o,s)=>{let a=await $n(n.steps,async c=>await c.stepFunction(o,s),e);if(a.some(c=>c&&typeof c=="object"&&"_parallelResults"in c)){let c=a.find(p=>p&&typeof p=="object"&&"_parallelResults"in p);return c||o}let u=o;for(let c of a)u={...u,...c};return u};t.push(r)}return t}getOptimizedExecutionSteps(){return this.parallelGroups.length===0&&this.steps.length>0&&this.rebuildParallelGroups(),this.createOptimizedExecution()}getExecutionPlan(){return this.parallelGroups.length===0&&this.steps.length>0&&this.rebuildParallelGroups(),{totalSteps:this.steps.length,parallelGroups:this.parallelGroups.length,maxParallelism:this.steps.length===0?1:Math.max(...this.parallelGroups.map(e=>e.steps.length),0),steps:this.steps,groups:this.parallelGroups}}};var jn=require("@opentelemetry/api");var uu=i=>{console.log(i)},Dn=(i,e=!1)=>{if(e)return"[State hidden]";let t={};for(let[n,r]of Object.entries(i))if(typeof r=="string"&&r.length>100)t[n]=`${r.substring(0,100)}...`;else if(Array.isArray(r)&&r.length>3)t[n]=[...r.slice(0,3),`... (${r.length-3} more)`];else if(typeof r=="object"&&r!==null){let o=JSON.stringify(r);o.length>200?t[n]=`${o.substring(0,200)}...`:t[n]=r}else t[n]=r;return JSON.stringify(t,null,2)},Un=i=>i<1e3?`${i.toFixed(1)}ms`:i<6e4?`${(i/1e3).toFixed(2)}s`:`${(i/6e4).toFixed(2)}min`,Or=(i=uu)=>{let e=new Je,t=e.gray(`${"\u2501".repeat(80)}
620
620
  `),n=e.gray(`${"\u2500".repeat(40)}
621
621
  `);return r=>{let o="";switch(r.name){case"FlowStart":o=`
622
622
  ${e.blueBright("\u{1F504} [ AXFLOW START ]")}
@@ -628,13 +628,13 @@ ${t}`,o+=`${e.white("Input Fields:")} ${e.cyan(r.inputFields.join(", "))}
628
628
  `,o+=t;break;case"StepStart":{let s=r.stepType==="execute"?"\u26A1":r.stepType==="map"?"\u{1F504}":r.stepType==="merge"?"\u{1F500}":r.stepType==="parallel"?"\u2696\uFE0F":"\u{1F4CB}";o=`${e.greenBright(`${s} [ STEP ${r.stepIndex} START ]`)} ${e.white(`(${r.stepType})`)}`,r.nodeName&&(o+=` ${e.cyanBright(`Node: ${r.nodeName}`)}`),o+=`
629
629
  `,r.dependencies.length>0&&(o+=`${e.white("Dependencies:")} ${e.gray(r.dependencies.join(", "))}
630
630
  `),r.produces.length>0&&(o+=`${e.white("Produces:")} ${e.cyan(r.produces.join(", "))}
631
- `),o+=`${e.white("State:")} ${e.gray(Un(r.state,!0))}
632
- `,o+=n;break}case"StepComplete":{let s=(r.stepType==="execute"||r.stepType==="map"||r.stepType==="merge"||r.stepType==="parallel","\u2705");o=`${e.greenBright(`${s} [ STEP ${r.stepIndex} COMPLETE ]`)} ${e.white(`(${r.stepType})`)}`,r.nodeName&&(o+=` ${e.cyanBright(`Node: ${r.nodeName}`)}`),o+=` ${e.magenta(`in ${Bn(r.executionTime)}`)}
631
+ `),o+=`${e.white("State:")} ${e.gray(Dn(r.state,!0))}
632
+ `,o+=n;break}case"StepComplete":{let s=(r.stepType==="execute"||r.stepType==="map"||r.stepType==="merge"||r.stepType==="parallel","\u2705");o=`${e.greenBright(`${s} [ STEP ${r.stepIndex} COMPLETE ]`)} ${e.white(`(${r.stepType})`)}`,r.nodeName&&(o+=` ${e.cyanBright(`Node: ${r.nodeName}`)}`),o+=` ${e.magenta(`in ${Un(r.executionTime)}`)}
633
633
  `,r.newFields&&r.newFields.length>0&&(o+=`${e.white("New Fields:")} ${e.green(r.newFields.join(", "))}
634
634
  `),r.result&&r.nodeName&&(o+=`${e.white("Result:")} ${e.yellow(JSON.stringify(r.result,null,2))}
635
635
  `),o+=n;break}case"ParallelGroupStart":o=`${e.blueBright("\u2696\uFE0F [ PARALLEL GROUP START ]")} ${e.white(`Level ${r.groupLevel}`)}
636
636
  `,o+=`${e.white("Steps:")} ${e.yellow(r.stepsCount.toString())} ${e.gray(`(${r.stepTypes.join(", ")})`)}
637
- `,o+=n;break;case"ParallelGroupComplete":o=`${e.blueBright("\u2705 [ PARALLEL GROUP COMPLETE ]")} ${e.white(`Level ${r.groupLevel}`)}`,o+=` ${e.magenta(`in ${Bn(r.executionTime)}`)}
637
+ `,o+=n;break;case"ParallelGroupComplete":o=`${e.blueBright("\u2705 [ PARALLEL GROUP COMPLETE ]")} ${e.white(`Level ${r.groupLevel}`)}`,o+=` ${e.magenta(`in ${Un(r.executionTime)}`)}
638
638
  `,o+=`${e.white("Steps Executed:")} ${e.yellow(r.stepsCount.toString())}
639
639
  `,o+=n;break;case"BranchEvaluation":o=`${e.yellow("\u{1F500} [ BRANCH EVALUATION ]")}
640
640
  `,o+=`${e.white("Branch Value:")} ${e.cyan(JSON.stringify(r.branchValue))}
@@ -642,15 +642,15 @@ ${t}`,o+=`${e.white("Input Fields:")} ${e.cyan(r.inputFields.join(", "))}
642
642
  `,r.hasMatchingBranch&&(o+=`${e.white("Branch Steps:")} ${e.yellow(r.branchStepsCount.toString())}
643
643
  `),o+=n;break;case"FlowComplete":o=`
644
644
  ${e.greenBright("\u2705 [ AXFLOW COMPLETE ]")}
645
- ${t}`,o+=`${e.white("Total Time:")} ${e.magenta(Bn(r.totalExecutionTime))}
645
+ ${t}`,o+=`${e.white("Total Time:")} ${e.magenta(Un(r.totalExecutionTime))}
646
646
  `,o+=`${e.white("Steps Executed:")} ${e.yellow(r.stepsExecuted.toString())}
647
647
  `,o+=`${e.white("Output Fields:")} ${e.green(r.outputFields.join(", "))}
648
- `,o+=`${e.white("Final State:")} ${e.gray(Un(r.finalState,!0))}
648
+ `,o+=`${e.white("Final State:")} ${e.gray(Dn(r.finalState,!0))}
649
649
  `,o+=t;break;case"FlowError":o=`
650
650
  ${e.redBright("\u274C [ AXFLOW ERROR ]")}
651
651
  ${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+=`
652
652
  `),o+=`${e.white("Error:")} ${e.red(r.error)}
653
- `,r.state&&(o+=`${e.white("State:")} ${e.gray(Un(r.state,!0))}
653
+ `,r.state&&(o+=`${e.white("State:")} ${e.gray(Dn(r.state,!0))}
654
654
  `),o+=t;break;default:o=e.gray(JSON.stringify(r,null,2))}i(o)}},cu=(i=uu)=>{let e="=".repeat(80),t="-".repeat(40);return n=>{let r="";switch(n.name){case"FlowStart":r=`
655
655
  [ AXFLOW START ]
656
656
  ${e}
@@ -663,16 +663,16 @@ ${e}
663
663
  `;break;case"StepStart":r=`[ STEP ${n.stepIndex} START ] (${n.stepType})`,n.nodeName&&(r+=` Node: ${n.nodeName}`),r+=`
664
664
  `,n.dependencies.length>0&&(r+=`Dependencies: ${n.dependencies.join(", ")}
665
665
  `),n.produces.length>0&&(r+=`Produces: ${n.produces.join(", ")}
666
- `),r+=`State: ${Un(n.state,!0)}
666
+ `),r+=`State: ${Dn(n.state,!0)}
667
667
  `,r+=`${t}
668
- `;break;case"StepComplete":r=`[ STEP ${n.stepIndex} COMPLETE ] (${n.stepType})`,n.nodeName&&(r+=` Node: ${n.nodeName}`),r+=` in ${Bn(n.executionTime)}
668
+ `;break;case"StepComplete":r=`[ STEP ${n.stepIndex} COMPLETE ] (${n.stepType})`,n.nodeName&&(r+=` Node: ${n.nodeName}`),r+=` in ${Un(n.executionTime)}
669
669
  `,n.newFields&&n.newFields.length>0&&(r+=`New Fields: ${n.newFields.join(", ")}
670
670
  `),n.result&&n.nodeName&&(r+=`Result: ${JSON.stringify(n.result,null,2)}
671
671
  `),r+=`${t}
672
672
  `;break;case"ParallelGroupStart":r=`[ PARALLEL GROUP START ] Level ${n.groupLevel}
673
673
  `,r+=`Steps: ${n.stepsCount} (${n.stepTypes.join(", ")})
674
674
  `,r+=`${t}
675
- `;break;case"ParallelGroupComplete":r=`[ PARALLEL GROUP COMPLETE ] Level ${n.groupLevel} in ${Bn(n.executionTime)}
675
+ `;break;case"ParallelGroupComplete":r=`[ PARALLEL GROUP COMPLETE ] Level ${n.groupLevel} in ${Un(n.executionTime)}
676
676
  `,r+=`Steps Executed: ${n.stepsCount}
677
677
  `,r+=`${t}
678
678
  `;break;case"BranchEvaluation":r=`[ BRANCH EVALUATION ]
@@ -683,70 +683,71 @@ ${e}
683
683
  `;break;case"FlowComplete":r=`
684
684
  [ AXFLOW COMPLETE ]
685
685
  ${e}
686
- `,r+=`Total Time: ${Bn(n.totalExecutionTime)}
686
+ `,r+=`Total Time: ${Un(n.totalExecutionTime)}
687
687
  `,r+=`Steps Executed: ${n.stepsExecuted}
688
688
  `,r+=`Output Fields: ${n.outputFields.join(", ")}
689
- `,r+=`Final State: ${Un(n.finalState,!0)}
689
+ `,r+=`Final State: ${Dn(n.finalState,!0)}
690
690
  `,r+=`${e}
691
691
  `;break;case"FlowError":r=`
692
692
  [ AXFLOW ERROR ]
693
693
  ${e}
694
694
  `,n.stepIndex!==void 0&&(r+=`Step: ${n.stepIndex}`,n.stepType&&(r+=` (${n.stepType})`),n.nodeName&&(r+=` Node: ${n.nodeName}`),r+=`
695
695
  `),r+=`Error: ${n.error}
696
- `,n.state&&(r+=`State: ${Un(n.state,!0)}
696
+ `,n.state&&(r+=`State: ${Dn(n.state,!0)}
697
697
  `),r+=`${e}
698
- `;break;default:r=JSON.stringify(n,null,2)}i(r)}},pu=Mr(),du=i=>{let e=new Map;return{logger:i,startTiming:t=>{e.set(t,Date.now())},endTiming:t=>{let n=e.get(t);if(!n)return 0;let r=Date.now()-n;return e.delete(t),r},getCurrentTime:()=>Date.now()}};var 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,s)=>{let a=n?.ai??s.mainAi,l=n?.options??s.mainOptions,u=t(o),c=l?.traceLabel?`Node:${e} (${l.traceLabel})`:`Node:${e}`,p;if("forward"in r&&typeof r.forward=="function")p=await r.forward(a,u,{...l,traceLabel:c});else throw new Error(`Node program for '${e}' does not have a forward method`);return{...o,[`${e}Result`]:p}}),this}map(e){return this.steps.push(t=>e(t)),this}async executeSteps(e,t){let n=e;for(let r of this.steps)n=await r(n,t);return n}},ts=class{constructor(e){this.nodeGenerators=e}steps=[];execute(e,t,n){let r=this.nodeGenerators.get(e);if(!r)throw new Error(`Node program for '${e}' not found.`);return this.steps.push(async(o,s)=>{let a=n?.ai??s.mainAi,l=n?.options??s.mainOptions,u=t(o),c=l?.traceLabel?`Node:${e} (${l.traceLabel})`:`Node:${e}`,p;if("forward"in r&&typeof r.forward=="function")p=await r.forward(a,u,{...l,traceLabel:c});else throw new Error(`Node program for '${e}' does not have a forward method`);return{...o,[`${e}Result`]:p}}),this}map(e){return this.steps.push(t=>e(t)),this}async executeSteps(e,t){let n=e;for(let r of this.steps)n=await r(n,t);return n}};var Er=class i{static _ctorWarned=!1;nodes=new Map;flowDefinition=[];nodeGenerators=new Map;loopStack=[];stepLabels=new Map;branchContext=null;autoParallelConfig;executionPlanner=new Dn;program;flowName;nodeUsage=new Map;nodeTraces=new Map;flowLogger;timingLogger;defaultAIOptions;activeAbortControllers=new Set;_stopRequested=!1;toCamelCase(e){return e.replace(/_([a-z])/g,(t,n)=>n.toUpperCase())}stop(){this._stopRequested=!0;for(let e of this.activeAbortControllers)e.abort("Stopped by user")}extractInputValues(e){if(!Array.isArray(e))return e;let t=e.filter(n=>n.role==="user").pop();if(!t)throw new Error("No user message found in values array");return t.values}getCacheKey(e,t){if(!t)return;this.ensureProgram();let n=this.program.getSignature(),r=n.getInputFields().map(l=>l.name),o=this.extractInputValues(e),s=tt("sha256");s.update(n.hash()??"");let a=l=>{let u=typeof l;if(s.update(`|${u}|`),l==null){s.update("null");return}if(u==="string"||u==="number"||u==="boolean"){s.update(String(l));return}if(Array.isArray(l)){s.update("[");for(let c of l)a(c);s.update("]");return}if(typeof l=="object"&&l!==null&&"mimeType"in l&&"data"in l){let c=l;s.update(c.mimeType??"");let p=tt("sha256").update(c.data??"").digest("hex");s.update(p);return}if(typeof l=="object"){let c=l,p=Object.keys(c).sort();for(let d of p)s.update(`{${d}}`),a(c[d]);return}s.update(String(l))};for(let l of r)a(o?.[l]);return s.digest("hex")}async executeStepsWithLogging(e,t,n,r){let o={...t},s=0;for(let a=0;a<e.length;a++){let l=e[a];if(!l)continue;let u=n.mainOptions?.abortSignal;if(u?.aborted)throw new Fe("flow-between-steps",u.reason??"Flow aborted between steps");let c=this.getStepType(l,a),p=this.getStepMetadata(l,a),d=Object.keys(o);this.flowLogger&&this.flowLogger({name:"StepStart",timestamp:Date.now(),stepIndex:a,stepType:c,nodeName:p.nodeName,dependencies:p.dependencies,produces:p.produces,state:{...o}});let m=Date.now();this.timingLogger?.startTiming(`step-${a}`);try{o=await l(o,n),s++;let h=this.timingLogger?.endTiming(`step-${a}`)??Date.now()-m,b=Object.keys(o).filter(A=>!d.includes(A)),x;if(c==="execute"&&p.nodeName&&b.length>0){let A=`${p.nodeName}Result`;x=o[A]}this.flowLogger&&this.flowLogger({name:"StepComplete",timestamp:Date.now(),stepIndex:a,stepType:c,nodeName:p.nodeName,executionTime:h,state:{...o},newFields:b,result:x})}catch(g){throw this.flowLogger&&this.flowLogger({name:"FlowError",timestamp:Date.now(),error:g instanceof Error?g.message:String(g),stepIndex:a,stepType:c,nodeName:p.nodeName,state:{...o}}),g}}return{finalState:o,stepsExecuted:s}}getStepType(e,t){let n=this.executionPlanner.getExecutionPlan().steps.find(o=>o.stepIndex===t);if(n)return n.type;let r=e.toString();return r.includes("nodeName")||r.includes("nodeProgram")?"execute":r.includes("_parallelResults")||r.includes("processBatches")?"parallel":r.includes("branchValue")||r.includes("branches.get")||r.includes("mergeFunction")?"merge":r.includes("transform(")||r.includes("...state,")?"map":r.includes("inputValue")&&r.includes("transformFn")?"derive":r.includes("condition(")&&r.includes("iterations")?r.includes("while")?"while":"feedback":r.includes("branchSteps")||r.includes("currentState")?"branch":"other"}getStepMetadata(e,t){let r=this.executionPlanner.getExecutionPlan().steps.find(a=>a.stepIndex===t);if(r)return{nodeName:r.nodeName,dependencies:r.dependencies,produces:r.produces};let o=e.toString();return{nodeName:this.extractNodeNameFromSource(o),dependencies:[],produces:[]}}extractNodeNameFromSource(e){let t=e.match(/nodeName['"]?\s*[=:]\s*['"](\w+)['"]/);if(t)return t[1];let n=e.match(/nodeProgram\.get\(['"](\w+)['"]\)/);if(n)return n[1]}inferSignatureFromFlow(){let e=this.executionPlanner.getExecutionPlan();if(this.nodeGenerators.size===0&&e.steps.length===0)return ge().input("userInput",ge.string("User input to the flow")).output("flowOutput",ge.string("Output from the flow")).build();let t=new Set,n=new Set;for(let c of e.steps)c.produces.forEach(p=>t.add(p)),c.dependencies.forEach(p=>n.add(p));let r=new Set;for(let c of Array.from(n))t.has(c)||r.add(c);let o=new Set,s=e.steps[e.steps.length-1];if(s&&(s.type==="map"||s.type==="merge")){if(s.produces.forEach(c=>{c.startsWith("_")||o.add(c)}),s.type==="merge"&&s.produces.includes("_mergedResult"))for(let c of e.steps)c.type==="execute"&&c.produces.length>0&&c.produces.forEach(p=>o.add(p))}else for(let c of Array.from(t)){let p=!1;for(let d of e.steps)if(d.dependencies.includes(c)){p=!0;break}if(!p)if(c.endsWith("Result")){let d=c.replace("Result",""),m=this.nodeGenerators.get(d);if(m){let h=m.getSignature().getOutputFields();for(let f of h)o.add(f.name)}else o.add(c)}else o.add(c)}if(r.size===0&&o.size===0){let c=[],p=[];for(let[m,g]of Array.from(this.nodeGenerators)){let h=g.getSignature();for(let f of h.getInputFields()){let b=this.toCamelCase(`${m}_${f.name}`);c.push({name:b,type:f.type,description:f.description,isOptional:f.isOptional,isInternal:f.isInternal})}for(let f of h.getOutputFields()){let b=this.toCamelCase(`${m}_${f.name}`);p.push({name:b,type:f.type,description:f.description,isOptional:f.isOptional,isInternal:f.isInternal})}}let d=new Oe;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 Oe,l=[];for(let c of Array.from(r))l.push({name:c,type:{name:"string"},description:`Input field: ${c}`});l.length===0&&l.push({name:"userInput",type:{name:"string"},description:"User input to the flow"});let u=[];for(let c of Array.from(o))c.startsWith("_")||u.push({name:c,type:{name:"string"},description:`Output field: ${c}`});return u.length===0&&u.push({name:"flowOutput",type:{name:"string"},description:"Output from the flow"}),a.setInputFields(l),a.setOutputFields(u),a}constructor(e){i._ctorWarned||(console.warn("[AxFlow] new AxFlow() is deprecated. Use flow() factory instead."),i._ctorWarned=!0),this.autoParallelConfig={enabled:e?.autoParallel!==!1,batchSize:e?.batchSize||10},e?.logger?this.flowLogger=e.logger:e?.debug===!0?this.flowLogger=Mr():this.flowLogger=void 0,this.timingLogger=this.flowLogger?du(this.flowLogger):void 0,(e?.tracer||e?.meter)&&(this.defaultAIOptions={tracer:e.tracer,meter:e.meter})}static create(e){return new i(e)}ensureProgram(){let e=this.inferSignatureFromFlow();if(!this.program){this.program=new Pt(e);for(let[t,n]of Array.from(this.nodeGenerators))this.program.register(n,t);return}this.program.setSignature(e)}getId(){return this.ensureProgram(),this.program.getId()}setId(e){this.ensureProgram(),this.program.setId(e)}namedPrograms(){return this.ensureProgram(),this.program.namedPrograms()}getTraces(){let e=[];for(let[t,n]of Array.from(this.nodeTraces))e.push(...n);return e}setDemos(e,t){this.ensureProgram(),this.program.setDemos(e,t)}description(e,t){return this.ensureProgram(),this.flowName=e,this.program.setDescription(t),this}toFunction(){this.ensureProgram();let e=this.program.getSignature(),n=(this.flowName??(e.getDescription()?.trim().split(`
698
+ `;break;default:r=JSON.stringify(n,null,2)}i(r)}},pu=Or(),du=i=>{let e=new Map;return{logger:i,startTiming:t=>{e.set(t,Date.now())},endTiming:t=>{let n=e.get(t);if(!n)return 0;let r=Date.now()-n;return e.delete(t),r},getCurrentTime:()=>Date.now()}};var Bn=class{constructor(e){this.nodeGenerators=e}steps=[];execute(e,t,n){let r=this.nodeGenerators.get(e);if(!r)throw new Error(`Node program for '${e}' not found.`);return this.steps.push(async(o,s)=>{let a=n?.ai??s.mainAi,l=n?.options??s.mainOptions,u=t(o),c=l?.traceLabel?`Node:${e} (${l.traceLabel})`:`Node:${e}`,p;if("forward"in r&&typeof r.forward=="function")p=await r.forward(a,u,{...l,traceLabel:c});else throw new Error(`Node program for '${e}' does not have a forward method`);return{...o,[`${e}Result`]:p}}),this}map(e){return this.steps.push(t=>e(t)),this}async executeSteps(e,t){let n=e;for(let r of this.steps)n=await r(n,t);return n}},ts=class{constructor(e){this.nodeGenerators=e}steps=[];execute(e,t,n){let r=this.nodeGenerators.get(e);if(!r)throw new Error(`Node program for '${e}' not found.`);return this.steps.push(async(o,s)=>{let a=n?.ai??s.mainAi,l=n?.options??s.mainOptions,u=t(o),c=l?.traceLabel?`Node:${e} (${l.traceLabel})`:`Node:${e}`,p;if("forward"in r&&typeof r.forward=="function")p=await r.forward(a,u,{...l,traceLabel:c});else throw new Error(`Node program for '${e}' does not have a forward method`);return{...o,[`${e}Result`]:p}}),this}map(e){return this.steps.push(t=>e(t)),this}async executeSteps(e,t){let n=e;for(let r of this.steps)n=await r(n,t);return n}};var Mr=class i{static _ctorWarned=!1;nodes=new Map;flowDefinition=[];nodeGenerators=new Map;loopStack=[];stepLabels=new Map;branchContext=null;autoParallelConfig;executionPlanner=new Gn;program;flowName;nodeUsage=new Map;nodeTraces=new Map;flowLogger;timingLogger;defaultAIOptions;activeAbortControllers=new Set;_stopRequested=!1;toCamelCase(e){return e.replace(/_([a-z])/g,(t,n)=>n.toUpperCase())}stop(){this._stopRequested=!0;for(let e of this.activeAbortControllers)e.abort("Stopped by user")}extractInputValues(e){if(!Array.isArray(e))return e;let t=e.filter(n=>n.role==="user").pop();if(!t)throw new Error("No user message found in values array");return t.values}getCacheKey(e,t){if(!t)return;this.ensureProgram();let n=this.program.getSignature(),r=n.getInputFields().map(l=>l.name),o=this.extractInputValues(e),s=et("sha256");s.update(n.hash()??"");let a=l=>{let u=typeof l;if(s.update(`|${u}|`),l==null){s.update("null");return}if(u==="string"||u==="number"||u==="boolean"){s.update(String(l));return}if(Array.isArray(l)){s.update("[");for(let c of l)a(c);s.update("]");return}if(typeof l=="object"&&l!==null&&"mimeType"in l&&"data"in l){let c=l;s.update(c.mimeType??"");let p=et("sha256").update(c.data??"").digest("hex");s.update(p);return}if(typeof l=="object"){let c=l,p=Object.keys(c).sort();for(let d of p)s.update(`{${d}}`),a(c[d]);return}s.update(String(l))};for(let l of r)a(o?.[l]);return s.digest("hex")}async executeStepsWithLogging(e,t,n,r){let o={...t},s=0;for(let a=0;a<e.length;a++){let l=e[a];if(!l)continue;let u=n.mainOptions?.abortSignal;if(u?.aborted)throw new Fe("flow-between-steps",u.reason??"Flow aborted between steps");let c=this.getStepType(l,a),p=this.getStepMetadata(l,a),d=Object.keys(o);this.flowLogger&&this.flowLogger({name:"StepStart",timestamp:Date.now(),stepIndex:a,stepType:c,nodeName:p.nodeName,dependencies:p.dependencies,produces:p.produces,state:{...o}});let m=Date.now();this.timingLogger?.startTiming(`step-${a}`);try{o=await l(o,n),s++;let h=this.timingLogger?.endTiming(`step-${a}`)??Date.now()-m,I=Object.keys(o).filter(A=>!d.includes(A)),x;if(c==="execute"&&p.nodeName&&I.length>0){let A=`${p.nodeName}Result`;x=o[A]}this.flowLogger&&this.flowLogger({name:"StepComplete",timestamp:Date.now(),stepIndex:a,stepType:c,nodeName:p.nodeName,executionTime:h,state:{...o},newFields:I,result:x})}catch(g){throw this.flowLogger&&this.flowLogger({name:"FlowError",timestamp:Date.now(),error:g instanceof Error?g.message:String(g),stepIndex:a,stepType:c,nodeName:p.nodeName,state:{...o}}),g}}return{finalState:o,stepsExecuted:s}}getStepType(e,t){let n=this.executionPlanner.getExecutionPlan().steps.find(o=>o.stepIndex===t);if(n)return n.type;let r=e.toString();return r.includes("nodeName")||r.includes("nodeProgram")?"execute":r.includes("_parallelResults")||r.includes("processBatches")?"parallel":r.includes("branchValue")||r.includes("branches.get")||r.includes("mergeFunction")?"merge":r.includes("transform(")||r.includes("...state,")?"map":r.includes("inputValue")&&r.includes("transformFn")?"derive":r.includes("condition(")&&r.includes("iterations")?r.includes("while")?"while":"feedback":r.includes("branchSteps")||r.includes("currentState")?"branch":"other"}getStepMetadata(e,t){let r=this.executionPlanner.getExecutionPlan().steps.find(a=>a.stepIndex===t);if(r)return{nodeName:r.nodeName,dependencies:r.dependencies,produces:r.produces};let o=e.toString();return{nodeName:this.extractNodeNameFromSource(o),dependencies:[],produces:[]}}extractNodeNameFromSource(e){let t=e.match(/nodeName['"]?\s*[=:]\s*['"](\w+)['"]/);if(t)return t[1];let n=e.match(/nodeProgram\.get\(['"](\w+)['"]\)/);if(n)return n[1]}inferSignatureFromFlow(){let e=this.executionPlanner.getExecutionPlan();if(this.nodeGenerators.size===0&&e.steps.length===0)return fe().input("userInput",fe.string("User input to the flow")).output("flowOutput",fe.string("Output from the flow")).build();let t=new Set,n=new Set;for(let c of e.steps)c.produces.forEach(p=>t.add(p)),c.dependencies.forEach(p=>n.add(p));let r=new Set;for(let c of Array.from(n))t.has(c)||r.add(c);let o=new Set,s=e.steps[e.steps.length-1];if(s&&(s.type==="map"||s.type==="merge")){if(s.produces.forEach(c=>{c.startsWith("_")||o.add(c)}),s.type==="merge"&&s.produces.includes("_mergedResult"))for(let c of e.steps)c.type==="execute"&&c.produces.length>0&&c.produces.forEach(p=>o.add(p))}else for(let c of Array.from(t)){let p=!1;for(let d of e.steps)if(d.dependencies.includes(c)){p=!0;break}if(!p)if(c.endsWith("Result")){let d=c.replace("Result",""),m=this.nodeGenerators.get(d);if(m){let h=m.getSignature().getOutputFields();for(let f of h)o.add(f.name)}else o.add(c)}else o.add(c)}if(r.size===0&&o.size===0){let c=[],p=[];for(let[m,g]of Array.from(this.nodeGenerators)){let h=g.getSignature();for(let f of h.getInputFields()){let I=this.toCamelCase(`${m}_${f.name}`);c.push({name:I,type:f.type,description:f.description,isOptional:f.isOptional,isInternal:f.isInternal})}for(let f of h.getOutputFields()){let I=this.toCamelCase(`${m}_${f.name}`);p.push({name:I,type:f.type,description:f.description,isOptional:f.isOptional,isInternal:f.isInternal})}}let d=new Oe;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 Oe,l=[];for(let c of Array.from(r))l.push({name:c,type:{name:"string"},description:`Input field: ${c}`});l.length===0&&l.push({name:"userInput",type:{name:"string"},description:"User input to the flow"});let u=[];for(let c of Array.from(o))c.startsWith("_")||u.push({name:c,type:{name:"string"},description:`Output field: ${c}`});return u.length===0&&u.push({name:"flowOutput",type:{name:"string"},description:"Output from the flow"}),a.setInputFields(l),a.setOutputFields(u),a}constructor(e){i._ctorWarned||(console.warn("[AxFlow] new AxFlow() is deprecated. Use flow() factory instead."),i._ctorWarned=!0),this.autoParallelConfig={enabled:e?.autoParallel!==!1,batchSize:e?.batchSize||10},e?.logger?this.flowLogger=e.logger:e?.debug===!0?this.flowLogger=Or():this.flowLogger=void 0,this.timingLogger=this.flowLogger?du(this.flowLogger):void 0,(e?.tracer||e?.meter)&&(this.defaultAIOptions={tracer:e.tracer,meter:e.meter})}static create(e){return new i(e)}ensureProgram(){let e=this.inferSignatureFromFlow();if(!this.program){this.program=new Pt(e);for(let[t,n]of Array.from(this.nodeGenerators))this.program.register(n,t);return}this.program.setSignature(e)}getId(){return this.ensureProgram(),this.program.getId()}setId(e){this.ensureProgram(),this.program.setId(e)}namedPrograms(){return this.ensureProgram(),this.program.namedPrograms()}getTraces(){let e=[];for(let[t,n]of Array.from(this.nodeTraces))e.push(...n);return e}setDemos(e,t){this.ensureProgram(),this.program.setDemos(e,t)}description(e,t){return this.ensureProgram(),this.flowName=e,this.program.setDescription(t),this}toFunction(){this.ensureProgram();let e=this.program.getSignature(),n=(this.flowName??(e.getDescription()?.trim().split(`
699
699
  `)[0]||"axFlow")).replace(/\s+/g,"_"),r=this.toCamelCase(n),o=async(s,a)=>{let l=a?.ai;if(!l)throw new Error("AI service is required to run the flow");let u=await this.forward(l,s??{}),c=e.getOutputFields(),p=u??{};return Object.keys(p).map(d=>{let m=c.find(g=>g.name===d);return m&&m.title?`${m.title}: ${p[d]}`:`${d}: ${p[d]}`}).join(`
700
- `)};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 fr(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]=fr(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??ce.cachingFunction,o=this.getCacheKey(t,r);if(r&&o){let a;try{a=await r(o)}catch{}if(a!==void 0){yield{version:0,index:0,delta:a};return}}let s=await this.forward(e,t,n);if(r&&o)try{await r(o,s)}catch{}yield{version:1,index:0,delta:s}}async forward(e,t,n){let r=n?.cachingFunction??ce.cachingFunction,o=this.getCacheKey(t,r);if(r&&o){let c;try{c=await r(o)}catch{}if(c!==void 0)return c}let s=Date.now();this.timingLogger?.startTiming("flow-execution");let a={},l,u;try{this.resetUsage(),this.resetTraces();let c=this.extractInputValues(t);if(this.nodeGenerators.size>0&&this.ensureProgram(),a={...c},this.flowLogger){let A=this.getExecutionPlan();this.flowLogger({name:"FlowStart",timestamp:s,inputFields:Object.keys(c),totalSteps:A.totalSteps,parallelGroups:A.parallelGroups,maxParallelism:A.maxParallelism,autoParallelEnabled:A.autoParallelEnabled})}let p=n?.tracer??this.defaultAIOptions?.tracer,d=n?.traceContext,m=d;if(p){let A=this.getExecutionPlan(),w=n?.traceLabel?`AxFlow > ${n.traceLabel}`:"AxFlow";l=p.startSpan(w,{kind:qn.SpanKind.INTERNAL,attributes:{total_steps:A.totalSteps,parallel_groups:A.parallelGroups,max_parallelism:A.maxParallelism,auto_parallel_enabled:A.autoParallelEnabled}});let I=d??qn.context.active();m=qn.trace.setSpan(I,l)}u=new AbortController,this.activeAbortControllers.add(u),this._stopRequested&&u.abort("Stopped by user (pre-forward)");let g=vt(n?.abortSignal,n?.abortController?.signal),h=vt(u.signal,vt(g,ce.abortSignal)),f={mainAi:e,mainOptions:(()=>{let A={...this.defaultAIOptions??{},...n};return n?.model&&(A.model=String(n.model)),p&&(A.tracer=p),m&&(A.traceContext=m),h&&(A.abortSignal=h),Object.keys(A).length>0?A:void 0})()},b=n?.autoParallel!==!1&&this.autoParallelConfig.enabled,x=0;if(b){this.executionPlanner.setInitialFields(Object.keys(c));let A=this.executionPlanner.createOptimizedExecution(this.autoParallelConfig.batchSize),w=await this.executeStepsWithLogging(A,a,f,!0);a=w.finalState,x=w.stepsExecuted}else{let A=await this.executeStepsWithLogging(this.flowDefinition,a,f,!1);a=A.finalState,x=A.stepsExecuted}if(this.flowLogger){let A=this.timingLogger?.endTiming("flow-execution")??Date.now()-s;this.flowLogger({name:"FlowComplete",timestamp:Date.now(),totalExecutionTime:A,finalState:a,outputFields:Object.keys(a),stepsExecuted:x})}if(r&&o)try{await r(o,a)}catch{}return a}catch(c){throw this.flowLogger&&this.flowLogger({name:"FlowError",timestamp:Date.now(),error:c instanceof Error?c.message:String(c),state:a}),c}finally{l&&l.end(),u&&this.activeAbortControllers.delete(u),this._stopRequested=!1}}node(e,t){if(this.nodes.has(e)||this.nodeGenerators.has(e))throw new Error(`Node '${e}' is already defined. Use a unique node name in this flow.`);if(typeof t=="string"||t instanceof Oe){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=We(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 Gn(n,async(l,u)=>{let c=l(o);return Promise.resolve(c)},this.autoParallelConfig.batchSize)).reduce((l,u)=>({...l,...u}),o);if(this.branchContext?.currentBranchValue!==void 0){let o=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];o.push(r),this.branchContext.branches.set(this.branchContext.currentBranchValue,o)}else this.flowDefinition.push(r),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(r,void 0,void 0,"parallel-map",n)}else{let n=async r=>{if(Array.isArray(e))throw new Error("Array of transforms requires parallel: true option");let o=e(r);return Promise.resolve(o)};if(this.branchContext?.currentBranchValue!==void 0){let r=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];r.push(n),this.branchContext.branches.set(this.branchContext.currentBranchValue,r)}else this.flowDefinition.push(n),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(n,void 0,void 0,"map",e)}return this.nodeGenerators.size>0&&this.ensureProgram(),this}m(e,t){return this.map(e,t)}returns(e){let t=async n=>{let r=e(n);return Promise.resolve(r)};if(this.branchContext?.currentBranchValue!==void 0){let n=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];n.push(t),this.branchContext.branches.set(this.branchContext.currentBranchValue,n)}else this.flowDefinition.push(t),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(t,void 0,void 0,"map",e);return this.nodeGenerators.size>0&&this.ensureProgram(),this}r(e){return this.returns(e)}label(e){if(this.branchContext?.currentBranchValue!==void 0)throw new Error("Cannot create labels inside branch blocks");return this.stepLabels.set(e,this.flowDefinition.length),this}l(e){return this.label(e)}execute(e,t,n){if(!this.nodes.has(e))throw new Error(`Node '${e}' not found. Make sure to define it with .node() first.`);let r=this.nodeGenerators.get(e);if(!r)throw new Error(`Node program for '${e}' not found.`);let o=async(s,a)=>{let l=n?.ai??a.mainAi,u={...a.mainOptions??{},...n?.options??{}},c=t(s),p=u?.traceLabel?`Node:${e} (${u.traceLabel})`:`Node:${e}`,d;if("forward"in r&&typeof r.forward=="function"){if(d=await r.forward(l,c,{...u,traceLabel:p}),"getUsage"in r&&typeof r.getUsage=="function"){let m=r.getUsage();if(m&&m.length>0){let g=this.nodeUsage.get(e)||[];this.nodeUsage.set(e,[...g,...m])}}if("getTraces"in r&&typeof r.getTraces=="function"){let m=r.getTraces();if(m&&m.length>0){let g=this.nodeTraces.get(e)||[];this.nodeTraces.set(e,[...g,...m])}}}else throw new Error(`Node program for '${e}' does not have a forward method`);return{...s,[`${e}Result`]:d}};if(this.branchContext?.currentBranchValue!==void 0){let s=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];s.push(o),this.branchContext.branches.set(this.branchContext.currentBranchValue,s)}else this.flowDefinition.push(o),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(o,e,t);return this.ensureProgram(),this}applyOptimization(e){this.program&&"applyOptimization"in this.program&&this.program.applyOptimization(e);for(let[t,n]of Array.from(this.nodeGenerators))n&&"applyOptimization"in n&&typeof n.applyOptimization=="function"&&n.applyOptimization(e)}e(e,t,n){return this.execute(e,t,n)}branch(e){if(this.branchContext)throw new Error("Nested branches are not supported");return this.branchContext={predicate:t=>e(t),branches:new Map,currentBranchValue:void 0},this}b(e){return this.branch(e)}when(e){if(!this.branchContext)throw new Error("when() called without matching branch()");return this.branchContext.currentBranchValue=e,this.branchContext.branches.set(e,[]),this}w(e){return this.when(e)}merge(){if(!this.branchContext)throw new Error("merge() called without matching branch()");let e=this.branchContext;this.branchContext=null;let t=async(n,r)=>{let o=e.predicate(n),s=e.branches.get(o);if(this.flowLogger&&this.flowLogger({name:"BranchEvaluation",timestamp:Date.now(),branchValue:o,hasMatchingBranch:!!s,branchStepsCount:s?.length??0}),!s)return n;let a=n;for(let l of s)a=await l(a,r);return a};return this.flowDefinition.push(t),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(t,void 0,void 0,"merge"),this.ensureProgram(),this}mg(){return this.merge()}parallel(e){let t=async(n,r)=>{let o=await Gn(e,async(s,a)=>{let l=new jn(this.nodeGenerators);return await s(l).executeSteps(n,r)},this.autoParallelConfig.batchSize);return{...n,_parallelResults:o}};return this.flowDefinition.push(t),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(t,void 0,void 0,"parallel",void 0,void 0),this.ensureProgram(),{merge:(n,r)=>{let o=s=>{let a=s._parallelResults;if(!Array.isArray(a))throw new Error("No parallel results found for merge");let l=r(...a),u={...s};return delete u._parallelResults,u[n]=l,u};return this.flowDefinition.push(o),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(o,void 0,void 0,"merge",void 0,{resultKey:n,mergeFunction:r}),this.ensureProgram(),this}}}p(e){return this.parallel(e)}feedback(e,t,n=10){if(!this.stepLabels.has(t))throw new Error(`Label '${t}' not found. Make sure to define it with .label() before the feedback point.`);let r=this.stepLabels.get(t),o=this.flowDefinition.length;return this.flowDefinition.push(async(s,a)=>{let l=s,u=1,c=`_feedback_${t}_iterations`;for(typeof l[c]!="number"&&(l={...l,[c]:1});e(l)&&u<n;){u++,l={...l,[c]:u};for(let p=r;p<o;p++){let d=this.flowDefinition[p];d&&(l=await d(l,a))}}return l}),this.nodeGenerators.size>0&&this.ensureProgram(),this}fb(e,t,n=10){return this.feedback(e,t,n)}while(e,t=100){let n=this.flowDefinition.length;this.loopStack.push(n);let r=Object.assign(o=>o,{_condition:e,_maxIterations:t,_isLoopStart:!0});return this.flowDefinition.push(r),this.nodeGenerators.size>0&&this.ensureProgram(),this}wh(e,t=100){return this.while(e,t)}endWhile(){if(this.loopStack.length===0)throw new Error("endWhile() called without matching while()");let e=this.loopStack.pop(),t=this.flowDefinition[e];if(!t||!("_isLoopStart"in t))throw new Error("Loop start step not found or invalid");let n=t._condition,r=t._maxIterations,o=this.flowDefinition.splice(e+1);return this.flowDefinition[e]=async(s,a)=>{let l=s,u=0;for(;n(l)&&u<r;){u++;for(let c of o)l=await c(l,a)}if(u>=r&&n(l))throw new Error(`While loop exceeded maximum iterations (${r}). Consider increasing maxIterations or ensuring the loop condition eventually becomes false.`);return l},this.nodeGenerators.size>0&&this.ensureProgram(),this}end(){return this.endWhile()}derive(e,t,n,r){let o=async s=>{let a=s[t];if(a===void 0)throw new Error(`Input field '${t}' not found in state`);let l;if(Array.isArray(a))if(this.autoParallelConfig.enabled){let u=r?.batchSize||this.autoParallelConfig.batchSize;l=await Gn(a,async(c,p)=>n(c,p,s),u)}else l=a.map((u,c)=>n(u,c,s));else l=n(a,void 0,s);return{...s,[e]:l}};if(this.branchContext?.currentBranchValue!==void 0){let s=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];s.push(o),this.branchContext.branches.set(this.branchContext.currentBranchValue,s)}else this.flowDefinition.push(o),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(o,void 0,void 0,"derive",n,void 0,{inputFieldName:t,outputFieldName:e,batchSize:r?.batchSize});return this.ensureProgram(),this}getExecutionPlan(){let e=this.executionPlanner.getExecutionPlan();return{totalSteps:e.totalSteps,parallelGroups:e.parallelGroups,maxParallelism:e.maxParallelism,autoParallelEnabled:this.autoParallelConfig.enabled,steps:e.steps,groups:e.groups}}getSignature(){return this.ensureProgram(),this.program.getSignature()}nodeExtended(e,t,n){let o=typeof t=="string"?Oe.create(t):t;if(n.prependInputs)for(let s of n.prependInputs)o=o.prependInputField(s.name,s.type);if(n.appendInputs)for(let s of n.appendInputs)o=o.appendInputField(s.name,s.type);if(n.prependOutputs)for(let s of n.prependOutputs)o=o.prependOutputField(s.name,s.type);if(n.appendOutputs)for(let s of n.appendOutputs)o=o.appendOutputField(s.name,s.type);return this.node(e,o)}nx(e,t,n){return this.nodeExtended(e,t,n)}mapOutput(e){let t=async n=>{let r=e(n);return{...n,...r}};if(this.branchContext?.currentBranchValue!==void 0){let n=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];n.push(t),this.branchContext.branches.set(this.branchContext.currentBranchValue,n)}else this.flowDefinition.push(t),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(t,void 0,void 0,"map",e);return this.nodeGenerators.size>0&&this.ensureProgram(),this}mo(e){return this.mapOutput(e)}};function ns(i){return Er.create(i)}var rs=class{apiUrl;containerId=null;constructor(e="http://localhost:2375"){this.apiUrl=e}async pullImage(e){let t=await this.fetchDockerAPI(`/images/create?fromImage=${encodeURIComponent(e)}`,{method:"POST"});if(!t.ok)throw new Error(`Failed to pull image: ${t.statusText}`);await t.text()}async createContainer({imageName:e,volumes:t=[],doNotPullImage:n,tag:r}){let o=t.map(u=>`${u.hostPath}:${u.containerPath}`);n||await this.pullImage(e);let s={Image:e,Tty:!0,OpenStdin:!1,AttachStdin:!1,AttachStdout:!1,AttachStderr:!1,HostConfig:{Binds:o},Labels:{}};r&&(s.Labels["com.example.tag"]=r);let a=await this.fetchDockerAPI("/containers/create",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(s)});if(!a.ok)throw new Error(`Failed to create container: ${a.statusText}`);let l=await a.json();return this.containerId=l.Id,l}async findOrCreateContainer({imageName:e,volumes:t=[],doNotPullImage:n,tag:r}){let s=(await this.listContainers(!0)).filter(l=>l.Labels&&l.Labels["com.example.tag"]===r);if(s&&s.length>0){let l=Math.floor(Math.random()*s.length),u=s[l];if(u)return await this.connectToContainer(u.Id),{Id:u.Id,isNew:!1}}return{Id:(await this.createContainer({imageName:e,volumes:t,doNotPullImage:n,tag:r})).Id,isNew:!0}}async startContainer(){if(!this.containerId)throw new Error("No container created or connected");let e=await this.fetchDockerAPI(`/containers/${this.containerId}/start`,{method:"POST"});if(!e.ok)throw new Error(`Failed to start container: ${e.statusText}`)}async connectToContainer(e){let t=await this.fetchDockerAPI(`/containers/${e}/json`);if(!t.ok)throw new Error(`Failed to connect to container: ${t.statusText}`);this.containerId=e}async stopContainers({tag:e,remove:t,timeout:n=10}){let r=[],o=await this.listContainers(!0),s=e?o.filter(a=>a.Labels["com.example.tag"]===e):o;for(let a of s){if(a.State.Status==="running"){let l=await this.fetchDockerAPI(`/containers/${a.Id}/stop?t=${n}`,{method:"POST"});if(!l.ok){console.warn(`Failed to stop container ${a.Id}: ${l.statusText}`);continue}r.push({Id:a.Id,Action:"stopped"})}if(t){let l=await this.fetchDockerAPI(`/containers/${a.Id}`,{method:"DELETE"});if(!l.ok){console.warn(`Failed to remove container ${a.Id}: ${l.statusText}`);continue}r.push({Id:a.Id,Action:"removed"})}}return r}async listContainers(e=!1){return(await this.fetchDockerAPI(`/containers/json?all=${e}`,{method:"GET"})).json()}async getContainerLogs(){if(!this.containerId)throw new Error("No container created or connected");return(await this.fetchDockerAPI(`/containers/${this.containerId}/logs?stdout=true&stderr=true`,{method:"GET"})).text()}async executeCommand(e){if(!this.containerId)throw new Error("No container created or connected");(await this.getContainerInfo(this.containerId)).State.Status!=="running"&&(await this.startContainer(),await this.waitForContainerToBeRunning(this.containerId));let n=await this.fetchDockerAPI(`/containers/${this.containerId}/exec`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({Cmd:["sh","-c",e],AttachStdout:!0,AttachStderr:!0})});if(!n.ok)throw new Error(`Failed to create exec instance: ${n.statusText}`);let r=await n.json(),o=await this.fetchDockerAPI(`/exec/${r.Id}/start`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({Detach:!1,Tty:!1})});if(!o.ok)throw new Error(`Failed to start exec instance: ${o.statusText}`);return await o.text()}async getContainerInfo(e){let t=await this.fetchDockerAPI(`/containers/${e}/json`);if(!t.ok)throw new Error(`Failed to get container info: ${t.statusText}`);return t.json()}async waitForContainerToBeRunning(e,t=3e4){let n=Date.now();for(;Date.now()-n<t;){if((await this.getContainerInfo(e)).State.Status==="running")return;await new Promise(o=>setTimeout(o,1e3))}throw new Error("Timeout waiting for container to start")}async fetchDockerAPI(e,t){let n=new URL(e,this.apiUrl).toString();return await fetch(n,t)}toFunction(){return{name:"commandExecution",description:"Use this function to execute shell commands, scripts, and programs. This function enables interaction with the file system, running system utilities, and performing tasks that require a shell interface.",parameters:{type:"object",properties:{command:{type:"string",description:'Shell command to execute. eg. `ls -l` or `echo "Hello, World!"`.'}},required:["command"]},func:async({command:e})=>await this.executeCommand(e)}}};var os=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 ss(i){let e=typeof self<"u"?self:globalThis,t=Object.getPrototypeOf(async()=>{}).constructor,n=i.functionRefKey,r="return",o="stdout",s=/^(if|for|while|switch|try|catch|finally|function|class|import|export|throw|return|var|let|const|break|continue|debugger)\b/,a=/^\s*return\s+([^\n;]+?)\s*;?\s*$/,l={network:["fetch","XMLHttpRequest","WebSocket","EventSource"],storage:["indexedDB","caches"],"code-loading":["importScripts"],communication:["BroadcastChannel"],timing:["performance"],workers:["Worker","SharedWorker"]},u=()=>{let C;if(typeof process<"u"&&typeof process.getBuiltinModule=="function"&&(C=process.getBuiltinModule.bind(process)),!C)try{C=new Function('return typeof require==="function"?require:undefined')()}catch{C=void 0}if(!(typeof C=="function"&&typeof process<"u"&&!!process.versions?.node))return{isNodeWorker:!1,parentPort:null};try{return{isNodeWorker:!0,parentPort:C("node:worker_threads").parentPort??null}}catch{return{isNodeWorker:!0,parentPort:null}}},{isNodeWorker:c,parentPort:p}=u(),d=()=>{if(!p&&typeof e.postMessage!="function")throw new Error("Worker transport unavailable: no postMessage channel");return{send:R=>{if(p){p.postMessage(R);return}e.postMessage(R)},setOnMessage:R=>{if(p){p.on("message",y=>R({data:y}));return}e.onmessage=R}}},{send:m,setOnMessage:g}=d(),h=C=>C&&(/\r?\n$/.test(C)?C:`${C}
701
- `),f=C=>{let O=C.trim();return O.startsWith("//")||O.startsWith("/*")||O.startsWith("*")},b=C=>{let O=C.length-1;for(;O>=0;){let R=C[O].trim();if(R&&!f(R))break;O-=1}return O},x=C=>{if(!C)return!0;let O=C.split(`
702
- `).find(R=>R.trim().length>0)?.trim();return!O||s.test(O)?!0:O.startsWith("{")&&!O.startsWith("({")||O==="}"||O==="};"||f(O)},A=(C,O)=>{let R=C,y=O.trim().replace(/;\s*$/,"");if(!y)return null;let P=y.lastIndexOf(";");if(P!==-1){let B=y.slice(P+1).trim(),j=y.slice(0,P).trim();B&&(B.startsWith("//")||B.startsWith("/*")?j&&(y=j):(j&&(R=R?`${R}
703
- ${j};`:`${j};`),y=B))}return x(y)?null:{head:R,expression:y}},w=(C,O,R)=>{let y=C.slice(0,O).join(`
704
- `),P=C.slice(O,R+1).join(`
705
- `).trim();if(!P)return null;if(!P.includes(`
706
- `)){let B=A(y,P);return B?B.head?`${B.head}
700
+ `)};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 hr(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]=hr(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??de.cachingFunction,o=this.getCacheKey(t,r);if(r&&o){let a;try{a=await r(o)}catch{}if(a!==void 0){yield{version:0,index:0,delta:a};return}}let s=await this.forward(e,t,n);if(r&&o)try{await r(o,s)}catch{}yield{version:1,index:0,delta:s}}async forward(e,t,n){let r=n?.cachingFunction??de.cachingFunction,o=this.getCacheKey(t,r);if(r&&o){let c;try{c=await r(o)}catch{}if(c!==void 0)return c}let s=Date.now();this.timingLogger?.startTiming("flow-execution");let a={},l,u;try{this.resetUsage(),this.resetTraces();let c=this.extractInputValues(t);if(this.nodeGenerators.size>0&&this.ensureProgram(),a={...c},this.flowLogger){let A=this.getExecutionPlan();this.flowLogger({name:"FlowStart",timestamp:s,inputFields:Object.keys(c),totalSteps:A.totalSteps,parallelGroups:A.parallelGroups,maxParallelism:A.maxParallelism,autoParallelEnabled:A.autoParallelEnabled})}let p=n?.tracer??this.defaultAIOptions?.tracer,d=n?.traceContext,m=d;if(p){let A=this.getExecutionPlan(),w=n?.traceLabel?`AxFlow > ${n.traceLabel}`:"AxFlow";l=p.startSpan(w,{kind:jn.SpanKind.INTERNAL,attributes:{total_steps:A.totalSteps,parallel_groups:A.parallelGroups,max_parallelism:A.maxParallelism,auto_parallel_enabled:A.autoParallelEnabled}});let b=d??jn.context.active();m=jn.trace.setSpan(b,l)}u=new AbortController,this.activeAbortControllers.add(u),this._stopRequested&&u.abort("Stopped by user (pre-forward)");let g=vt(n?.abortSignal,n?.abortController?.signal),h=vt(u.signal,vt(g,de.abortSignal)),f={mainAi:e,mainOptions:(()=>{let A={...this.defaultAIOptions??{},...n};return n?.model&&(A.model=String(n.model)),p&&(A.tracer=p),m&&(A.traceContext=m),h&&(A.abortSignal=h),Object.keys(A).length>0?A:void 0})()},I=n?.autoParallel!==!1&&this.autoParallelConfig.enabled,x=0;if(I){this.executionPlanner.setInitialFields(Object.keys(c));let A=this.executionPlanner.createOptimizedExecution(this.autoParallelConfig.batchSize),w=await this.executeStepsWithLogging(A,a,f,!0);a=w.finalState,x=w.stepsExecuted}else{let A=await this.executeStepsWithLogging(this.flowDefinition,a,f,!1);a=A.finalState,x=A.stepsExecuted}if(this.flowLogger){let A=this.timingLogger?.endTiming("flow-execution")??Date.now()-s;this.flowLogger({name:"FlowComplete",timestamp:Date.now(),totalExecutionTime:A,finalState:a,outputFields:Object.keys(a),stepsExecuted:x})}if(r&&o)try{await r(o,a)}catch{}return a}catch(c){throw this.flowLogger&&this.flowLogger({name:"FlowError",timestamp:Date.now(),error:c instanceof Error?c.message:String(c),state:a}),c}finally{l&&l.end(),u&&this.activeAbortControllers.delete(u),this._stopRequested=!1}}node(e,t){if(this.nodes.has(e)||this.nodeGenerators.has(e))throw new Error(`Node '${e}' is already defined. Use a unique node name in this flow.`);if(typeof t=="string"||t instanceof Oe){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=ze(n);this.nodeGenerators.set(e,r),this.ensureProgram(),this.program.register(r,e)}else if(typeof t=="function"){this.nodes.set(e,{inputs:{},outputs:{}});let n=new t;this.nodeGenerators.set(e,n),this.ensureProgram(),this.program.register(n,e)}else if(t&&typeof t=="object"&&"forward"in t){this.nodes.set(e,{inputs:{},outputs:{}});let n=t;this.nodeGenerators.set(e,n),this.ensureProgram(),this.program.register(n,e)}else throw new Error(`Invalid second argument for node '${e}': expected string, AxSignature, AxProgrammable instance, or constructor function`);return this}n(e,t){return this.node(e,t)}map(e,t){if(t?.parallel){let n=Array.isArray(e)?e:[e],r=async o=>(await $n(n,async(l,u)=>{let c=l(o);return Promise.resolve(c)},this.autoParallelConfig.batchSize)).reduce((l,u)=>({...l,...u}),o);if(this.branchContext?.currentBranchValue!==void 0){let o=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];o.push(r),this.branchContext.branches.set(this.branchContext.currentBranchValue,o)}else this.flowDefinition.push(r),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(r,void 0,void 0,"parallel-map",n)}else{let n=async r=>{if(Array.isArray(e))throw new Error("Array of transforms requires parallel: true option");let o=e(r);return Promise.resolve(o)};if(this.branchContext?.currentBranchValue!==void 0){let r=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];r.push(n),this.branchContext.branches.set(this.branchContext.currentBranchValue,r)}else this.flowDefinition.push(n),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(n,void 0,void 0,"map",e)}return this.nodeGenerators.size>0&&this.ensureProgram(),this}m(e,t){return this.map(e,t)}returns(e){let t=async n=>{let r=e(n);return Promise.resolve(r)};if(this.branchContext?.currentBranchValue!==void 0){let n=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];n.push(t),this.branchContext.branches.set(this.branchContext.currentBranchValue,n)}else this.flowDefinition.push(t),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(t,void 0,void 0,"map",e);return this.nodeGenerators.size>0&&this.ensureProgram(),this}r(e){return this.returns(e)}label(e){if(this.branchContext?.currentBranchValue!==void 0)throw new Error("Cannot create labels inside branch blocks");return this.stepLabels.set(e,this.flowDefinition.length),this}l(e){return this.label(e)}execute(e,t,n){if(!this.nodes.has(e))throw new Error(`Node '${e}' not found. Make sure to define it with .node() first.`);let r=this.nodeGenerators.get(e);if(!r)throw new Error(`Node program for '${e}' not found.`);let o=async(s,a)=>{let l=n?.ai??a.mainAi,u={...a.mainOptions??{},...n?.options??{}},c=t(s),p=u?.traceLabel?`Node:${e} (${u.traceLabel})`:`Node:${e}`,d;if("forward"in r&&typeof r.forward=="function"){if(d=await r.forward(l,c,{...u,traceLabel:p}),"getUsage"in r&&typeof r.getUsage=="function"){let m=r.getUsage();if(m&&m.length>0){let g=this.nodeUsage.get(e)||[];this.nodeUsage.set(e,[...g,...m])}}if("getTraces"in r&&typeof r.getTraces=="function"){let m=r.getTraces();if(m&&m.length>0){let g=this.nodeTraces.get(e)||[];this.nodeTraces.set(e,[...g,...m])}}}else throw new Error(`Node program for '${e}' does not have a forward method`);return{...s,[`${e}Result`]:d}};if(this.branchContext?.currentBranchValue!==void 0){let s=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];s.push(o),this.branchContext.branches.set(this.branchContext.currentBranchValue,s)}else this.flowDefinition.push(o),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(o,e,t);return this.ensureProgram(),this}applyOptimization(e){this.program&&"applyOptimization"in this.program&&this.program.applyOptimization(e);for(let[t,n]of Array.from(this.nodeGenerators))n&&"applyOptimization"in n&&typeof n.applyOptimization=="function"&&n.applyOptimization(e)}e(e,t,n){return this.execute(e,t,n)}branch(e){if(this.branchContext)throw new Error("Nested branches are not supported");return this.branchContext={predicate:t=>e(t),branches:new Map,currentBranchValue:void 0},this}b(e){return this.branch(e)}when(e){if(!this.branchContext)throw new Error("when() called without matching branch()");return this.branchContext.currentBranchValue=e,this.branchContext.branches.set(e,[]),this}w(e){return this.when(e)}merge(){if(!this.branchContext)throw new Error("merge() called without matching branch()");let e=this.branchContext;this.branchContext=null;let t=async(n,r)=>{let o=e.predicate(n),s=e.branches.get(o);if(this.flowLogger&&this.flowLogger({name:"BranchEvaluation",timestamp:Date.now(),branchValue:o,hasMatchingBranch:!!s,branchStepsCount:s?.length??0}),!s)return n;let a=n;for(let l of s)a=await l(a,r);return a};return this.flowDefinition.push(t),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(t,void 0,void 0,"merge"),this.ensureProgram(),this}mg(){return this.merge()}parallel(e){let t=async(n,r)=>{let o=await $n(e,async(s,a)=>{let l=new Bn(this.nodeGenerators);return await s(l).executeSteps(n,r)},this.autoParallelConfig.batchSize);return{...n,_parallelResults:o}};return this.flowDefinition.push(t),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(t,void 0,void 0,"parallel",void 0,void 0),this.ensureProgram(),{merge:(n,r)=>{let o=s=>{let a=s._parallelResults;if(!Array.isArray(a))throw new Error("No parallel results found for merge");let l=r(...a),u={...s};return delete u._parallelResults,u[n]=l,u};return this.flowDefinition.push(o),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(o,void 0,void 0,"merge",void 0,{resultKey:n,mergeFunction:r}),this.ensureProgram(),this}}}p(e){return this.parallel(e)}feedback(e,t,n=10){if(!this.stepLabels.has(t))throw new Error(`Label '${t}' not found. Make sure to define it with .label() before the feedback point.`);let r=this.stepLabels.get(t),o=this.flowDefinition.length;return this.flowDefinition.push(async(s,a)=>{let l=s,u=1,c=`_feedback_${t}_iterations`;for(typeof l[c]!="number"&&(l={...l,[c]:1});e(l)&&u<n;){u++,l={...l,[c]:u};for(let p=r;p<o;p++){let d=this.flowDefinition[p];d&&(l=await d(l,a))}}return l}),this.nodeGenerators.size>0&&this.ensureProgram(),this}fb(e,t,n=10){return this.feedback(e,t,n)}while(e,t=100){let n=this.flowDefinition.length;this.loopStack.push(n);let r=Object.assign(o=>o,{_condition:e,_maxIterations:t,_isLoopStart:!0});return this.flowDefinition.push(r),this.nodeGenerators.size>0&&this.ensureProgram(),this}wh(e,t=100){return this.while(e,t)}endWhile(){if(this.loopStack.length===0)throw new Error("endWhile() called without matching while()");let e=this.loopStack.pop(),t=this.flowDefinition[e];if(!t||!("_isLoopStart"in t))throw new Error("Loop start step not found or invalid");let n=t._condition,r=t._maxIterations,o=this.flowDefinition.splice(e+1);return this.flowDefinition[e]=async(s,a)=>{let l=s,u=0;for(;n(l)&&u<r;){u++;for(let c of o)l=await c(l,a)}if(u>=r&&n(l))throw new Error(`While loop exceeded maximum iterations (${r}). Consider increasing maxIterations or ensuring the loop condition eventually becomes false.`);return l},this.nodeGenerators.size>0&&this.ensureProgram(),this}end(){return this.endWhile()}derive(e,t,n,r){let o=async s=>{let a=s[t];if(a===void 0)throw new Error(`Input field '${t}' not found in state`);let l;if(Array.isArray(a))if(this.autoParallelConfig.enabled){let u=r?.batchSize||this.autoParallelConfig.batchSize;l=await $n(a,async(c,p)=>n(c,p,s),u)}else l=a.map((u,c)=>n(u,c,s));else l=n(a,void 0,s);return{...s,[e]:l}};if(this.branchContext?.currentBranchValue!==void 0){let s=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];s.push(o),this.branchContext.branches.set(this.branchContext.currentBranchValue,s)}else this.flowDefinition.push(o),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(o,void 0,void 0,"derive",n,void 0,{inputFieldName:t,outputFieldName:e,batchSize:r?.batchSize});return this.ensureProgram(),this}getExecutionPlan(){let e=this.executionPlanner.getExecutionPlan();return{totalSteps:e.totalSteps,parallelGroups:e.parallelGroups,maxParallelism:e.maxParallelism,autoParallelEnabled:this.autoParallelConfig.enabled,steps:e.steps,groups:e.groups}}getSignature(){return this.ensureProgram(),this.program.getSignature()}nodeExtended(e,t,n){let o=typeof t=="string"?Oe.create(t):t;if(n.prependInputs)for(let s of n.prependInputs)o=o.prependInputField(s.name,s.type);if(n.appendInputs)for(let s of n.appendInputs)o=o.appendInputField(s.name,s.type);if(n.prependOutputs)for(let s of n.prependOutputs)o=o.prependOutputField(s.name,s.type);if(n.appendOutputs)for(let s of n.appendOutputs)o=o.appendOutputField(s.name,s.type);return this.node(e,o)}nx(e,t,n){return this.nodeExtended(e,t,n)}mapOutput(e){let t=async n=>{let r=e(n);return{...n,...r}};if(this.branchContext?.currentBranchValue!==void 0){let n=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];n.push(t),this.branchContext.branches.set(this.branchContext.currentBranchValue,n)}else this.flowDefinition.push(t),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(t,void 0,void 0,"map",e);return this.nodeGenerators.size>0&&this.ensureProgram(),this}mo(e){return this.mapOutput(e)}};function ns(i){return Mr.create(i)}var rs=class{apiUrl;containerId=null;constructor(e="http://localhost:2375"){this.apiUrl=e}async pullImage(e){let t=await this.fetchDockerAPI(`/images/create?fromImage=${encodeURIComponent(e)}`,{method:"POST"});if(!t.ok)throw new Error(`Failed to pull image: ${t.statusText}`);await t.text()}async createContainer({imageName:e,volumes:t=[],doNotPullImage:n,tag:r}){let o=t.map(u=>`${u.hostPath}:${u.containerPath}`);n||await this.pullImage(e);let s={Image:e,Tty:!0,OpenStdin:!1,AttachStdin:!1,AttachStdout:!1,AttachStderr:!1,HostConfig:{Binds:o},Labels:{}};r&&(s.Labels["com.example.tag"]=r);let a=await this.fetchDockerAPI("/containers/create",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(s)});if(!a.ok)throw new Error(`Failed to create container: ${a.statusText}`);let l=await a.json();return this.containerId=l.Id,l}async findOrCreateContainer({imageName:e,volumes:t=[],doNotPullImage:n,tag:r}){let s=(await this.listContainers(!0)).filter(l=>l.Labels&&l.Labels["com.example.tag"]===r);if(s&&s.length>0){let l=Math.floor(Math.random()*s.length),u=s[l];if(u)return await this.connectToContainer(u.Id),{Id:u.Id,isNew:!1}}return{Id:(await this.createContainer({imageName:e,volumes:t,doNotPullImage:n,tag:r})).Id,isNew:!0}}async startContainer(){if(!this.containerId)throw new Error("No container created or connected");let e=await this.fetchDockerAPI(`/containers/${this.containerId}/start`,{method:"POST"});if(!e.ok)throw new Error(`Failed to start container: ${e.statusText}`)}async connectToContainer(e){let t=await this.fetchDockerAPI(`/containers/${e}/json`);if(!t.ok)throw new Error(`Failed to connect to container: ${t.statusText}`);this.containerId=e}async stopContainers({tag:e,remove:t,timeout:n=10}){let r=[],o=await this.listContainers(!0),s=e?o.filter(a=>a.Labels["com.example.tag"]===e):o;for(let a of s){if(a.State.Status==="running"){let l=await this.fetchDockerAPI(`/containers/${a.Id}/stop?t=${n}`,{method:"POST"});if(!l.ok){console.warn(`Failed to stop container ${a.Id}: ${l.statusText}`);continue}r.push({Id:a.Id,Action:"stopped"})}if(t){let l=await this.fetchDockerAPI(`/containers/${a.Id}`,{method:"DELETE"});if(!l.ok){console.warn(`Failed to remove container ${a.Id}: ${l.statusText}`);continue}r.push({Id:a.Id,Action:"removed"})}}return r}async listContainers(e=!1){return(await this.fetchDockerAPI(`/containers/json?all=${e}`,{method:"GET"})).json()}async getContainerLogs(){if(!this.containerId)throw new Error("No container created or connected");return(await this.fetchDockerAPI(`/containers/${this.containerId}/logs?stdout=true&stderr=true`,{method:"GET"})).text()}async executeCommand(e){if(!this.containerId)throw new Error("No container created or connected");(await this.getContainerInfo(this.containerId)).State.Status!=="running"&&(await this.startContainer(),await this.waitForContainerToBeRunning(this.containerId));let n=await this.fetchDockerAPI(`/containers/${this.containerId}/exec`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({Cmd:["sh","-c",e],AttachStdout:!0,AttachStderr:!0})});if(!n.ok)throw new Error(`Failed to create exec instance: ${n.statusText}`);let r=await n.json(),o=await this.fetchDockerAPI(`/exec/${r.Id}/start`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({Detach:!1,Tty:!1})});if(!o.ok)throw new Error(`Failed to start exec instance: ${o.statusText}`);return await o.text()}async getContainerInfo(e){let t=await this.fetchDockerAPI(`/containers/${e}/json`);if(!t.ok)throw new Error(`Failed to get container info: ${t.statusText}`);return t.json()}async waitForContainerToBeRunning(e,t=3e4){let n=Date.now();for(;Date.now()-n<t;){if((await this.getContainerInfo(e)).State.Status==="running")return;await new Promise(o=>setTimeout(o,1e3))}throw new Error("Timeout waiting for container to start")}async fetchDockerAPI(e,t){let n=new URL(e,this.apiUrl).toString();return await fetch(n,t)}toFunction(){return{name:"commandExecution",description:"Use this function to execute shell commands, scripts, and programs. This function enables interaction with the file system, running system utilities, and performing tasks that require a shell interface.",parameters:{type:"object",properties:{command:{type:"string",description:'Shell command to execute. eg. `ls -l` or `echo "Hello, World!"`.'}},required:["command"]},func:async({command:e})=>await this.executeCommand(e)}}};var os=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 ss(i){let e=typeof self<"u"?self:globalThis,t=Object.getPrototypeOf(async()=>{}).constructor,n=i.functionRefKey,r="return",o="stdout",s=/^(if|for|while|switch|try|catch|finally|function|class|import|export|throw|return|var|let|const|break|continue|debugger)\b/,a=/^\s*return\s+([^\n;]+?)\s*;?\s*$/,l={network:["fetch","XMLHttpRequest","WebSocket","EventSource"],storage:["indexedDB","caches"],"code-loading":["importScripts"],communication:["BroadcastChannel"],timing:["performance"],workers:["Worker","SharedWorker"]},u=()=>{let C;if(typeof process<"u"&&typeof process.getBuiltinModule=="function"&&(C=process.getBuiltinModule.bind(process)),!C)try{C=new Function('return typeof require==="function"?require:undefined')()}catch{C=void 0}if(!(typeof C=="function"&&typeof process<"u"&&!!process.versions?.node))return{isNodeWorker:!1,parentPort:null};try{return{isNodeWorker:!0,parentPort:C("node:worker_threads").parentPort??null}}catch{return{isNodeWorker:!0,parentPort:null}}},{isNodeWorker:c,parentPort:p}=u(),d=()=>{if(!p&&typeof e.postMessage!="function")throw new Error("Worker transport unavailable: no postMessage channel");return{send:R=>{if(p){p.postMessage(R);return}e.postMessage(R)},setOnMessage:R=>{if(p){p.on("message",y=>R({data:y}));return}e.onmessage=R}}},{send:m,setOnMessage:g}=d(),h=C=>C&&(/\r?\n$/.test(C)?C:`${C}
701
+ `),f=C=>{let k=C.trim();return k.startsWith("//")||k.startsWith("/*")||k.startsWith("*")},I=C=>{let k=C.length-1;for(;k>=0;){let R=C[k].trim();if(R&&!f(R))break;k-=1}return k},x=C=>{if(!C)return!0;let k=C.split(`
702
+ `).find(R=>R.trim().length>0)?.trim();return!k||s.test(k)?!0:k.startsWith("{")&&!k.startsWith("({")||k==="}"||k==="};"||f(k)},A=(C,k)=>{let R=C,y=k.trim().replace(/;\s*$/,"");if(!y)return null;let E=y.lastIndexOf(";");if(E!==-1){let U=y.slice(E+1).trim(),j=y.slice(0,E).trim();U&&(U.startsWith("//")||U.startsWith("/*")?j&&(y=j):(j&&(R=R?`${R}
703
+ ${j};`:`${j};`),y=U))}return x(y)?null:{head:R,expression:y}},w=(C,k,R)=>{let y=C.slice(0,k).join(`
704
+ `),E=C.slice(k,R+1).join(`
705
+ `).trim();if(!E)return null;if(!E.includes(`
706
+ `)){let U=A(y,E);return U?U.head?`${U.head}
707
707
  return (
708
- ${B.expression}
708
+ ${U.expression}
709
709
  );`:`return (
710
- ${B.expression}
711
- );`:null}return x(P)?null:y?`${y}
710
+ ${U.expression}
711
+ );`:null}return x(E)?null:y?`${y}
712
712
  return (
713
- ${P}
713
+ ${E}
714
714
  );`:`return (
715
- ${P}
716
- );`},I=C=>{try{return new t(C),!0}catch{return!1}},T=C=>{let O=C.split(`
717
- `),R=b(O);if(R<0)return C;let y=new Set;for(let P=R;P>=0;P-=1){let B=O[P]??"";if(!B.trim()||f(B))continue;let j=w(O,P,R);if(j&&!y.has(j)&&(y.add(j),I(j)))return j}return C},v=C=>{let O=a.exec(C);return O&&(O[1]||"").trim()||C},F=C=>{let O=[],R=C.length,y=0,P=0,B=0,j=S=>S>="a"&&S<="z"||S>="A"&&S<="Z"||S>="0"&&S<="9"||S==="_"||S==="$",H=S=>{if(y++,S==="`"){let G=0;for(;y<R;){let Z=C[y];if(Z==="\\"){y+=2;continue}if(G>0){Z==="{"?G++:Z==="}"&&G--,y++;continue}if(Z==="$"&&y+1<R&&C[y+1]==="{"){G++,y+=2;continue}if(Z==="`"){y++;return}y++}}else for(;y<R;){let G=C[y];if(G==="\\"){y+=2;continue}if(G===S){y++;return}y++}},q=()=>{for(y+=2;y<R&&C[y]!==`
718
- `;)y++},K=()=>{for(y+=2;y<R;){if(C[y]==="*"&&y+1<R&&C[y+1]==="/"){y+=2;return}y++}},z=()=>{let S=y;for(;y<R&&j(C[y]);)y++;return C.slice(S,y)},W=()=>{let S=y;for(;y<R;){let G=C[y];if(G===" "||G===" "||G===`
719
- `||G==="\r"){y++;continue}if(G==="/"&&y+1<R){if(C[y+1]==="/"){q();continue}if(C[y+1]==="*"){K();continue}}break}return y>S},ee=S=>{let G=1;for(;y<R&&G>0;){if(W(),y>=R)return;let Z=C[y];if(Z===S){G--,y++;continue}if(Z==="{"||Z==="["){let he=Z==="{"?"}":"]";y++,G++,y--,G--,y++,ee(he);continue}if(Z==="."&&y+2<R&&C[y+1]==="."&&C[y+2]==="."){if(y+=3,W(),y<R&&j(C[y])){let he=z();he&&O.push(he)}continue}if(Z===","){y++;continue}if(Z==="="){y++;let he=0;for(;y<R;){let ue=C[y];if(ue==="'"||ue==='"'||ue==="`"){H(ue);continue}if(ue==="("||ue==="["||ue==="{"){he++,y++;continue}if(ue===")"||ue==="]"||ue==="}"){if(he>0){he--,y++;continue}break}if(ue===","&&he===0)break;y++}continue}if(j(Z)){let he=z();if(W(),y<R&&C[y]===":"){if(y++,W(),y<R){let ue=C[y];if(ue==="{"||ue==="["){let ae=ue==="{"?"}":"]";y++,ee(ae)}else if(j(ue)){let ae=z();ae&&O.push(ae)}}}else he&&O.push(he);continue}y++}},ie=()=>{let S=0;for(;y<R;){let G=C[y];if(G==="'"||G==='"'||G==="`"){H(G);continue}if(G==="/"&&y+1<R){if(C[y+1]==="/"){q();continue}if(C[y+1]==="*"){K();continue}}if(G==="("||G==="["||G==="{"){S++,y++;continue}if(G===")"||G==="]"||G==="}"){if(S>0){S--,y++;continue}return!1}if(G===","&&S===0)return y++,!0;if(G===";"&&S===0)return y++,!1;if(G===`
720
- `&&S===0){let Z=y;return y++,W(),y<R&&C[y]===","?(y++,!0):(y=Z,!1)}y++}return!1},de=()=>{for(;y<R;){if(W(),y>=R)return;let S=C[y];if(S==="{"){if(y++,ee("}"),!ie())return;continue}if(S==="["){if(y++,ee("]"),!ie())return;continue}if(j(S)){let G=z();if(G&&O.push(G),!ie())return;continue}return}},fe=S=>{if(S===0)return!0;let G=S-1;for(;G>=0;){let Z=C[G];if(Z===" "||Z===" "||Z==="\r"){G--;continue}return Z===`
721
- `||Z===";"||Z==="{"||Z==="}"}return!0};for(;y<R;){let S=C[y];if(S==="'"||S==='"'||S==="`"){H(S);continue}if(S==="/"&&y+1<R){if(C[y+1]==="/"){q();continue}if(C[y+1]==="*"){K();continue}}if(S==="{"){P++,y++;continue}if(S==="}"){P--,y++;continue}if(S==="("){B++,y++;continue}if(S===")"){B--,y++;continue}if(P===0&&B===0&&j(S)){let G=y,Z=z();if((Z==="var"||Z==="let"||Z==="const")&&y<R&&(C[y]===" "||C[y]===" "||C[y]===`
722
- `)&&fe(G)){de();continue}continue}y++}let Ae=new Set,ye=[];for(let S of O)Ae.has(S)||(Ae.add(S),ye.push(S));return ye},E=C=>C.length===0?"":`
723
- try { ${C.map(R=>`globalThis[${JSON.stringify(R)}] = ${R};`).join(" ")} } catch (_ax_e) {} void 0;`,D=C=>{if(typeof C=="string")return C;try{return JSON.stringify(C,null,2)}catch{return String(C)}},U=(C,O)=>{let R=e.console&&typeof e.console=="object"?e.console:null,y=R?.[C],P=typeof y=="function"?(...j)=>y.apply(R,j):null,B=(...j)=>{O.push(j.map(D).join(" "))};return(!e.console||typeof e.console!="object")&&(e.console={}),e.console[C]=B,()=>{if(!(!e.console||typeof e.console!="object")){if(P){e.console[C]=P;return}try{delete e.console[C]}catch{e.console[C]=void 0}}}},V=C=>{let O=[];return oe&&(O.push(U("log",C)),O.push(U("info",C)),O.push(U("warn",C)),O.push(U("error",C))),O},k=()=>{let C=[],O=V(C),R=e.print;return J===o&&(e.print=(...P)=>{C.push(P.map(D).join(" "))}),{output:C,cleanup:()=>{for(let P of O)try{P()}catch{}if(J===o)if(R===void 0)try{delete e.print}catch{e.print=void 0}else e.print=R}}},$=C=>{for(let O of C)try{Object.defineProperty(e,O,{value:void 0,writable:!1,configurable:!1})}catch{}},N=C=>{let O=new Set(Array.isArray(C)?C:[]);for(let[R,y]of Object.entries(l))O.has(R)||$(y)},Q=C=>{c&&!C&&$(["process","require"])},M=i.maxErrorCauseDepth,L=(C,O=0,R=new Set)=>{if(O>M)return{name:"Error",message:"[cause chain truncated]"};if(C&&typeof C=="object"){if(R.has(C))return{name:"Error",message:"[circular]"};R.add(C)}let y=C,P=y?.name!=null?String(y.name):"Error",B=y?.message!=null?String(y.message):X(C),j=typeof y?.stack=="string"?y.stack:void 0,H;if(typeof y?.cause<"u"&&O<M)try{let K=y.cause;K instanceof Error||K&&typeof K=="object"&&("message"in K||"name"in K)?H=L(K,O+1,R):H={name:"Error",message:X(K)}}catch{H={name:"Error",message:X(y.cause)}}let q={name:P,message:B};if(j!==void 0&&(q.stack=j),H!==void 0&&(q.cause=H),typeof y?.data<"u")try{q.data=typeof structuredClone=="function"?structuredClone(y.data):y.data}catch{}return q},Y=C=>{if(typeof C=="string")return new Error(C);if(!C||typeof C!="object")return new Error(String(C));let O=C,R=new Error(O.message!=null?String(O.message):"");return R.name=O.name!=null?String(O.name):"Error",typeof O.stack=="string"&&(R.stack=O.stack),O.cause!==void 0&&(R.cause=Y(O.cause)),O.data!==void 0&&(R.data=O.data),R},_=C=>{let O=globalThis.AggregateError,R=typeof O=="function"&&C instanceof O;return C instanceof SyntaxError||C instanceof TypeError||C instanceof RangeError||C instanceof ReferenceError||R||C instanceof EvalError||C instanceof URIError},X=C=>{if(C==null||typeof C!="object")return String(C);try{return JSON.stringify(C,null,2)}catch{return String(C)}},xe=(C,O,R=0)=>{let y=C,P=y?.name!=null?String(y.name):"Error",B=y?.message!=null?String(y.message):X(C),j=[`${P}: ${B}`];if(typeof y?.stack=="string"){let H=y.stack.match(/<anonymous>:(\d+):(\d+)/);if(H){let q=Math.max(1,Number(H[1])-R);j.push(` at line ${q}, column ${H[2]}`)}}if(O){let H=O.split(`
724
- `),q=30,z=(H.length<=q?H:[...H.slice(0,q),`... (${H.length-q} more lines)`]).map((W,ee)=>` ${String(ee+1).padStart(3)}| ${W}`).join(`
715
+ ${E}
716
+ );`},b=C=>{try{return new t(C),!0}catch{return!1}},T=C=>{let k=C.split(`
717
+ `),R=I(k);if(R<0)return C;let y=new Set;for(let E=R;E>=0;E-=1){let U=k[E]??"";if(!U.trim()||f(U))continue;let j=w(k,E,R);if(j&&!y.has(j)&&(y.add(j),b(j)))return j}return C},v=C=>{let k=a.exec(C);return k&&(k[1]||"").trim()||C},F=C=>{let k=[],R=C.length,y=0,E=0,U=0,j=$=>$>="a"&&$<="z"||$>="A"&&$<="Z"||$>="0"&&$<="9"||$==="_"||$==="$",K=$=>{if(y++,$==="`"){let V=0;for(;y<R;){let ne=C[y];if(ne==="\\"){y+=2;continue}if(V>0){ne==="{"?V++:ne==="}"&&V--,y++;continue}if(ne==="$"&&y+1<R&&C[y+1]==="{"){V++,y+=2;continue}if(ne==="`"){y++;return}y++}}else for(;y<R;){let V=C[y];if(V==="\\"){y+=2;continue}if(V===$){y++;return}y++}},q=()=>{for(y+=2;y<R&&C[y]!==`
718
+ `;)y++},H=()=>{for(y+=2;y<R;){if(C[y]==="*"&&y+1<R&&C[y+1]==="/"){y+=2;return}y++}},z=()=>{let $=y;for(;y<R&&j(C[y]);)y++;return C.slice($,y)},W=()=>{let $=y;for(;y<R;){let V=C[y];if(V===" "||V===" "||V===`
719
+ `||V==="\r"){y++;continue}if(V==="/"&&y+1<R){if(C[y+1]==="/"){q();continue}if(C[y+1]==="*"){H();continue}}break}return y>$},ee=$=>{let V=1;for(;y<R&&V>0;){if(W(),y>=R)return;let ne=C[y];if(ne===$){V--,y++;continue}if(ne==="{"||ne==="["){let he=ne==="{"?"}":"]";y++,V++,y--,V--,y++,ee(he);continue}if(ne==="."&&y+2<R&&C[y+1]==="."&&C[y+2]==="."){if(y+=3,W(),y<R&&j(C[y])){let he=z();he&&k.push(he)}continue}if(ne===","){y++;continue}if(ne==="="){y++;let he=0;for(;y<R;){let pe=C[y];if(pe==="'"||pe==='"'||pe==="`"){K(pe);continue}if(pe==="("||pe==="["||pe==="{"){he++,y++;continue}if(pe===")"||pe==="]"||pe==="}"){if(he>0){he--,y++;continue}break}if(pe===","&&he===0)break;y++}continue}if(j(ne)){let he=z();if(W(),y<R&&C[y]===":"){if(y++,W(),y<R){let pe=C[y];if(pe==="{"||pe==="["){let ie=pe==="{"?"}":"]";y++,ee(ie)}else if(j(pe)){let ie=z();ie&&k.push(ie)}}}else he&&k.push(he);continue}y++}},ce=()=>{let $=0;for(;y<R;){let V=C[y];if(V==="'"||V==='"'||V==="`"){K(V);continue}if(V==="/"&&y+1<R){if(C[y+1]==="/"){q();continue}if(C[y+1]==="*"){H();continue}}if(V==="("||V==="["||V==="{"){$++,y++;continue}if(V===")"||V==="]"||V==="}"){if($>0){$--,y++;continue}return!1}if(V===","&&$===0)return y++,!0;if(V===";"&&$===0)return y++,!1;if(V===`
720
+ `&&$===0){let ne=y;return y++,W(),y<R&&C[y]===","?(y++,!0):(y=ne,!1)}y++}return!1},ae=()=>{for(;y<R;){if(W(),y>=R)return;let $=C[y];if($==="{"){if(y++,ee("}"),!ce())return;continue}if($==="["){if(y++,ee("]"),!ce())return;continue}if(j($)){let V=z();if(V&&k.push(V),!ce())return;continue}return}},xe=$=>{if($===0)return!0;let V=$-1;for(;V>=0;){let ne=C[V];if(ne===" "||ne===" "||ne==="\r"){V--;continue}return ne===`
721
+ `||ne===";"||ne==="{"||ne==="}"}return!0};for(;y<R;){let $=C[y];if($==="'"||$==='"'||$==="`"){K($);continue}if($==="/"&&y+1<R){if(C[y+1]==="/"){q();continue}if(C[y+1]==="*"){H();continue}}if($==="{"){E++,y++;continue}if($==="}"){E--,y++;continue}if($==="("){U++,y++;continue}if($===")"){U--,y++;continue}if(E===0&&U===0&&j($)){let V=y,ne=z();if((ne==="var"||ne==="let"||ne==="const")&&y<R&&(C[y]===" "||C[y]===" "||C[y]===`
722
+ `)&&xe(V)){ae();continue}continue}y++}let ye=new Set,P=[];for(let $ of k)ye.has($)||(ye.add($),P.push($));return P},O=C=>C.length===0?"":`
723
+ try { ${C.map(R=>`globalThis[${JSON.stringify(R)}] = ${R};`).join(" ")} } catch (_ax_e) {} void 0;`,D=C=>{if(typeof C=="string")return C;try{return JSON.stringify(C,null,2)}catch{return String(C)}},B=(C,k)=>{let R=e.console&&typeof e.console=="object"?e.console:null,y=R?.[C],E=typeof y=="function"?(...j)=>y.apply(R,j):null,U=(...j)=>{k.push(j.map(D).join(" "))};return(!e.console||typeof e.console!="object")&&(e.console={}),e.console[C]=U,()=>{if(!(!e.console||typeof e.console!="object")){if(E){e.console[C]=E;return}try{delete e.console[C]}catch{e.console[C]=void 0}}}},Q=C=>{let k=[];return se&&(k.push(B("log",C)),k.push(B("info",C)),k.push(B("warn",C)),k.push(B("error",C))),k},S=()=>{let C=[],k=Q(C),R=e.print;return J===o&&(e.print=(...E)=>{C.push(E.map(D).join(" "))}),{output:C,cleanup:()=>{for(let E of k)try{E()}catch{}if(J===o)if(R===void 0)try{delete e.print}catch{e.print=void 0}else e.print=R}}},G=C=>{for(let k of C)try{Object.defineProperty(e,k,{value:void 0,writable:!1,configurable:!1})}catch{}},N=C=>{let k=new Set(Array.isArray(C)?C:[]);for(let[R,y]of Object.entries(l))k.has(R)||G(y)},Y=C=>{c&&!C&&G(["process","require"])},M=i.maxErrorCauseDepth,L=(C,k=0,R=new Set)=>{if(k>M)return{name:"Error",message:"[cause chain truncated]"};if(C&&typeof C=="object"){if(R.has(C))return{name:"Error",message:"[circular]"};R.add(C)}let y=C,E=y?.name!=null?String(y.name):"Error",U=y?.message!=null?String(y.message):Z(C),j=typeof y?.stack=="string"?y.stack:void 0,K;if(typeof y?.cause<"u"&&k<M)try{let H=y.cause;H instanceof Error||H&&typeof H=="object"&&("message"in H||"name"in H)?K=L(H,k+1,R):K={name:"Error",message:Z(H)}}catch{K={name:"Error",message:Z(y.cause)}}let q={name:E,message:U};if(j!==void 0&&(q.stack=j),K!==void 0&&(q.cause=K),typeof y?.data<"u")try{q.data=typeof structuredClone=="function"?structuredClone(y.data):y.data}catch{}return q},X=C=>{if(typeof C=="string")return new Error(C);if(!C||typeof C!="object")return new Error(String(C));let k=C,R=new Error(k.message!=null?String(k.message):"");return R.name=k.name!=null?String(k.name):"Error",typeof k.stack=="string"&&(R.stack=k.stack),k.cause!==void 0&&(R.cause=X(k.cause)),k.data!==void 0&&(R.data=k.data),R},_=C=>{let k=globalThis.AggregateError,R=typeof k=="function"&&C instanceof k;return C instanceof SyntaxError||C instanceof TypeError||C instanceof RangeError||C instanceof ReferenceError||R||C instanceof EvalError||C instanceof URIError},Z=C=>{if(C==null||typeof C!="object")return String(C);try{return JSON.stringify(C,null,2)}catch{return String(C)}},Ae=(C,k,R=0)=>{let y=C,E=y?.name!=null?String(y.name):"Error",U=y?.message!=null?String(y.message):Z(C),j=[`${E}: ${U}`],K;if(typeof y?.stack=="string"){let q=y.stack.match(/<anonymous>:(\d+):(\d+)/);q&&(K=Math.max(1,Number(q[1])-R),j.push(` at line ${K}, column ${q[2]}`))}if(k&&K!==void 0&&K>=1&&K<=k.split(`
724
+ `).length){let q=k.split(`
725
+ `),H=Math.max(0,K-2),z=Math.min(q.length,K+1),ee=q.slice(H,z).map((ce,ae)=>` ${String(H+ae+1).padStart(3)}| ${ce}`).join(`
725
726
  `);j.push(`Source:
726
- ${z}`)}if(y?.data!==void 0&&j.push(`Data: ${X(y.data)}`),y?.cause!==void 0){let H=(q,K)=>{if(K>4)return"[cause chain truncated]";let z=q,W=z?.name!=null?String(z.name):"Error",ee=z?.message!=null?String(z.message):X(q),ie=[`${W}: ${ee}`];return z?.data!==void 0&&ie.push(`Data: ${X(z.data)}`),z?.cause!==void 0&&ie.push(`Caused by: ${H(z.cause,K+1)}`),ie.join(`
727
- `)};j.push(`Caused by: ${H(y.cause,1)}`)}return j.join(`
728
- `)},ne=new Map,re=0,J=r,oe=!1,Pe=C=>(...O)=>{let R=++re;return new Promise((y,P)=>{ne.set(R,{resolve:y,reject:P}),m({type:"fn-call",id:R,name:C,args:O})})},se=C=>{if(!C||typeof C!="object")return C;if(Array.isArray(C)){for(let R=0;R<C.length;R+=1)C[R]=se(C[R]);return C}let O=C;if(n in O){let R=O[n];return typeof R=="string"?Pe(R):void 0}for(let[R,y]of Object.entries(O))O[R]=se(y);return C},Te=C=>{if(C.globals&&typeof C.globals=="object")for(let[O,R]of Object.entries(C.globals))e[O]=se(R);if(Array.isArray(C.fnNames))for(let O of C.fnNames)typeof O=="string"&&(e[O]=Pe(O))},et=async C=>{let O=h(C),R=[];try{R=F(C)}catch{R=[]}let y=E(R),P=O;try{P=T(O)}catch{P=O}let B=P;if(y){let q=P.lastIndexOf(`
729
- return (`);q!==-1?B=P.slice(0,q)+y+P.slice(q):B=P+y}let j=I(B)?B:I(P)?P:O;return await new t(j)()},_e=C=>{let O=v(C),R=[];try{R=F(C)}catch{R=[]}let y=E(R),P=y?O+y:O;return(0,eval)(P)},Ge=(C,O)=>{if(J!==o)return C;let R=O.join(`
730
- `).trim();return R||C};g(async C=>{let O=C.data;if(!O||typeof O!="object"||typeof O.type!="string")return;if(O.type==="init"){J=O.outputMode===o?o:r,oe=O.captureConsole!==void 0?!!O.captureConsole:J===o;let H=O.allowUnsafeNodeHostAccess===!0;Te(O),N(O.permissions),Q(H);return}if(O.type==="fn-result"){if(typeof O.id!="number")return;let H=ne.get(O.id);H&&(ne.delete(O.id),O.error!==void 0?H.reject(Y(O.error)):H.resolve(O.value));return}if(O.type!=="execute"||typeof O.id!="number"||typeof O.code!="string")return;let R=O.id,y=O.code,P=/\bawait\b/.test(y),{output:B,cleanup:j}=k();try{let H=P?await et(y):_e(y),q=Ge(H,B);try{m({type:"result",id:R,value:q})}catch{m({type:"result",id:R,value:String(q)})}}catch(H){_(H)?m({type:"result",id:R,value:xe(H,y,P?2:0)}):m({type:"result",id:R,error:L(H)})}finally{j()}})}var Ei=4,mu=16,Pi="__ax_rlm_fn_ref__",Fi=16;function gu(){let i={functionRefKey:Pi,maxErrorCauseDepth:Fi},e=`(${ss.toString()})(${JSON.stringify(i)});
727
+ ${ee}`)}if(y?.data!==void 0&&j.push(`Data: ${Z(y.data)}`),y?.cause!==void 0){let q=(H,z)=>{if(z>4)return"[cause chain truncated]";let W=H,ee=W?.name!=null?String(W.name):"Error",ce=W?.message!=null?String(W.message):Z(H),ae=[`${ee}: ${ce}`];return W?.data!==void 0&&ae.push(`Data: ${Z(W.data)}`),W?.cause!==void 0&&ae.push(`Caused by: ${q(W.cause,z+1)}`),ae.join(`
728
+ `)};j.push(`Caused by: ${q(y.cause,1)}`)}return j.join(`
729
+ `)},re=new Map,oe=0,J=r,se=!1,Ee=C=>(...k)=>{let R=++oe;return new Promise((y,E)=>{re.set(R,{resolve:y,reject:E}),m({type:"fn-call",id:R,name:C,args:k})})},le=C=>{if(!C||typeof C!="object")return C;if(Array.isArray(C)){for(let R=0;R<C.length;R+=1)C[R]=le(C[R]);return C}let k=C;if(n in k){let R=k[n];return typeof R=="string"?Ee(R):void 0}for(let[R,y]of Object.entries(k))k[R]=le(y);return C},Ce=C=>{if(C.globals&&typeof C.globals=="object")for(let[k,R]of Object.entries(C.globals))e[k]=le(R);if(Array.isArray(C.fnNames))for(let k of C.fnNames)typeof k=="string"&&(e[k]=Ee(k))},Xe=async C=>{let k=h(C),R=[];try{R=F(C)}catch{R=[]}let y=O(R),E=k;try{E=T(k)}catch{E=k}let U=E;if(y){let q=E.lastIndexOf(`
730
+ return (`);q!==-1?U=E.slice(0,q)+y+E.slice(q):U=E+y}let j=b(U)?U:b(E)?E:k;return await new t(j)()},_e=C=>{let k=v(C),R=[];try{R=F(C)}catch{R=[]}let y=O(R),E=y?k+y:k;return(0,eval)(E)},$e=(C,k)=>{if(J!==o)return C;let R=k.join(`
731
+ `).trim();return R||C};g(async C=>{let k=C.data;if(!k||typeof k!="object"||typeof k.type!="string")return;if(k.type==="init"){J=k.outputMode===o?o:r,se=k.captureConsole!==void 0?!!k.captureConsole:J===o;let K=k.allowUnsafeNodeHostAccess===!0;Ce(k),N(k.permissions),Y(K);return}if(k.type==="fn-result"){if(typeof k.id!="number")return;let K=re.get(k.id);K&&(re.delete(k.id),k.error!==void 0?K.reject(X(k.error)):K.resolve(k.value));return}if(k.type!=="execute"||typeof k.id!="number"||typeof k.code!="string")return;let R=k.id,y=k.code,E=/\bawait\b/.test(y),{output:U,cleanup:j}=S();try{let K=E?await Xe(y):_e(y),q=$e(K,U);try{m({type:"result",id:R,value:q})}catch{m({type:"result",id:R,value:String(q)})}}catch(K){_(K)?m({type:"result",id:R,value:Ae(K,y,E?2:0)}):m({type:"result",id:R,error:L(K)})}finally{j()}})}var Ei=4,mu=16,Pi="__ax_rlm_fn_ref__",Fi=16;function gu(){let i={functionRefKey:Pi,maxErrorCauseDepth:Fi},e=`(${ss.toString()})(${JSON.stringify(i)});
731
732
  `,t="";return e.includes("__name")&&(t+=`var __name=(fn,_n)=>fn;
732
- `),t?`${t}${e}`:e}var kp=()=>typeof Worker<"u"&&typeof Blob<"u"&&typeof URL<"u"&&typeof URL.createObjectURL=="function",is=()=>typeof process<"u"&&!!process.versions?.node,Op=()=>!!globalThis.Deno?.version?.deno,Mp=()=>globalThis.Deno?.version?.deno??null,Ep=i=>{let e=/^(\d+)\.(\d+)\.(\d+)$/.exec(i.trim());return e?{major:Number(e[1]),minor:Number(e[2]),patch:Number(e[3])}:null},hu=i=>Number.isFinite(i)?Math.max(1,Math.min(mu,Math.floor(i))):Ei,Pp=()=>{if(!is())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},Fp=i=>{if(i!==void 0)return hu(i);let e=Pp();return e?hu(Math.ceil(e/2)):Ei},_p=i=>i?.debugNodeWorkerPool?!0:(globalThis.process?.env??{}).AX_RLM_DEBUG_NODE_POOL==="1",Np=i=>{let e=new Set(i),t={};return e.has("network")&&(t.net=!0),Object.keys(t).length>0?t:"none"},Lp=(i,e)=>{let t=Mp(),n=t?Ep(t):null;if(n?n.major>=1:!0)try{return new Worker(i,{type:"module",deno:{permissions:Np(e)}})}catch{}return new Worker(i,{type:"module"})},$p=(i,e)=>{let t=new Blob([i],{type:"application/javascript"}),n=URL.createObjectURL(t),r=Op()?Lp(n,e):new Worker(n),o=!1,s=()=>{o||(URL.revokeObjectURL(n),o=!0)},a={postMessage:l=>r.postMessage(l),terminate:()=>{r.terminate(),s()},onmessage:null,onerror:null};return r.onmessage=l=>{a.onmessage?.({data:l.data})},r.onerror=l=>{a.onerror?.(new Error(l.message||"Worker error"))},a},Ni=async i=>{let e="node:worker_threads",{Worker:t}=await import(e),n=new t(i,{eval:!0}),r=null,o=null,s=!1;n.on("error",l=>{o?o(l):r=l}),n.on("exit",l=>{if(s=!0,l!==0&&!r){let u=new Error(`Worker exited with code ${l}`);o?o(u):r=u}}),n.on("message",l=>{a.onmessage?.({data:l})});let a={postMessage:l=>n.postMessage(l),terminate:()=>{n.terminate()},onmessage:null,get onerror(){return o},set onerror(l){if(o=l,l&&r){let u=r;r=null,l(u)}},get exited(){return s}};return a},Li=class{source;maxSize;idle=[];pendingCreates=0;constructor(e,t){this.source=e,this.maxSize=t}warm(){if(is())for(;this.idle.length+this.pendingCreates<this.maxSize;)this.pendingCreates+=1,Ni(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(),Ni(this.source)}release(e){e.onmessage=null,e.onerror=null,e.terminate(),this.warm()}},fu=new Map,Gp=(i,e)=>`${e}:${i}`,Dp=(i,e)=>{let t=Gp(i,e),n=fu.get(t);if(n)return n;let r=new Li(i,e);return fu.set(t,r),r},Up=i=>{let e={},t=new Map,n=0,r=new WeakMap,o=(s,a)=>{if(typeof s=="function"){let d=`fn_${++n}_${a||"root"}`;return t.set(d,s),{[Pi]:d}}if(!s||typeof s!="object")return s;if(r.has(s))return r.get(s);if(Array.isArray(s)){let d=[];r.set(s,d);for(let m=0;m<s.length;m+=1)d[m]=o(s[m],`${a}[${m}]`);return d}let l=Object.getPrototypeOf(s);if(!(l===Object.prototype||l===null))return s;let c=s,p={};r.set(s,p);for(let[d,m]of Object.entries(c))p[d]=o(m,a?`${a}.${d}`:d);return p};if(i)for(let[s,a]of Object.entries(i))e[s]=o(a,s);return{serializableGlobals:e,fnMap:t}},Bp=i=>{if(typeof structuredClone=="function")try{structuredClone(i)}catch(e){let t=e instanceof Error?e.message:String(e);throw new Error(`RLM globals must be structured-cloneable: ${t}`)}};function _i(i){if(i==null||typeof i!="object")return String(i);try{return JSON.stringify(i,null,2)}catch{return String(i)}}function xu(i,e=Fi,t=0,n=new Set){if(t>e)return{name:"Error",message:"[cause chain truncated]"};if(i!==null&&typeof i=="object"){if(n.has(i))return{name:"Error",message:"[circular]"};n.add(i)}let r=i!==null&&typeof i=="object"&&i.name!=null?String(i.name):"Error",o=i!==null&&typeof i=="object"&&i.message!=null?String(i.message):_i(i),s=i!==null&&typeof i=="object"&&typeof i.stack=="string"?i.stack:void 0,a,l=i;if(l&&typeof l=="object"&&l.cause!==void 0&&t<e)try{let p=l.cause;p instanceof Error||p!==null&&typeof p=="object"&&("message"in p||"name"in p)?a=xu(p,e,t+1,n):a={name:"Error",message:_i(p)}}catch{a={name:"Error",message:_i(l.cause)}}let u={name:r,message:o};s!==void 0&&(u.stack=s),a!==void 0&&(u.cause=a);let c=i;if(c&&typeof c=="object"&&"data"in c&&c.data!==void 0)try{typeof structuredClone=="function"?u.data=structuredClone(c.data):u.data=c.data}catch{}return u}function Au(i){if(typeof i=="string")return new Error(i);if(!i||typeof i!="object")return new Error(String(i));let e=i.message!=null?String(i.message):"Unknown error",t=new Error(e);return t.name=i.name!=null?String(i.name):"Error",typeof i.stack=="string"&&(t.stack=i.stack),i.cause!==void 0&&(t.cause=Au(i.cause)),i.data!==void 0&&(t.data=i.data),t}var $i=(s=>(s.NETWORK="network",s.STORAGE="storage",s.CODE_LOADING="code-loading",s.COMMUNICATION="communication",s.TIMING="timing",s.WORKERS="workers",s))($i||{}),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=Fp(e?.nodeWorkerPoolSize),this.debugNodeWorkerPool=_p(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(`
733
- `)}createSession(e){let t=gu(),n=is()?Dp(t,this.nodeWorkerPoolSize):null;n&&this.debugNodeWorkerPool&&console.debug(`[AxJSRuntime] Node worker pool size: ${this.nodeWorkerPoolSize}`),n?.warm();let r=null,o=null,s=null,a=!1,l=this.timeout,{serializableGlobals:u,fnMap:c}=Up(e);Bp(u);let p=new Map,d=0,m=b=>{let x=b.data;if(!x||typeof x!="object"||!("type"in x)||x.type===void 0)return;let A=x;if(A.type==="result"){if(typeof A.id!="number")return;let w=p.get(A.id);w&&(p.delete(A.id),A.error!==void 0?w.reject(Au(A.error)):w.resolve(A.value));return}if(A.type==="fn-call"){if(typeof A.id!="number"||typeof A.name!="string")return;let w=c.get(A.name);if(!w){r?.postMessage({type:"fn-result",id:A.id,error:`Function "${A.name}" not found`});return}Promise.resolve().then(()=>w(...A.args??[])).then(I=>{r?.postMessage({type:"fn-result",id:A.id,value:I})}).catch(I=>{r?.postMessage({type:"fn-result",id:A.id,error:xu(I)})})}},g=()=>{a=!0,r&&(o==="node"&&n?n.release(r):r.terminate(),r=null,o=null);for(let b of p.values())b.reject(new Error("Worker terminated"));p.clear()},h=b=>{r&&(o==="node"&&n?n.release(r):r.terminate(),r=null,o=null);for(let x of p.values())x.reject(b);p.clear()};if(kp()){r=$p(t,this.permissions),o="browser",r.onmessage=m,r.onerror=h;try{r.postMessage({type:"init",globals:u,fnNames:[...c.keys()],permissions:[...this.permissions],allowUnsafeNodeHostAccess:this.allowUnsafeNodeHostAccess,outputMode:this.outputMode,captureConsole:this.captureConsole})}catch(b){throw g(),b}}let f=async()=>{if(!r){if(a)throw new Error("Session is closed");if(!is())throw new Error("No worker runtime available: Web Worker is unavailable in this environment");s||(s=(n?n.acquire():Ni(t)).then(b=>{if(a)throw n?n.release(b):b.terminate(),new Error("Session is closed");r=b,o="node",r.onmessage=m,r.onerror=h;try{r.postMessage({type:"init",globals:u,fnNames:[...c.keys()],permissions:[...this.permissions],allowUnsafeNodeHostAccess:this.allowUnsafeNodeHostAccess,outputMode:this.outputMode,captureConsole:this.captureConsole})}catch(x){throw n?n.release(b):b.terminate(),r=null,o=null,x}})),await s}};return{execute(b,x){if(a)return Promise.reject(new Error("Session is closed"));if(/['"]use strict['"]/.test(b))return Promise.resolve('[ERROR] "use strict" is not allowed in the runtime session. Remove it and try again.');let A=x?.reservedNames;if(A){for(let T of A)if(new RegExp(`(?:^|[;\\n])\\s*(?:(?:var|let|const)\\s+)?${T}\\s*=`).test(b))return Promise.resolve(`[ERROR] Cannot assign to or redeclare reserved runtime variable '${T}'. Use a different local variable name (for example: \`ctx\`) or access the original via \`inputs.${T}\`.`)}let w=x?.signal;if(w?.aborted)return Promise.reject(new Error(`Aborted: ${w.reason??"execution aborted"}`));let I=++d;return new Promise((T,v)=>{let F=setTimeout(()=>{p.delete(I),g(),v(new Error("Execution timed out"))},l),E=T,D=v;p.set(I,{resolve:V=>{clearTimeout(F),U(),E(V)},reject:V=>{clearTimeout(F),U(),D(V)}});let U=()=>{};if(w){let V=()=>{clearTimeout(F),p.delete(I),g(),D(new Error(`Aborted: ${w.reason??"execution aborted"}`))};w.addEventListener("abort",V,{once:!0}),U=()=>{w.removeEventListener("abort",V)}}f().then(()=>{if(!r)throw new Error("Worker unavailable");r.postMessage({type:"execute",id:I,code:b})}).catch(V=>{p.get(I)&&(p.delete(I),clearTimeout(F),U(),D(V))})})},close(){g()}}}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 yu(i){return new zt(i)}var as=class{constructor(e,t={}){this.transport=e;this.options=t;this.logger=t.logger??(n=>{console.log(typeof n=="string"?n:JSON.stringify(n,null,2))})}functions=[];promptFunctions=[];resourceFunctions=[];activeRequests=new Map;capabilities={};logger;async init(){"connect"in this.transport&&await this.transport.connect?.();let{result:e}=await this.sendRequest("initialize",{protocolVersion:"2024-11-05",capabilities:{roots:{listChanged:!0},sampling:{}},clientInfo:{name:"AxMCPClient",version:"1.0.0"}}),t="2024-11-05";if(e.protocolVersion!==t)throw new Error(`Protocol version mismatch. Expected ${t} but got ${e.protocolVersion}`);e.capabilities.tools&&(this.capabilities.tools=!0),e.capabilities.resources&&(this.capabilities.resources=!0),e.capabilities.prompts&&(this.capabilities.prompts=!0),await this.sendNotification("notifications/initialized"),this.capabilities.tools&&await this.discoverFunctions(),this.capabilities.prompts&&await this.discoverPromptFunctions(),this.capabilities.resources&&await this.discoverResourceFunctions()}async discoverFunctions(){let{result:e}=await this.sendRequest("tools/list");this.functions=e.tools.map(t=>{let n=this.options.functionOverrides?.find(o=>o.name===t.name),r=t.inputSchema.properties?{properties:t.inputSchema.properties,required:t.inputSchema.required??[],type:t.inputSchema.type}:void 0;return{name:n?.updates.name??t.name,description:n?.updates.description??t.description,parameters:r,func:async o=>{let{result:s}=await this.sendRequest("tools/call",{name:t.name,arguments:o});return s}}})}async discoverPromptFunctions(){let e;do{let t=await this.listPrompts(e);for(let n of t.prompts??[])this.promptFunctions.push(this.promptToFunction(n));e=t.nextCursor}while(e)}async discoverResourceFunctions(){let e;do{let t=await this.listResources(e);for(let n of t.resources??[])this.resourceFunctions.push(this.resourceToFunction(n));e=t.nextCursor}while(e);e=void 0;do{let t=await this.listResourceTemplates(e);for(let n of t.resourceTemplates??[])this.resourceFunctions.push(this.resourceTemplateToFunction(n));e=t.nextCursor}while(e)}promptToFunction(e){let t=`prompt_${e.name}`,n=this.options.functionOverrides?.find(o=>o.name===t),r=e.arguments?.length?{type:"object",properties:Object.fromEntries(e.arguments.map(o=>[o.name,{type:"string",description:o.description??""}])),required:e.arguments.filter(o=>o.required).map(o=>o.name)}:void 0;return{name:n?.updates.name??t,description:n?.updates.description??e.description??`Get the ${e.name} prompt`,parameters:r,func:async o=>{let s=await this.getPrompt(e.name,o);return this.formatPromptMessages(s.messages)}}}resourceToFunction(e){let t=`resource_${this.sanitizeName(e.name)}`,n=this.options.functionOverrides?.find(r=>r.name===t);return{name:n?.updates.name??t,description:n?.updates.description??e.description??`Read ${e.name}`,parameters:void 0,func:async()=>{let r=await this.readResource(e.uri);return this.formatResourceContents(r.contents)}}}resourceTemplateToFunction(e){let t=`resource_${this.sanitizeName(e.name)}`,n=this.options.functionOverrides?.find(o=>o.name===t),r=this.parseUriTemplate(e.uriTemplate);return{name:n?.updates.name??t,description:n?.updates.description??e.description??`Read ${e.name}`,parameters:r.length?{type:"object",properties:Object.fromEntries(r.map(o=>[o,{type:"string",description:`Value for ${o}`}])),required:r}:void 0,func:async o=>{let s=this.expandUriTemplate(e.uriTemplate,o??{}),a=await this.readResource(s);return this.formatResourceContents(a.contents)}}}formatPromptMessages(e){return e.map(t=>{let n=t.role==="user"?"User":"Assistant",r=this.extractContent(t.content);return`${n}: ${r}`}).join(`
733
+ `),t?`${t}${e}`:e}var hu=()=>typeof Worker<"u"&&typeof Blob<"u"&&typeof URL<"u"&&typeof URL.createObjectURL=="function",is=()=>typeof process<"u"&&!!process.versions?.node,Mp=()=>!!globalThis.Deno?.version?.deno,Ep=()=>globalThis.Deno?.version?.deno??null,Pp=i=>{let e=/^(\d+)\.(\d+)\.(\d+)$/.exec(i.trim());return e?{major:Number(e[1]),minor:Number(e[2]),patch:Number(e[3])}:null},fu=i=>Number.isFinite(i)?Math.max(1,Math.min(mu,Math.floor(i))):Ei,Fp=()=>{if(!is())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},_p=i=>{if(i!==void 0)return fu(i);let e=Fp();return e?fu(Math.ceil(e/2)):Ei},Np=i=>i?.debugNodeWorkerPool?!0:(globalThis.process?.env??{}).AX_RLM_DEBUG_NODE_POOL==="1",Lp=i=>{let e=new Set(i),t={};return e.has("network")&&(t.net=!0),Object.keys(t).length>0?t:"none"},$p=(i,e)=>{let t=Ep(),n=t?Pp(t):null;if(n?n.major>=1:!0)try{return new Worker(i,{type:"module",deno:{permissions:Lp(e)}})}catch{}return new Worker(i,{type:"module"})},xu=(i,e)=>{let t=new Blob([i],{type:"application/javascript"}),n=URL.createObjectURL(t),r=Mp()?$p(n,e):new Worker(n),o=!1,s=()=>{o||(URL.revokeObjectURL(n),o=!0)},a={postMessage:l=>r.postMessage(l),terminate:()=>{r.terminate(),s()},onmessage:null,onerror:null};return r.onmessage=l=>{a.onmessage?.({data:l.data})},r.onerror=l=>{a.onerror?.(new Error(l.message||"Worker error"))},a},Ni=async i=>{let e="node:worker_threads",{Worker:t}=await import(e),n=new t(i,{eval:!0}),r=null,o=null,s=!1;n.on("error",l=>{o?o(l):r=l}),n.on("exit",l=>{if(s=!0,l!==0&&!r){let u=new Error(`Worker exited with code ${l}`);o?o(u):r=u}}),n.on("message",l=>{a.onmessage?.({data:l})});let a={postMessage:l=>n.postMessage(l),terminate:()=>{n.terminate()},onmessage:null,get onerror(){return o},set onerror(l){if(o=l,l&&r){let u=r;r=null,l(u)}},get exited(){return s}};return a},Li=class{source;maxSize;idle=[];pendingCreates=0;constructor(e,t){this.source=e,this.maxSize=t}warm(){if(is())for(;this.idle.length+this.pendingCreates<this.maxSize;)this.pendingCreates+=1,Ni(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(),Ni(this.source)}release(e){e.onmessage=null,e.onerror=null,e.terminate(),this.warm()}},Au=new Map,Gp=(i,e)=>`${e}:${i}`,Dp=(i,e)=>{let t=Gp(i,e),n=Au.get(t);if(n)return n;let r=new Li(i,e);return Au.set(t,r),r},Up=i=>{let e={},t=new Map,n=0,r=new WeakMap,o=(s,a)=>{if(typeof s=="function"){let d=`fn_${++n}_${a||"root"}`;return t.set(d,s),{[Pi]:d}}if(!s||typeof s!="object")return s;if(r.has(s))return r.get(s);if(Array.isArray(s)){let d=[];r.set(s,d);for(let m=0;m<s.length;m+=1)d[m]=o(s[m],`${a}[${m}]`);return d}let l=Object.getPrototypeOf(s);if(!(l===Object.prototype||l===null))return s;let c=s,p={};r.set(s,p);for(let[d,m]of Object.entries(c))p[d]=o(m,a?`${a}.${d}`:d);return p};if(i)for(let[s,a]of Object.entries(i))e[s]=o(a,s);return{serializableGlobals:e,fnMap:t}},Bp=i=>{if(typeof structuredClone=="function")try{structuredClone(i)}catch(e){let t=e instanceof Error?e.message:String(e);throw new Error(`RLM globals must be structured-cloneable: ${t}`)}};function _i(i){if(i==null||typeof i!="object")return String(i);try{return JSON.stringify(i,null,2)}catch{return String(i)}}function yu(i,e=Fi,t=0,n=new Set){if(t>e)return{name:"Error",message:"[cause chain truncated]"};if(i!==null&&typeof i=="object"){if(n.has(i))return{name:"Error",message:"[circular]"};n.add(i)}let r=i!==null&&typeof i=="object"&&i.name!=null?String(i.name):"Error",o=i!==null&&typeof i=="object"&&i.message!=null?String(i.message):_i(i),s=i!==null&&typeof i=="object"&&typeof i.stack=="string"?i.stack:void 0,a,l=i;if(l&&typeof l=="object"&&l.cause!==void 0&&t<e)try{let p=l.cause;p instanceof Error||p!==null&&typeof p=="object"&&("message"in p||"name"in p)?a=yu(p,e,t+1,n):a={name:"Error",message:_i(p)}}catch{a={name:"Error",message:_i(l.cause)}}let u={name:r,message:o};s!==void 0&&(u.stack=s),a!==void 0&&(u.cause=a);let c=i;if(c&&typeof c=="object"&&"data"in c&&c.data!==void 0)try{typeof structuredClone=="function"?u.data=structuredClone(c.data):u.data=c.data}catch{}return u}function bu(i){if(typeof i=="string")return new Error(i);if(!i||typeof i!="object")return new Error(String(i));let e=i.message!=null?String(i.message):"Unknown error",t=new Error(e);return t.name=i.name!=null?String(i.name):"Error",typeof i.stack=="string"&&(t.stack=i.stack),i.cause!==void 0&&(t.cause=bu(i.cause)),i.data!==void 0&&(t.data=i.data),t}var $i=(s=>(s.NETWORK="network",s.STORAGE="storage",s.CODE_LOADING="code-loading",s.COMMUNICATION="communication",s.TIMING="timing",s.WORKERS="workers",s))($i||{}),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=_p(e?.nodeWorkerPoolSize),this.debugNodeWorkerPool=Np(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(`
734
+ `)}createSession(e){let t=gu(),n=is()?Dp(t,this.nodeWorkerPoolSize):null;n&&this.debugNodeWorkerPool&&console.debug(`[AxJSRuntime] Node worker pool size: ${this.nodeWorkerPoolSize}`),n?.warm();let r=null,o=null,s=null,a=!1,l=this.timeout,{serializableGlobals:u,fnMap:c}=Up(e);Bp(u);let p=new Map,d=0,m=x=>{let A=x.data;if(!A||typeof A!="object"||!("type"in A)||A.type===void 0)return;let w=A;if(w.type==="result"){if(typeof w.id!="number")return;let b=p.get(w.id);b&&(p.delete(w.id),w.error!==void 0?b.reject(bu(w.error)):b.resolve(w.value));return}if(w.type==="fn-call"){if(typeof w.id!="number"||typeof w.name!="string")return;let b=c.get(w.name);if(!b){r?.postMessage({type:"fn-result",id:w.id,error:`Function "${w.name}" not found`});return}Promise.resolve().then(()=>b(...w.args??[])).then(T=>{r?.postMessage({type:"fn-result",id:w.id,value:T})}).catch(T=>{r?.postMessage({type:"fn-result",id:w.id,error:yu(T)})})}},g=()=>{r&&(o==="node"&&n?n.release(r):r.terminate(),r=null,o=null),s=null},h=()=>{a=!0,g();for(let x of p.values())x.reject(new Error("Worker terminated"));p.clear()},f=x=>{g();for(let A of p.values())A.reject(x);p.clear()};if(hu()){r=xu(t,this.permissions),o="browser",r.onmessage=m,r.onerror=f;try{r.postMessage({type:"init",globals:u,fnNames:[...c.keys()],permissions:[...this.permissions],allowUnsafeNodeHostAccess:this.allowUnsafeNodeHostAccess,outputMode:this.outputMode,captureConsole:this.captureConsole})}catch(x){throw h(),x}}let I=async()=>{if(!r){if(a)throw new Error("Session is closed");if(hu()){r=xu(t,this.permissions),o="browser",r.onmessage=m,r.onerror=f;try{r.postMessage({type:"init",globals:u,fnNames:[...c.keys()],permissions:[...this.permissions],allowUnsafeNodeHostAccess:this.allowUnsafeNodeHostAccess,outputMode:this.outputMode,captureConsole:this.captureConsole})}catch(x){throw h(),x}return}if(!is())throw new Error("No worker runtime available: Web Worker is unavailable in this environment");s||(s=(n?n.acquire():Ni(t)).then(x=>{if(a)throw n?n.release(x):x.terminate(),new Error("Session is closed");r=x,o="node",r.onmessage=m,r.onerror=f;try{r.postMessage({type:"init",globals:u,fnNames:[...c.keys()],permissions:[...this.permissions],allowUnsafeNodeHostAccess:this.allowUnsafeNodeHostAccess,outputMode:this.outputMode,captureConsole:this.captureConsole})}catch(A){throw n?n.release(x):x.terminate(),r=null,o=null,A}})),await s}};return{execute(x,A){if(a)return Promise.reject(new Error("Session is closed"));if(/['"]use strict['"]/.test(x))return Promise.resolve('[ERROR] "use strict" is not allowed in the runtime session. Remove it and try again.');let w=A?.reservedNames;if(w){for(let v of w)if(new RegExp(`(?:^|[;\\n])\\s*(?:(?:var|let|const)\\s+)?${v}\\s*=`).test(x))return Promise.resolve(`[ERROR] Cannot assign to or redeclare reserved runtime variable '${v}'. Use a different local variable name (for example: \`ctx\`) or access the original via \`inputs.${v}\`.`)}let b=A?.signal;if(b?.aborted)return Promise.reject(new Error(`Aborted: ${b.reason??"execution aborted"}`));let T=++d;return new Promise((v,F)=>{let O=setTimeout(()=>{p.delete(T),g();for(let S of p.values())S.reject(new Error("Worker terminated"));p.clear(),F(new Error("Execution timed out"))},l),D=v,B=F;p.set(T,{resolve:S=>{clearTimeout(O),Q(),D(S)},reject:S=>{clearTimeout(O),Q(),B(S)}});let Q=()=>{};if(b){let S=()=>{clearTimeout(O),p.delete(T),h(),B(new Error(`Aborted: ${b.reason??"execution aborted"}`))};b.addEventListener("abort",S,{once:!0}),Q=()=>{b.removeEventListener("abort",S)}}I().then(()=>{if(!r)throw new Error("Worker unavailable");r.postMessage({type:"execute",id:T,code:x})}).catch(S=>{p.get(T)&&(p.delete(T),clearTimeout(O),Q(),B(S))})})},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 Iu(i){return new zt(i)}var as=class{constructor(e,t={}){this.transport=e;this.options=t;this.logger=t.logger??(n=>{console.log(typeof n=="string"?n:JSON.stringify(n,null,2))})}functions=[];promptFunctions=[];resourceFunctions=[];activeRequests=new Map;capabilities={};logger;async init(){"connect"in this.transport&&await this.transport.connect?.();let{result:e}=await this.sendRequest("initialize",{protocolVersion:"2024-11-05",capabilities:{roots:{listChanged:!0},sampling:{}},clientInfo:{name:"AxMCPClient",version:"1.0.0"}}),t="2024-11-05";if(e.protocolVersion!==t)throw new Error(`Protocol version mismatch. Expected ${t} but got ${e.protocolVersion}`);e.capabilities.tools&&(this.capabilities.tools=!0),e.capabilities.resources&&(this.capabilities.resources=!0),e.capabilities.prompts&&(this.capabilities.prompts=!0),await this.sendNotification("notifications/initialized"),this.capabilities.tools&&await this.discoverFunctions(),this.capabilities.prompts&&await this.discoverPromptFunctions(),this.capabilities.resources&&await this.discoverResourceFunctions()}async discoverFunctions(){let{result:e}=await this.sendRequest("tools/list");this.functions=e.tools.map(t=>{let n=this.options.functionOverrides?.find(o=>o.name===t.name),r=t.inputSchema.properties?{properties:t.inputSchema.properties,required:t.inputSchema.required??[],type:t.inputSchema.type}:void 0;return{name:n?.updates.name??t.name,description:n?.updates.description??t.description,parameters:r,func:async o=>{let{result:s}=await this.sendRequest("tools/call",{name:t.name,arguments:o});return s}}})}async discoverPromptFunctions(){let e;do{let t=await this.listPrompts(e);for(let n of t.prompts??[])this.promptFunctions.push(this.promptToFunction(n));e=t.nextCursor}while(e)}async discoverResourceFunctions(){let e;do{let t=await this.listResources(e);for(let n of t.resources??[])this.resourceFunctions.push(this.resourceToFunction(n));e=t.nextCursor}while(e);e=void 0;do{let t=await this.listResourceTemplates(e);for(let n of t.resourceTemplates??[])this.resourceFunctions.push(this.resourceTemplateToFunction(n));e=t.nextCursor}while(e)}promptToFunction(e){let t=`prompt_${e.name}`,n=this.options.functionOverrides?.find(o=>o.name===t),r=e.arguments?.length?{type:"object",properties:Object.fromEntries(e.arguments.map(o=>[o.name,{type:"string",description:o.description??""}])),required:e.arguments.filter(o=>o.required).map(o=>o.name)}:void 0;return{name:n?.updates.name??t,description:n?.updates.description??e.description??`Get the ${e.name} prompt`,parameters:r,func:async o=>{let s=await this.getPrompt(e.name,o);return this.formatPromptMessages(s.messages)}}}resourceToFunction(e){let t=`resource_${this.sanitizeName(e.name)}`,n=this.options.functionOverrides?.find(r=>r.name===t);return{name:n?.updates.name??t,description:n?.updates.description??e.description??`Read ${e.name}`,parameters:void 0,func:async()=>{let r=await this.readResource(e.uri);return this.formatResourceContents(r.contents)}}}resourceTemplateToFunction(e){let t=`resource_${this.sanitizeName(e.name)}`,n=this.options.functionOverrides?.find(o=>o.name===t),r=this.parseUriTemplate(e.uriTemplate);return{name:n?.updates.name??t,description:n?.updates.description??e.description??`Read ${e.name}`,parameters:r.length?{type:"object",properties:Object.fromEntries(r.map(o=>[o,{type:"string",description:`Value for ${o}`}])),required:r}:void 0,func:async o=>{let s=this.expandUriTemplate(e.uriTemplate,o??{}),a=await this.readResource(s);return this.formatResourceContents(a.contents)}}}formatPromptMessages(e){return e.map(t=>{let n=t.role==="user"?"User":"Assistant",r=this.extractContent(t.content);return`${n}: ${r}`}).join(`
734
735
 
735
736
  `)}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(`
736
- `)}sanitizeName(e){return e.replace(/[^a-zA-Z0-9_]/g,"_")}parseUriTemplate(e){return(e.match(/\{([^}]+)\}/g)??[]).map(n=>n.slice(1,-1))}expandUriTemplate(e,t){return e.replace(/\{([^}]+)\}/g,(n,r)=>t[r]??"")}async ping(e=3e3){let t=this.sendRequest("ping"),n=new Promise((s,a)=>setTimeout(()=>a(new Error("Ping response timeout exceeded")),e)),r=await Promise.race([t,n]),{result:o}=r;if(typeof o!="object"||o===null||Object.keys(o).length!==0)throw new Error(`Unexpected ping response: ${JSON.stringify(o)}`)}toFunction(){return[...this.functions,...this.promptFunctions,...this.resourceFunctions]}getCapabilities(){return{tools:this.capabilities.tools??!1,resources:this.capabilities.resources??!1,prompts:this.capabilities.prompts??!1}}hasToolsCapability(){return this.capabilities.tools??!1}hasPromptsCapability(){return this.capabilities.prompts??!1}hasResourcesCapability(){return this.capabilities.resources??!1}async listPrompts(e){if(!this.capabilities.prompts)throw new Error("Prompts are not supported");let t=e?{cursor:e}:void 0,{result:n}=await this.sendRequest("prompts/list",t);return n}async getPrompt(e,t){if(!this.capabilities.prompts)throw new Error("Prompts are not supported");let{result:n}=await this.sendRequest("prompts/get",{name:e,arguments:t});return n}async listResources(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");let t=e?{cursor:e}:void 0,{result:n}=await this.sendRequest("resources/list",t);return n}async listResourceTemplates(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");let t=e?{cursor:e}:void 0,{result:n}=await this.sendRequest("resources/templates/list",t);return n}async readResource(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");let{result:t}=await this.sendRequest("resources/read",{uri:e});return t}async subscribeResource(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");await this.sendRequest("resources/subscribe",{uri:e})}async unsubscribeResource(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");await this.sendRequest("resources/unsubscribe",{uri:e})}cancelRequest(e){if(this.activeRequests.has(e)){this.sendNotification("notifications/cancelled",{requestId:e,reason:"Client cancelled request"});let t=this.activeRequests.get(e);t&&t.reject(new Error(`Request ${e} cancelled`)),this.activeRequests.delete(e)}}async sendRequest(e,t={}){let n=ze(),r={jsonrpc:"2.0",id:n,method:e,params:t},o=new Promise((a,l)=>{this.activeRequests.set(n,{reject:l}),this.transport.send(r).then(u=>{if(this.activeRequests.delete(n),u!==null&&typeof u=="object"&&"error"in u){let c=u;l(new Error(`RPC Error ${c.error.code}: ${c.error.message}`))}else u!==null&&typeof u=="object"&&"result"in u?a({result:u.result}):l(new Error("Invalid response no result or error"))}).catch(u=>{this.activeRequests.delete(n),l(u)})}),{result:s}=await o;return{id:n,result:s}}async sendNotification(e,t={}){let n={jsonrpc:"2.0",method:e,params:t},{debug:r}=this.options;if(r){let o={name:"Notification",id:"mcp_notification",value:`Sending notification: ${JSON.stringify(n,null,2)}`};this.logger(o)}await this.transport.sendNotification(n)}};function bu(i){if(typeof Buffer<"u")return Buffer.from(i).toString("base64").replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/g,"");let e="";for(let n=0;n<i.length;n++)e+=String.fromCharCode(i[n]);return(typeof btoa=="function"?btoa(e):"").replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/g,"")}async function Iu(i){let t=new TextEncoder().encode(i),n=await Bi().subtle.digest("SHA-256",t);return new Uint8Array(n)}async function Gi(){return bu(await Iu(ze()+Math.random().toString(36)))}async function Cu(i){return bu(await Iu(i))}async function ls(i,e){let t=await fetch(i,{headers:e});if(!t.ok)throw new Error(`HTTP ${t.status} fetching ${i}: ${t.statusText}`);return await t.json()}function Tu(i){let e=new URLSearchParams;for(let[t,n]of Object.entries(i))n!==void 0&&e.set(t,n);return e.toString()}function Pr(i){return i.endsWith("/")?i.slice(0,-1):i}function jp(i){if(!i)return null;let e=i.match(/resource_metadata\s*=\s*"([^"]+)"/i)||i.match(/resource_metadata\s*=\s*([^,\s]+)/i);return e?e[1]:null}async function wu(i,e){let t=jp(e);if(t){let a=await ls(t),l=Pr(new URL(i).toString().split("?")[0]),u=Pr(a.resource??"");if(!u||u!==l)throw new Error(`Protected resource metadata 'resource' mismatch. Expected ${l} but got ${u}`);let c=Array.isArray(a.authorization_servers)?a.authorization_servers:[];if(c.length===0)throw new Error("No authorization_servers advertised by protected resource");return{resource:l,issuers:c}}let n=new URL(i),r=n.pathname.replace(/\/+$/,""),o=[];r&&r!=="/"&&o.push({url:`${n.origin}/.well-known/oauth-protected-resource${r}`,expected:`${n.origin}${r}`}),o.push({url:`${n.origin}/.well-known/oauth-protected-resource`,expected:`${n.origin}`});let s;for(let a of o)try{let l=await ls(a.url),u=Pr(l.resource??""),c=Pr(a.expected);if(!u||u!==c)throw new Error(`Protected resource metadata 'resource' mismatch. Expected ${c} but got ${u}`);let p=Array.isArray(l.authorization_servers)?l.authorization_servers:[];if(p.length===0)throw new Error("No authorization_servers advertised by protected resource");return{resource:c,issuers:p}}catch(l){s=l}throw new Error(`Failed to resolve protected resource metadata via well-known endpoints. Last error: ${String(s)}`)}async function Ru(i){let e=new URL(i),t=e.pathname.replace(/^\/+/,""),n=[];t?(n.push(`${e.origin}/.well-known/oauth-authorization-server/${t}`),n.push(`${e.origin}/.well-known/openid-configuration/${t}`),n.push(`${e.origin}/${t.replace(/\/+$/,"")}/.well-known/openid-configuration`)):(n.push(`${e.origin}/.well-known/oauth-authorization-server`),n.push(`${e.origin}/.well-known/openid-configuration`));let r;for(let o of n)try{let s=await ls(o);if(!s.authorization_endpoint||!s.token_endpoint)throw new Error("AS metadata missing endpoints");let a=s.code_challenge_methods_supported;if(!a||!a.includes("S256"))throw new Error("Authorization server does not advertise PKCE S256 support");return s}catch(s){r=s}throw new Error(`Failed to discover AS metadata for ${i}: ${String(r)}`)}var zn=class{constructor(e){this.oauth=e}tokenCache=new Map;asMetaCache=new Map;key(e,t){return`${e}::${t}`}async getStoredToken(e,t){let n=this.key(e,t);if(this.tokenCache.has(n))return this.tokenCache.get(n);let r=await this.oauth?.tokenStore?.getToken?.(n);return r&&this.tokenCache.set(n,r),r??null}async setStoredToken(e,t,n){let r=this.key(e,t);this.tokenCache.set(r,n),await this.oauth?.tokenStore?.setToken?.(r,n)}async clearStoredToken(e,t){let n=this.key(e,t);this.tokenCache.delete(n),await this.oauth?.tokenStore?.clearToken?.(n)}isExpired(e){return e?Date.now()>e-6e4:!1}async getASMeta(e){if(this.asMetaCache.has(e))return this.asMetaCache.get(e);let t=await Ru(e);return this.asMetaCache.set(e,t),t}async ensureAccessToken(e){if(!this.oauth)return null;let{resource:t,issuers:n}=await wu(e.requestedUrl,e.wwwAuthenticate),r=this.oauth.selectAuthorizationServer?await this.oauth.selectAuthorizationServer(n,{}):n[0],o=await this.getASMeta(r),s=e.currentToken??await this.getStoredToken(t,r);if(s?.accessToken&&!this.isExpired(s.expiresAt))return{token:s,issuer:r,asMeta:o,resource:t};if(s?.refreshToken)try{let x=await this.refreshToken(s.refreshToken,t,r,o);return await this.setStoredToken(t,r,x),{token:x,issuer:r,asMeta:o,resource:t}}catch{await this.clearStoredToken(t,r)}let a=this.oauth.redirectUri??"http://localhost:8787/callback",l=this.oauth.clientId?{client_id:this.oauth.clientId,client_secret:this.oauth.clientSecret}:await this.dynamicClientRegistration(o,a),u=await Gi(),c=await Cu(u),p=await Gi(),d=this.oauth.scopes?.join(" "),m=`${o.authorization_endpoint}?${Tu({response_type:"code",client_id:l.client_id,redirect_uri:a,scope:d,state:p,code_challenge:c,code_challenge_method:"S256",resource:t})}`;if(!this.oauth.onAuthCode)throw new Error(`Authorization required. Provide oauth.onAuthCode to complete the flow. Navigate to: ${m}`);let{code:g,redirectUri:h}=await this.oauth.onAuthCode(m),f=h??a,b=await this.exchangeCodeForToken({asMeta:o,code:g,codeVerifier:u,client:l,redirectUri:f,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 s=await o.json();if(!s.client_id)throw new Error("Dynamic client registration did not return client_id");return s}async exchangeCodeForToken(e){let t=new URLSearchParams;t.set("grant_type","authorization_code"),t.set("code",e.code),t.set("redirect_uri",e.redirectUri),t.set("client_id",e.client.client_id),t.set("code_verifier",e.codeVerifier),t.set("resource",e.resource),e.client.client_secret&&t.set("client_secret",e.client.client_secret);let n=await fetch(e.asMeta.token_endpoint,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:t.toString()});if(!n.ok)throw new Error(`Token exchange failed: ${n.status} ${n.statusText}`);let r=await n.json();if(!r.access_token)throw new Error("No access_token in token response");let o=r.expires_in?Date.now()+r.expires_in*1e3:void 0;return{accessToken:r.access_token,refreshToken:r.refresh_token,expiresAt:o}}async refreshToken(e,t,n,r){let o=new URLSearchParams;o.set("grant_type","refresh_token"),o.set("refresh_token",e),o.set("resource",t),this.oauth?.clientId&&o.set("client_id",this.oauth.clientId),this.oauth?.clientSecret&&o.set("client_secret",this.oauth.clientSecret);let s=await fetch(r.token_endpoint,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:o.toString()});if(!s.ok)throw new Error(`Token refresh failed: ${s.status} ${s.statusText}`);let a=await s.json();if(!a.access_token)throw new Error("No access_token in refresh response");let l=a.expires_in?Date.now()+a.expires_in*1e3:void 0;return{accessToken:a.access_token,refreshToken:a.refresh_token??e,expiresAt:l}}};var us=class{mcpEndpoint;sessionId;eventSource;pendingRequests=new Map;messageHandler;customHeaders;oauthHelper;currentToken;currentIssuer;constructor(e,t){this.mcpEndpoint=e,this.customHeaders={...t?.headers??{}},t?.authorization&&(this.customHeaders.Authorization=t.authorization),this.oauthHelper=new zn(t?.oauth)}setHeaders(e){this.customHeaders={...e}}setAuthorization(e){this.customHeaders.Authorization=e}getHeaders(){return{...this.customHeaders}}buildHeaders(e){let t={...this.customHeaders,...e};return this.sessionId&&(t["Mcp-Session-Id"]=this.sessionId),t}setMessageHandler(e){this.messageHandler=e}async connect(){return Promise.resolve()}async openListeningStream(){return new Promise((e,t)=>{let n=this.buildHeaders({Accept:"text/event-stream"}),r=new URL(this.mcpEndpoint);if(Object.keys(this.customHeaders).length>0){this.openListeningStreamWithFetch(n).then(e).catch(t);return}this.eventSource=new EventSource(r.toString()),this.eventSource.onopen=()=>e(),this.eventSource.onmessage=o=>{try{let s=JSON.parse(o.data);this.messageHandler&&this.messageHandler(s)}catch(s){console.error("Failed to parse SSE message:",s)}},this.eventSource.onerror=()=>t(new Error("Failed to establish SSE connection"))})}async openListeningStreamWithFetch(e){let t=await fetch(this.mcpEndpoint,{method:"GET",headers:e});if(t.status===401){let a=t.headers.get("WWW-Authenticate"),l=await this.oauthHelper.ensureAccessToken({requestedUrl:this.mcpEndpoint,wwwAuthenticate:a,currentToken:null});if(!l)throw new Error("HTTP 401: Unauthorized");return this.customHeaders.Authorization=`Bearer ${l.token.accessToken}`,this.openListeningStreamWithFetch(this.buildHeaders({Accept:"text/event-stream"}))}if(!t.ok)throw new Error(`Failed to open SSE stream: ${t.status} ${t.statusText}`);if(!t.body)throw new Error("No response body available for SSE stream");let n=t.body.getReader(),r=new TextDecoder,o="",s=async()=>{try{let{done:a,value:l}=await n.read();if(a){n.releaseLock();return}o+=r.decode(l,{stream:!0});let u=o.split(`
737
+ `)}sanitizeName(e){return e.replace(/[^a-zA-Z0-9_]/g,"_")}parseUriTemplate(e){return(e.match(/\{([^}]+)\}/g)??[]).map(n=>n.slice(1,-1))}expandUriTemplate(e,t){return e.replace(/\{([^}]+)\}/g,(n,r)=>t[r]??"")}async ping(e=3e3){let t=this.sendRequest("ping"),n=new Promise((s,a)=>setTimeout(()=>a(new Error("Ping response timeout exceeded")),e)),r=await Promise.race([t,n]),{result:o}=r;if(typeof o!="object"||o===null||Object.keys(o).length!==0)throw new Error(`Unexpected ping response: ${JSON.stringify(o)}`)}toFunction(){return[...this.functions,...this.promptFunctions,...this.resourceFunctions]}getCapabilities(){return{tools:this.capabilities.tools??!1,resources:this.capabilities.resources??!1,prompts:this.capabilities.prompts??!1}}hasToolsCapability(){return this.capabilities.tools??!1}hasPromptsCapability(){return this.capabilities.prompts??!1}hasResourcesCapability(){return this.capabilities.resources??!1}async listPrompts(e){if(!this.capabilities.prompts)throw new Error("Prompts are not supported");let t=e?{cursor:e}:void 0,{result:n}=await this.sendRequest("prompts/list",t);return n}async getPrompt(e,t){if(!this.capabilities.prompts)throw new Error("Prompts are not supported");let{result:n}=await this.sendRequest("prompts/get",{name:e,arguments:t});return n}async listResources(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");let t=e?{cursor:e}:void 0,{result:n}=await this.sendRequest("resources/list",t);return n}async listResourceTemplates(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");let t=e?{cursor:e}:void 0,{result:n}=await this.sendRequest("resources/templates/list",t);return n}async readResource(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");let{result:t}=await this.sendRequest("resources/read",{uri:e});return t}async subscribeResource(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");await this.sendRequest("resources/subscribe",{uri:e})}async unsubscribeResource(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");await this.sendRequest("resources/unsubscribe",{uri:e})}cancelRequest(e){if(this.activeRequests.has(e)){this.sendNotification("notifications/cancelled",{requestId:e,reason:"Client cancelled request"});let t=this.activeRequests.get(e);t&&t.reject(new Error(`Request ${e} cancelled`)),this.activeRequests.delete(e)}}async sendRequest(e,t={}){let n=Be(),r={jsonrpc:"2.0",id:n,method:e,params:t},o=new Promise((a,l)=>{this.activeRequests.set(n,{reject:l}),this.transport.send(r).then(u=>{if(this.activeRequests.delete(n),u!==null&&typeof u=="object"&&"error"in u){let c=u;l(new Error(`RPC Error ${c.error.code}: ${c.error.message}`))}else u!==null&&typeof u=="object"&&"result"in u?a({result:u.result}):l(new Error("Invalid response no result or error"))}).catch(u=>{this.activeRequests.delete(n),l(u)})}),{result:s}=await o;return{id:n,result:s}}async sendNotification(e,t={}){let n={jsonrpc:"2.0",method:e,params:t},{debug:r}=this.options;if(r){let o={name:"Notification",id:"mcp_notification",value:`Sending notification: ${JSON.stringify(n,null,2)}`};this.logger(o)}await this.transport.sendNotification(n)}};function Cu(i){if(typeof Buffer<"u")return Buffer.from(i).toString("base64").replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/g,"");let e="";for(let n=0;n<i.length;n++)e+=String.fromCharCode(i[n]);return(typeof btoa=="function"?btoa(e):"").replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/g,"")}async function Tu(i){let t=new TextEncoder().encode(i),n=await Bi().subtle.digest("SHA-256",t);return new Uint8Array(n)}async function Gi(){return Cu(await Tu(Be()+Math.random().toString(36)))}async function wu(i){return Cu(await Tu(i))}async function ls(i,e){let t=await fetch(i,{headers:e});if(!t.ok)throw new Error(`HTTP ${t.status} fetching ${i}: ${t.statusText}`);return await t.json()}function Ru(i){let e=new URLSearchParams;for(let[t,n]of Object.entries(i))n!==void 0&&e.set(t,n);return e.toString()}function Er(i){return i.endsWith("/")?i.slice(0,-1):i}function jp(i){if(!i)return null;let e=i.match(/resource_metadata\s*=\s*"([^"]+)"/i)||i.match(/resource_metadata\s*=\s*([^,\s]+)/i);return e?e[1]:null}async function Su(i,e){let t=jp(e);if(t){let a=await ls(t),l=Er(new URL(i).toString().split("?")[0]),u=Er(a.resource??"");if(!u||u!==l)throw new Error(`Protected resource metadata 'resource' mismatch. Expected ${l} but got ${u}`);let c=Array.isArray(a.authorization_servers)?a.authorization_servers:[];if(c.length===0)throw new Error("No authorization_servers advertised by protected resource");return{resource:l,issuers:c}}let n=new URL(i),r=n.pathname.replace(/\/+$/,""),o=[];r&&r!=="/"&&o.push({url:`${n.origin}/.well-known/oauth-protected-resource${r}`,expected:`${n.origin}${r}`}),o.push({url:`${n.origin}/.well-known/oauth-protected-resource`,expected:`${n.origin}`});let s;for(let a of o)try{let l=await ls(a.url),u=Er(l.resource??""),c=Er(a.expected);if(!u||u!==c)throw new Error(`Protected resource metadata 'resource' mismatch. Expected ${c} but got ${u}`);let p=Array.isArray(l.authorization_servers)?l.authorization_servers:[];if(p.length===0)throw new Error("No authorization_servers advertised by protected resource");return{resource:c,issuers:p}}catch(l){s=l}throw new Error(`Failed to resolve protected resource metadata via well-known endpoints. Last error: ${String(s)}`)}async function vu(i){let e=new URL(i),t=e.pathname.replace(/^\/+/,""),n=[];t?(n.push(`${e.origin}/.well-known/oauth-authorization-server/${t}`),n.push(`${e.origin}/.well-known/openid-configuration/${t}`),n.push(`${e.origin}/${t.replace(/\/+$/,"")}/.well-known/openid-configuration`)):(n.push(`${e.origin}/.well-known/oauth-authorization-server`),n.push(`${e.origin}/.well-known/openid-configuration`));let r;for(let o of n)try{let s=await ls(o);if(!s.authorization_endpoint||!s.token_endpoint)throw new Error("AS metadata missing endpoints");let a=s.code_challenge_methods_supported;if(!a||!a.includes("S256"))throw new Error("Authorization server does not advertise PKCE S256 support");return s}catch(s){r=s}throw new Error(`Failed to discover AS metadata for ${i}: ${String(r)}`)}var qn=class{constructor(e){this.oauth=e}tokenCache=new Map;asMetaCache=new Map;key(e,t){return`${e}::${t}`}async getStoredToken(e,t){let n=this.key(e,t);if(this.tokenCache.has(n))return this.tokenCache.get(n);let r=await this.oauth?.tokenStore?.getToken?.(n);return r&&this.tokenCache.set(n,r),r??null}async setStoredToken(e,t,n){let r=this.key(e,t);this.tokenCache.set(r,n),await this.oauth?.tokenStore?.setToken?.(r,n)}async clearStoredToken(e,t){let n=this.key(e,t);this.tokenCache.delete(n),await this.oauth?.tokenStore?.clearToken?.(n)}isExpired(e){return e?Date.now()>e-6e4:!1}async getASMeta(e){if(this.asMetaCache.has(e))return this.asMetaCache.get(e);let t=await vu(e);return this.asMetaCache.set(e,t),t}async ensureAccessToken(e){if(!this.oauth)return null;let{resource:t,issuers:n}=await Su(e.requestedUrl,e.wwwAuthenticate),r=this.oauth.selectAuthorizationServer?await this.oauth.selectAuthorizationServer(n,{}):n[0],o=await this.getASMeta(r),s=e.currentToken??await this.getStoredToken(t,r);if(s?.accessToken&&!this.isExpired(s.expiresAt))return{token:s,issuer:r,asMeta:o,resource:t};if(s?.refreshToken)try{let x=await this.refreshToken(s.refreshToken,t,r,o);return await this.setStoredToken(t,r,x),{token:x,issuer:r,asMeta:o,resource:t}}catch{await this.clearStoredToken(t,r)}let a=this.oauth.redirectUri??"http://localhost:8787/callback",l=this.oauth.clientId?{client_id:this.oauth.clientId,client_secret:this.oauth.clientSecret}:await this.dynamicClientRegistration(o,a),u=await Gi(),c=await wu(u),p=await Gi(),d=this.oauth.scopes?.join(" "),m=`${o.authorization_endpoint}?${Ru({response_type:"code",client_id:l.client_id,redirect_uri:a,scope:d,state:p,code_challenge:c,code_challenge_method:"S256",resource:t})}`;if(!this.oauth.onAuthCode)throw new Error(`Authorization required. Provide oauth.onAuthCode to complete the flow. Navigate to: ${m}`);let{code:g,redirectUri:h}=await this.oauth.onAuthCode(m),f=h??a,I=await this.exchangeCodeForToken({asMeta:o,code:g,codeVerifier:u,client:l,redirectUri:f,resource:t});return await this.setStoredToken(t,r,I),{token:I,issuer:r,asMeta:o,resource:t}}async dynamicClientRegistration(e,t){if(!e.registration_endpoint)throw new Error("Authorization server does not support dynamic client registration and no clientId was provided.");let r={application_type:t.startsWith("http://localhost")?"native":"web",client_name:"Ax MCP Client",redirect_uris:[t],grant_types:["authorization_code","refresh_token"],response_types:["code"],token_endpoint_auth_method:"none"},o=await fetch(e.registration_endpoint,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});if(!o.ok)throw new Error(`Dynamic client registration failed: ${o.status} ${o.statusText}`);let s=await o.json();if(!s.client_id)throw new Error("Dynamic client registration did not return client_id");return s}async exchangeCodeForToken(e){let t=new URLSearchParams;t.set("grant_type","authorization_code"),t.set("code",e.code),t.set("redirect_uri",e.redirectUri),t.set("client_id",e.client.client_id),t.set("code_verifier",e.codeVerifier),t.set("resource",e.resource),e.client.client_secret&&t.set("client_secret",e.client.client_secret);let n=await fetch(e.asMeta.token_endpoint,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:t.toString()});if(!n.ok)throw new Error(`Token exchange failed: ${n.status} ${n.statusText}`);let r=await n.json();if(!r.access_token)throw new Error("No access_token in token response");let o=r.expires_in?Date.now()+r.expires_in*1e3:void 0;return{accessToken:r.access_token,refreshToken:r.refresh_token,expiresAt:o}}async refreshToken(e,t,n,r){let o=new URLSearchParams;o.set("grant_type","refresh_token"),o.set("refresh_token",e),o.set("resource",t),this.oauth?.clientId&&o.set("client_id",this.oauth.clientId),this.oauth?.clientSecret&&o.set("client_secret",this.oauth.clientSecret);let s=await fetch(r.token_endpoint,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:o.toString()});if(!s.ok)throw new Error(`Token refresh failed: ${s.status} ${s.statusText}`);let a=await s.json();if(!a.access_token)throw new Error("No access_token in refresh response");let l=a.expires_in?Date.now()+a.expires_in*1e3:void 0;return{accessToken:a.access_token,refreshToken:a.refresh_token??e,expiresAt:l}}};var us=class{mcpEndpoint;sessionId;eventSource;pendingRequests=new Map;messageHandler;customHeaders;oauthHelper;currentToken;currentIssuer;constructor(e,t){this.mcpEndpoint=e,this.customHeaders={...t?.headers??{}},t?.authorization&&(this.customHeaders.Authorization=t.authorization),this.oauthHelper=new qn(t?.oauth)}setHeaders(e){this.customHeaders={...e}}setAuthorization(e){this.customHeaders.Authorization=e}getHeaders(){return{...this.customHeaders}}buildHeaders(e){let t={...this.customHeaders,...e};return this.sessionId&&(t["Mcp-Session-Id"]=this.sessionId),t}setMessageHandler(e){this.messageHandler=e}async connect(){return Promise.resolve()}async openListeningStream(){return new Promise((e,t)=>{let n=this.buildHeaders({Accept:"text/event-stream"}),r=new URL(this.mcpEndpoint);if(Object.keys(this.customHeaders).length>0){this.openListeningStreamWithFetch(n).then(e).catch(t);return}this.eventSource=new EventSource(r.toString()),this.eventSource.onopen=()=>e(),this.eventSource.onmessage=o=>{try{let s=JSON.parse(o.data);this.messageHandler&&this.messageHandler(s)}catch(s){console.error("Failed to parse SSE message:",s)}},this.eventSource.onerror=()=>t(new Error("Failed to establish SSE connection"))})}async openListeningStreamWithFetch(e){let t=await fetch(this.mcpEndpoint,{method:"GET",headers:e});if(t.status===401){let a=t.headers.get("WWW-Authenticate"),l=await this.oauthHelper.ensureAccessToken({requestedUrl:this.mcpEndpoint,wwwAuthenticate:a,currentToken:null});if(!l)throw new Error("HTTP 401: Unauthorized");return this.customHeaders.Authorization=`Bearer ${l.token.accessToken}`,this.openListeningStreamWithFetch(this.buildHeaders({Accept:"text/event-stream"}))}if(!t.ok)throw new Error(`Failed to open SSE stream: ${t.status} ${t.statusText}`);if(!t.body)throw new Error("No response body available for SSE stream");let n=t.body.getReader(),r=new TextDecoder,o="",s=async()=>{try{let{done:a,value:l}=await n.read();if(a){n.releaseLock();return}o+=r.decode(l,{stream:!0});let u=o.split(`
737
738
  `);o=u.pop()||"";for(let c of u)if(c.startsWith("data: ")){let p=c.slice(6);if(p==="[DONE]")return;try{let d=JSON.parse(p);this.messageHandler&&this.messageHandler(d)}catch(d){console.error("Failed to parse SSE data:",d)}}await s()}catch(a){throw n.releaseLock(),a}};await s()}async send(e){let t=this.buildHeaders({"Content-Type":"application/json",Accept:"application/json, text/event-stream"}),n=JSON.stringify(e),r=await fetch(this.mcpEndpoint,{method:"POST",headers:t,body:n});if(r.status===401){let a=r.headers.get("WWW-Authenticate"),l=await this.oauthHelper.ensureAccessToken({requestedUrl:this.mcpEndpoint,wwwAuthenticate:a,currentToken:null});if(!l)throw new Error("HTTP 401: Unauthorized");this.customHeaders.Authorization=`Bearer ${l.token.accessToken}`,r=await fetch(this.mcpEndpoint,{method:"POST",headers:this.buildHeaders({"Content-Type":"application/json",Accept:"application/json, text/event-stream"}),body:n})}if(!r.ok)throw r.status===404&&this.sessionId?(this.sessionId=void 0,new Error("Session expired. Please reinitialize.")):new Error(`HTTP error ${r.status}: ${r.statusText}`);let o=r.headers.get("Mcp-Session-Id");o&&(this.sessionId=o);let s=r.headers.get("Content-Type");if(s?.includes("text/event-stream"))return this.handleSSEResponse(r,e.id);if(s?.includes("application/json"))return r.json();throw new Error(`Unexpected content type: ${s}`)}async handleSSEResponse(e,t){return new Promise((n,r)=>{let o=e.body?.getReader();if(!o){r(new Error("No response body reader available"));return}let s=new TextDecoder,a="",l=async()=>{try{let{done:u,value:c}=await o.read();if(u){o.releaseLock();return}a+=s.decode(c,{stream:!0});let p=a.split(`
738
- `);a=p.pop()||"";for(let d of p)if(d.startsWith("data: ")){let m=d.slice(6);if(m==="[DONE]")return;try{let g=JSON.parse(m);if("id"in g&&g.id===t){n(g);return}this.messageHandler&&this.messageHandler(g)}catch(g){console.error("Failed to parse SSE data:",g)}}await l()}catch(u){o.releaseLock(),r(u)}};l().catch(r)})}async sendNotification(e){let t=this.buildHeaders({"Content-Type":"application/json",Accept:"application/json, text/event-stream"}),n=JSON.stringify(e),r=await fetch(this.mcpEndpoint,{method:"POST",headers:t,body:n});if(r.status===401){let o=r.headers.get("WWW-Authenticate"),s=await this.oauthHelper.ensureAccessToken({requestedUrl:this.mcpEndpoint,wwwAuthenticate:o,currentToken:null});if(!s)throw new Error("HTTP 401: Unauthorized");this.customHeaders.Authorization=`Bearer ${s.token.accessToken}`,r=await fetch(this.mcpEndpoint,{method:"POST",headers:this.buildHeaders({"Content-Type":"application/json",Accept:"application/json, text/event-stream"}),body:n})}if(!r.ok)throw r.status===404&&this.sessionId?(this.sessionId=void 0,new Error("Session expired. Please reinitialize.")):new Error(`HTTP error ${r.status}: ${r.statusText}`);r.status!==202&&console.warn(`Unexpected status for notification: ${r.status}`)}async terminateSession(){if(this.sessionId)try{let e=this.buildHeaders({});(await fetch(this.mcpEndpoint,{method:"DELETE",headers:e})).status===405&&console.info("Server does not support explicit session termination")}catch(e){console.error("Failed to terminate session:",e)}finally{this.sessionId=void 0}}close(){this.eventSource&&(this.eventSource.close(),this.eventSource=void 0)}};var 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 zn(t?.oauth)}buildHeaders(e){return{...this.customHeaders,...e}}async openSSEWithFetch(e){let t=new AbortController;this.sseAbort=t;let n=await fetch(this.sseUrl,{method:"GET",headers:e,signal:t.signal});if(n.status===401){let o=n.headers.get("WWW-Authenticate"),s=await this.oauthHelper.ensureAccessToken({requestedUrl:this.sseUrl,wwwAuthenticate:o,currentToken:null});if(!s)throw new Error("HTTP 401: Unauthorized");return this.customHeaders.Authorization=`Bearer ${s.token.accessToken}`,this.openSSEWithFetch(this.buildHeaders({Accept:"text/event-stream"}))}if(!n.ok)throw new Error("Failed to establish SSE connection");let r=this.createEndpointReady();this.consumeSSEStream(n),await r}createEndpointReady(){if(!this.endpointReady){let e,t=new Promise(n=>{e=n});this.endpointReady={resolve:e,promise:t}}return this.endpointReady.promise}async consumeSSEStream(e){if(!e.body)throw new Error("No response body available for SSE stream");let t=e.body.getReader(),n=new TextDecoder,r="",o=null;for(;;){let{done:s,value:a}=await t.read();if(s)break;r+=n.decode(a,{stream:!0});let l=r.split(`
739
- `);r=l.pop()||"";for(let u of l)if(u.startsWith("event: "))o=u.slice(7).trim();else if(u.startsWith("data: ")){let c=u.slice(6);if(o==="endpoint"){let p=c.trim(),d;try{let m=JSON.parse(p);typeof m=="string"?d=m:m&&typeof m=="object"&&"uri"in m&&(d=m.uri)}catch{d=p}if(!d)throw new Error("Endpoint URI missing in SSE event data");/^https?:\/\//i.test(d)||(d=new URL(this.sseUrl).origin+(d.startsWith("/")?d:`/${d}`)),this.endpoint=d,this.endpointReady&&(this.endpointReady.resolve(),this.endpointReady=void 0)}else{let p=c.trim();try{let d=JSON.parse(p);if(d&&typeof d=="object"&&"id"in d){let m=d.id,g=this.pendingRequests.get(m);g?(g.resolve(d),this.pendingRequests.delete(m)):this.messageHandler&&this.messageHandler(d)}else this.messageHandler&&this.messageHandler(d)}catch{}}}else u.trim()===""&&(o=null)}}async connect(){let e=this.buildHeaders({Accept:"text/event-stream"});await this.openSSEWithFetch(e)}async send(e){if(!this.endpoint)throw new Error("HTTPTransport endpoint is not initialized. Call connect() first.");let t=this.buildHeaders({"Content-Type":"application/json"}),n=JSON.stringify(e),r=new Promise((a,l)=>{this.pendingRequests.set(e.id,{resolve:a,reject:l})}),o=await fetch(this.endpoint,{method:"POST",headers:t,body:n});if(o.status===401){let a=o.headers.get("WWW-Authenticate"),l=await this.oauthHelper.ensureAccessToken({requestedUrl:this.sseUrl,wwwAuthenticate:a,currentToken:null});if(!l)throw new Error("HTTP 401: Unauthorized");this.customHeaders.Authorization=`Bearer ${l.token.accessToken}`,o=await fetch(this.endpoint,{method:"POST",headers:this.buildHeaders({"Content-Type":"application/json"}),body:n})}if(!o.ok)throw this.pendingRequests.delete(e.id),new Error(`HTTP error ${o.status}: ${o.statusText}`);if(o.headers.get("Content-Type")?.includes("application/json")){let a=await o.json();return this.pendingRequests.delete(e.id),a}return r}async sendNotification(e){if(!this.endpoint)throw new Error("HTTPTransport endpoint is not initialized. Call connect() first.");let t=this.buildHeaders({"Content-Type":"application/json"}),n=JSON.stringify(e),r=await fetch(this.endpoint,{method:"POST",headers:t,body:n});if(r.status===401){let o=r.headers.get("WWW-Authenticate"),s=await this.oauthHelper.ensureAccessToken({requestedUrl:this.sseUrl,wwwAuthenticate:o,currentToken:null});if(!s)throw new Error("HTTP 401: Unauthorized");this.customHeaders.Authorization=`Bearer ${s.token.accessToken}`,r=await fetch(this.endpoint,{method:"POST",headers:this.buildHeaders({"Content-Type":"application/json"}),body:n})}if(!r.ok)throw new Error(`HTTP error ${r.status}: ${r.statusText}`);r.status!==202&&console.warn(`Unexpected status for notification: ${r.status}`)}close(){this.eventSource&&(this.eventSource.close(),this.eventSource=void 0),this.sseAbort&&(this.sseAbort.abort(),this.sseAbort=void 0)}};function qp(i){let e=i.type;return Array.isArray(e)?e.filter(t=>typeof t=="string"):typeof e=="string"?e.includes(",")?e.split(",").map(t=>t.trim()).filter(Boolean):[e]:[]}function zp(i){let e=new Set(i);return e.has("object")&&e.has("array")&&e.has("string")&&e.has("number")&&e.has("boolean")&&e.has("null")}function Di(i){if(i.enum)return i.enum.map(n=>`"${n}"`).join(" | ");let e=qp(i);if(e.length===0)return"unknown";if(zp(e))return"any";let t=[...new Set(e)].map(n=>{if(n==="array"){let r=i.items?Di(i.items):"unknown";return r.includes(" | ")?`(${r})[]`:`${r}[]`}return n==="object"?i.properties&&Object.keys(i.properties).length>0?vu(i):"object":n});return t.length>1?t.join(" | "):t[0]??"unknown"}function vu(i,e){if(!i)return"{}";let t=!!i.properties&&Object.keys(i.properties).length>0,n=i.additionalProperties===!0;if(!t)return n?"{ [key: string]: unknown }":"{}";let r=new Set(i.required??[]),o=e?.respectRequired??!1,s=Object.entries(i.properties).map(([a,l])=>{let u=Di(l),c=o&&!r.has(a)?"?":"";return`${a}${c}: ${u}`});return i?.additionalProperties===!0&&s.push("[key: string]: unknown"),`{ ${s.join(", ")} }`}function Hp(i){return i?Di(i):"unknown"}function Su(i){let e=vu(i.parameters,{respectRequired:!0}),t=i.returns?`: Promise<${Hp(i.returns)}>`:"";return`- \`${i.qualifiedName}(args: ${e})${t}\``}function ps(i,e,t,n){let r=e.length>0?e.map(u=>{let c=kn(u.type),p=u.isOptional?"optional":"required",d=u.description?`: ${u.description}`:"";return`- \`${u.name}\` -> \`inputs.${u.name}\` (${c}, ${p})${d}`}).join(`
740
- `):"(none)",o=t.map(u=>`\`${u.name}\``).join(", "),s=[...n.agents??[]].sort((u,c)=>u.name.localeCompare(c.name)),a=[...n.agentFunctions??[]].sort((u,c)=>u.namespace!==c.namespace?u.namespace.localeCompare(c.namespace):u.name.localeCompare(c.name)),l=ut("rlm/actor.md",{contextVarList:r,responderOutputFieldTitles:o,hasInspectRuntime:!!n.hasInspectRuntime,hasAgentFunctions:s.length>0,agentFunctionsList:s.map(u=>Su({qualifiedName:`agents.${u.name}`,parameters:u.parameters})).join(`
741
- `),hasFunctions:a.length>0,functionsList:a.map(u=>Su({qualifiedName:`${u.namespace}.${u.name}`,parameters:u.parameters,returns:u.returns})).join(`
739
+ `);a=p.pop()||"";for(let d of p)if(d.startsWith("data: ")){let m=d.slice(6);if(m==="[DONE]")return;try{let g=JSON.parse(m);if("id"in g&&g.id===t){n(g);return}this.messageHandler&&this.messageHandler(g)}catch(g){console.error("Failed to parse SSE data:",g)}}await l()}catch(u){o.releaseLock(),r(u)}};l().catch(r)})}async sendNotification(e){let t=this.buildHeaders({"Content-Type":"application/json",Accept:"application/json, text/event-stream"}),n=JSON.stringify(e),r=await fetch(this.mcpEndpoint,{method:"POST",headers:t,body:n});if(r.status===401){let o=r.headers.get("WWW-Authenticate"),s=await this.oauthHelper.ensureAccessToken({requestedUrl:this.mcpEndpoint,wwwAuthenticate:o,currentToken:null});if(!s)throw new Error("HTTP 401: Unauthorized");this.customHeaders.Authorization=`Bearer ${s.token.accessToken}`,r=await fetch(this.mcpEndpoint,{method:"POST",headers:this.buildHeaders({"Content-Type":"application/json",Accept:"application/json, text/event-stream"}),body:n})}if(!r.ok)throw r.status===404&&this.sessionId?(this.sessionId=void 0,new Error("Session expired. Please reinitialize.")):new Error(`HTTP error ${r.status}: ${r.statusText}`);r.status!==202&&console.warn(`Unexpected status for notification: ${r.status}`)}async terminateSession(){if(this.sessionId)try{let e=this.buildHeaders({});(await fetch(this.mcpEndpoint,{method:"DELETE",headers:e})).status===405&&console.info("Server does not support explicit session termination")}catch(e){console.error("Failed to terminate session:",e)}finally{this.sessionId=void 0}}close(){this.eventSource&&(this.eventSource.close(),this.eventSource=void 0)}};var 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 qn(t?.oauth)}buildHeaders(e){return{...this.customHeaders,...e}}async openSSEWithFetch(e){let t=new AbortController;this.sseAbort=t;let n=await fetch(this.sseUrl,{method:"GET",headers:e,signal:t.signal});if(n.status===401){let o=n.headers.get("WWW-Authenticate"),s=await this.oauthHelper.ensureAccessToken({requestedUrl:this.sseUrl,wwwAuthenticate:o,currentToken:null});if(!s)throw new Error("HTTP 401: Unauthorized");return this.customHeaders.Authorization=`Bearer ${s.token.accessToken}`,this.openSSEWithFetch(this.buildHeaders({Accept:"text/event-stream"}))}if(!n.ok)throw new Error("Failed to establish SSE connection");let r=this.createEndpointReady();this.consumeSSEStream(n),await r}createEndpointReady(){if(!this.endpointReady){let e,t=new Promise(n=>{e=n});this.endpointReady={resolve:e,promise:t}}return this.endpointReady.promise}async consumeSSEStream(e){if(!e.body)throw new Error("No response body available for SSE stream");let t=e.body.getReader(),n=new TextDecoder,r="",o=null;for(;;){let{done:s,value:a}=await t.read();if(s)break;r+=n.decode(a,{stream:!0});let l=r.split(`
740
+ `);r=l.pop()||"";for(let u of l)if(u.startsWith("event: "))o=u.slice(7).trim();else if(u.startsWith("data: ")){let c=u.slice(6);if(o==="endpoint"){let p=c.trim(),d;try{let m=JSON.parse(p);typeof m=="string"?d=m:m&&typeof m=="object"&&"uri"in m&&(d=m.uri)}catch{d=p}if(!d)throw new Error("Endpoint URI missing in SSE event data");/^https?:\/\//i.test(d)||(d=new URL(this.sseUrl).origin+(d.startsWith("/")?d:`/${d}`)),this.endpoint=d,this.endpointReady&&(this.endpointReady.resolve(),this.endpointReady=void 0)}else{let p=c.trim();try{let d=JSON.parse(p);if(d&&typeof d=="object"&&"id"in d){let m=d.id,g=this.pendingRequests.get(m);g?(g.resolve(d),this.pendingRequests.delete(m)):this.messageHandler&&this.messageHandler(d)}else this.messageHandler&&this.messageHandler(d)}catch{}}}else u.trim()===""&&(o=null)}}async connect(){let e=this.buildHeaders({Accept:"text/event-stream"});await this.openSSEWithFetch(e)}async send(e){if(!this.endpoint)throw new Error("HTTPTransport endpoint is not initialized. Call connect() first.");let t=this.buildHeaders({"Content-Type":"application/json"}),n=JSON.stringify(e),r=new Promise((a,l)=>{this.pendingRequests.set(e.id,{resolve:a,reject:l})}),o=await fetch(this.endpoint,{method:"POST",headers:t,body:n});if(o.status===401){let a=o.headers.get("WWW-Authenticate"),l=await this.oauthHelper.ensureAccessToken({requestedUrl:this.sseUrl,wwwAuthenticate:a,currentToken:null});if(!l)throw new Error("HTTP 401: Unauthorized");this.customHeaders.Authorization=`Bearer ${l.token.accessToken}`,o=await fetch(this.endpoint,{method:"POST",headers:this.buildHeaders({"Content-Type":"application/json"}),body:n})}if(!o.ok)throw this.pendingRequests.delete(e.id),new Error(`HTTP error ${o.status}: ${o.statusText}`);if(o.headers.get("Content-Type")?.includes("application/json")){let a=await o.json();return this.pendingRequests.delete(e.id),a}return r}async sendNotification(e){if(!this.endpoint)throw new Error("HTTPTransport endpoint is not initialized. Call connect() first.");let t=this.buildHeaders({"Content-Type":"application/json"}),n=JSON.stringify(e),r=await fetch(this.endpoint,{method:"POST",headers:t,body:n});if(r.status===401){let o=r.headers.get("WWW-Authenticate"),s=await this.oauthHelper.ensureAccessToken({requestedUrl:this.sseUrl,wwwAuthenticate:o,currentToken:null});if(!s)throw new Error("HTTP 401: Unauthorized");this.customHeaders.Authorization=`Bearer ${s.token.accessToken}`,r=await fetch(this.endpoint,{method:"POST",headers:this.buildHeaders({"Content-Type":"application/json"}),body:n})}if(!r.ok)throw new Error(`HTTP error ${r.status}: ${r.statusText}`);r.status!==202&&console.warn(`Unexpected status for notification: ${r.status}`)}close(){this.eventSource&&(this.eventSource.close(),this.eventSource=void 0),this.sseAbort&&(this.sseAbort.abort(),this.sseAbort=void 0)}};function qp(i){let e=i.type;return Array.isArray(e)?e.filter(t=>typeof t=="string"):typeof e=="string"?e.includes(",")?e.split(",").map(t=>t.trim()).filter(Boolean):[e]:[]}function zp(i){let e=new Set(i);return e.has("object")&&e.has("array")&&e.has("string")&&e.has("number")&&e.has("boolean")&&e.has("null")}function Di(i){if(i.enum)return i.enum.map(n=>`"${n}"`).join(" | ");let e=qp(i);if(e.length===0)return"unknown";if(zp(e))return"any";let t=[...new Set(e)].map(n=>{if(n==="array"){let r=i.items?Di(i.items):"unknown";return r.includes(" | ")?`(${r})[]`:`${r}[]`}return n==="object"?i.properties&&Object.keys(i.properties).length>0?Ou(i):"object":n});return t.length>1?t.join(" | "):t[0]??"unknown"}function Ou(i,e){if(!i)return"{}";let t=!!i.properties&&Object.keys(i.properties).length>0,n=i.additionalProperties===!0;if(!t)return n?"{ [key: string]: unknown }":"{}";let r=new Set(i.required??[]),o=e?.respectRequired??!1,s=Object.entries(i.properties).map(([a,l])=>{let u=Di(l),c=o&&!r.has(a)?"?":"";return`${a}${c}: ${u}`});return i?.additionalProperties===!0&&s.push("[key: string]: unknown"),`{ ${s.join(", ")} }`}function Hp(i){return i?Di(i):"unknown"}function ku(i){let e=Ou(i.parameters,{respectRequired:!0}),t=i.returns?`: Promise<${Hp(i.returns)}>`:"";return`- \`${i.qualifiedName}(args: ${e})${t}\``}function ps(i,e,t,n){let r=e.length>0?e.map(u=>{let c=vn(u.type),p=u.isOptional?"optional":"required",d=u.description?`: ${u.description}`:"";return`- \`${u.name}\` -> \`inputs.${u.name}\` (${c}, ${p})${d}`}).join(`
741
+ `):"(none)",o=t.map(u=>`\`${u.name}\``).join(", "),s=[...n.agents??[]].sort((u,c)=>u.name.localeCompare(c.name)),a=[...n.agentFunctions??[]].sort((u,c)=>u.namespace!==c.namespace?u.namespace.localeCompare(c.namespace):u.name.localeCompare(c.name)),l=lt("rlm/actor.md",{contextVarList:r,responderOutputFieldTitles:o,hasInspectRuntime:!!n.hasInspectRuntime,hasAgentFunctions:s.length>0,agentFunctionsList:s.map(u=>ku({qualifiedName:`agents.${u.name}`,parameters:u.parameters})).join(`
742
+ `),hasFunctions:a.length>0,functionsList:a.map(u=>ku({qualifiedName:`${u.namespace}.${u.name}`,parameters:u.parameters,returns:u.returns})).join(`
742
743
  `),runtimeUsageInstructions:String(n.runtimeUsageInstructions)}).replace(/\n{3,}/g,`
743
744
 
744
745
  `).trim();return i?`${l}
745
746
 
746
- ${i}`:l}function ds(i,e){let t=e.length>0?e.map(r=>{let o=kn(r.type),s=r.isOptional?"optional":"required";return`- \`${r.name}\` (${o}, ${s})`}).join(`
747
- `):"(none)",n=ut("rlm/responder.md",{contextVarSummary:t}).trim();return i?`${n}
747
+ ${i}`:l}function ds(i,e){let t=e.length>0?e.map(r=>{let o=vn(r.type),s=r.isOptional?"optional":"required";return`- \`${r.name}\` (${o}, ${s})`}).join(`
748
+ `):"(none)",n=lt("rlm/responder.md",{contextVarSummary:t}).trim();return i?`${n}
748
749
 
749
- ${i}`:n}function ku(i){return i.match(/^(\w+Error:\s*.{0,60})/m)?.[1]??i.slice(0,80)}function Kp(i){let e=[],t=/(?:^|[\n;])\s*(?:var|let|const)\s+(\w+)/g,n=t.exec(i);for(;n!==null;)n[1]&&e.push(n[1]),n=t.exec(i);return e}var Wp=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 Vp(i){let e=/\b([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g,t=new Set,n=e.exec(i);for(;n!==null;)n[1]&&!Wp.has(n[1])&&t.add(n[1]),n=e.exec(i);return t}function Jp(i,e){let t=i.tags.includes("error"),n=e.tags.includes("error");if(t&&!n){i.rank=0,Fr(i,"dead-end");return}if(t&&n){let r=ku(i.output),o=ku(e.output);r!==o?(i.rank=3,Fr(i,"pivot")):(i.rank=0,Fr(i,"dead-end"));return}if(!t&&!n){let r=Kp(i.code),o=Vp(e.code);r.filter(a=>o.has(a)).length>0?(i.rank=5,Fr(i,"foundational")):(i.rank=1,Fr(i,"superseded"));return}}function Fr(i,e){i.tags.includes(e)||i.tags.push(e)}async function Qp(i,e,t,n){let r=`Summarize this resolved error in exactly one line (20-40 tokens).
750
+ ${i}`:n}function Mu(i){return i.match(/^(\w+Error:\s*.{0,60})/m)?.[1]??i.slice(0,80)}function Kp(i){let e=[],t=/(?:^|[\n;])\s*(?:var|let|const)\s+(\w+)/g,n=t.exec(i);for(;n!==null;)n[1]&&e.push(n[1]),n=t.exec(i);return e}var Wp=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 Vp(i){let e=/\b([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g,t=new Set,n=e.exec(i);for(;n!==null;)n[1]&&!Wp.has(n[1])&&t.add(n[1]),n=e.exec(i);return t}function Jp(i,e){let t=i.tags.includes("error"),n=e.tags.includes("error");if(t&&!n){i.rank=0,Pr(i,"dead-end");return}if(t&&n){let r=Mu(i.output),o=Mu(e.output);r!==o?(i.rank=3,Pr(i,"pivot")):(i.rank=0,Pr(i,"dead-end"));return}if(!t&&!n){let r=Kp(i.code),o=Vp(e.code);r.filter(a=>o.has(a)).length>0?(i.rank=5,Pr(i,"foundational")):(i.rank=1,Pr(i,"superseded"));return}}function Pr(i,e){i.tags.includes(e)||i.tags.push(e)}async function Qp(i,e,t,n){let r=`Summarize this resolved error in exactly one line (20-40 tokens).
750
751
 
751
752
  Error code:
752
753
  \`\`\`javascript
@@ -761,7 +762,7 @@ Resolution code:
761
762
  ${n.code.slice(0,500)}
762
763
  \`\`\`
763
764
 
764
- Format: [TOMBSTONE]: Resolved [Error Type] in [Module]. Fix: [1-line-summary]. Avoid: [failed-approach].`;try{let o=await i.chat({chatPrompt:[{role:"system",content:"You are a concise code summarizer."},{role:"user",content:r}],...e?.model?{model:e.model}:{},...e?.modelConfig?{modelConfig:e.modelConfig}:{}});if(!Yp(o)&&o.results?.[0]?.content){let s=o.results[0].content;return typeof s=="string"?s.trim():String(s)}return Ou}catch{return Ou}}var Ou="[TOMBSTONE]: Error was resolved in subsequent turn.";function Yp(i){return typeof i=="object"&&i!==null&&"getReader"in i}async function Mu(i,e,t,n){let r=i[e];if(!r)return;let o=r.tags.includes("error");if(t.hindsightEvaluation&&i.length>=2){let s=i[i.length-2];Jp(s,r)}if(t.tombstoning&&n){for(let s of i)if(s.tags.includes("error")&&!s.tombstone&&!s._tombstonePromise){let a=i.indexOf(s),l=i[a+1];if(l&&!l.tags.includes("error")){let u=typeof t.tombstoning=="object"?t.tombstoning:void 0;s._tombstonePromise=Qp(n,u,s,l),s._tombstonePromise.then(c=>{s.tombstone=c}).catch(()=>{}).finally(()=>{s._tombstonePromise=void 0})}}}if(t.errorPruning&&!o){let s=i.filter(a=>!a.tags.includes("error")||a.tombstone!=null||a._tombstonePromise!=null);i.length=0,i.push(...s)}if(t.hindsightEvaluation){let s=i.filter((a,l)=>l===i.length-1||a.rank===void 0||a.rank>=t.pruneRank||a.tombstone!=null||a._tombstonePromise!=null);i.length=0,i.push(...s)}}function ms(i){return i.length===0?"":i.map(e=>e.tombstone?`Action ${e.turn}:
765
+ Format: [TOMBSTONE]: Resolved [Error Type] in [Module]. Fix: [1-line-summary]. Avoid: [failed-approach].`;try{let o=await i.chat({chatPrompt:[{role:"system",content:"You are a concise code summarizer."},{role:"user",content:r}],...e?.model?{model:e.model}:{},...e?.modelConfig?{modelConfig:e.modelConfig}:{}});if(!Yp(o)&&o.results?.[0]?.content){let s=o.results[0].content;return typeof s=="string"?s.trim():String(s)}return Eu}catch{return Eu}}var Eu="[TOMBSTONE]: Error was resolved in subsequent turn.";function Yp(i){return typeof i=="object"&&i!==null&&"getReader"in i}async function Pu(i,e,t,n){let r=i[e];if(!r)return;let o=r.tags.includes("error");if(t.hindsightEvaluation&&i.length>=2){let s=i[i.length-2];Jp(s,r)}if(t.tombstoning&&n){for(let s of i)if(s.tags.includes("error")&&!s.tombstone&&!s._tombstonePromise){let a=i.indexOf(s),l=i[a+1];if(l&&!l.tags.includes("error")){let u=typeof t.tombstoning=="object"?t.tombstoning:void 0;s._tombstonePromise=Qp(n,u,s,l),s._tombstonePromise.then(c=>{s.tombstone=c}).catch(()=>{}).finally(()=>{s._tombstonePromise=void 0})}}}if(t.errorPruning&&!o){let s=i.filter(a=>!a.tags.includes("error")||a.tombstone!=null||a._tombstonePromise!=null);i.length=0,i.push(...s)}if(t.hindsightEvaluation){let s=i.filter((a,l)=>l===i.length-1||a.rank===void 0||a.rank>=t.pruneRank||a.tombstone!=null||a._tombstonePromise!=null);i.length=0,i.push(...s)}}function ms(i){return i.length===0?"":i.map(e=>e.tombstone?`Action ${e.turn}:
765
766
  ${e.tombstone}`:`Action ${e.turn}:
766
767
  \`\`\`javascript
767
768
  ${e.code}
@@ -769,7 +770,7 @@ ${e.code}
769
770
  Result:
770
771
  ${e.output}${e.actorFieldsOutput}`).join(`
771
772
 
772
- `)}function Eu(i){return`(() => {
773
+ `)}function Fu(i){return`(() => {
773
774
  const skip = new Set([${i.map(t=>`'${t}'`).join(",")}]);
774
775
  return Object.entries(globalThis)
775
776
  .filter(([k]) => !skip.has(k) && !k.startsWith('_'))
@@ -786,17 +787,18 @@ ${e.output}${e.actorFieldsOutput}`).join(`
786
787
  } catch { preview = String(v).slice(0, 80); }
787
788
  return k + ': ' + type + (size ? ' (' + size + ')' : '') + ' = ' + preview;
788
789
  }).join('\\n') || '(no user variables)';
789
- })()`}var Pu=50,Xp=5e3,Zp=8,Fu=10,ed=2,td=1200,_r=class i{ai;program;actorProgram;responderProgram;agents;agentFunctions;debug;options;rlmConfig;runtime;actorFieldNames;sharedFieldNames;globalSharedFieldNames;excludedSharedFields;excludedAgents;excludedAgentFunctions;actorDescription;responderDescription;recursionForwardOptions;actorForwardOptions;responderForwardOptions;contextPromptMaxCharsByField=new Map;activeAbortControllers=new Set;_stopRequested=!1;func;_parentSharedFields=new Set;_parentSharedAgents=new Set;_parentSharedAgentFunctions=new Set;constructor({ai:e,agentIdentity:t,signature:n},r){let{debug:o,contextFields:s,runtime:a,maxSubAgentCalls:l,maxRuntimeChars:u,maxBatchedLlmQueryConcurrency:c,maxTurns:p,trajectoryPruning:d,contextManagement:m,actorFields:g,actorCallback:h,mode:f,recursionOptions:b,actorOptions:x,responderOptions:A}=r;this.ai=e,this.agents=r.agents?.local,this.agentFunctions=r.functions?.local??[],this.debug=o,this.options=r,this.runtime=a??new zt;let{agents:w,fields:I,functions:T,...v}=r;this.program=new ve(n,v);let F=this.program.getSignature().getInputFields(),E=od(s,F,td);this.contextPromptMaxCharsByField=E.promptMaxCharsByField,this.rlmConfig={contextFields:E.contextFieldNames,sharedFields:r.fields?.shared,runtime:this.runtime,maxSubAgentCalls:l,maxRuntimeChars:u,maxBatchedLlmQueryConcurrency:c,maxTurns:p,trajectoryPruning:d,contextManagement:m,actorFields:g,actorCallback:h,mode:f},this.recursionForwardOptions=b;let{description:D,...U}=x??{},{description:V,...k}=A??{};this.actorDescription=D,this.actorForwardOptions=U,this.responderDescription=V,this.responderForwardOptions=k;let $=this.agents;for(let J of $??[]){let oe=J.getFunction().name;this.program.register(J,oe)}if(t&&(this.func={name:ld(t.name),description:t.description,parameters:this._buildFuncParameters(),func:async()=>{throw new Error("Use getFunction() to get a callable wrapper")}}),this.program.getSignature().getDescription())throw new Error("AxAgent does not support signature-level descriptions. Use setActorDescription() and/or setResponderDescription() to customize the actor and responder prompts independently.");let N=this.program.getSignature().getOutputFields(),Q=g??[];this.actorFieldNames=Q;for(let J of Q)if(!N.some(oe=>oe.name===J))throw new Error(`RLM actorField "${J}" not found in output signature`);let M=r.fields?.shared??[];this.sharedFieldNames=M;for(let J of M)if(!F.some(oe=>oe.name===J))throw new Error(`sharedField "${J}" not found in signature input fields`);this.excludedSharedFields=r.fields?.excluded??[];let L=r.fields?.globallyShared??[];this.globalSharedFieldNames=L;for(let J of L)if(!F.some(oe=>oe.name===J))throw new Error(`globalSharedField "${J}" not found in signature input fields`);let Y=r.agents?.shared??[],_=r.agents?.globallyShared??[];this.excludedAgents=r.agents?.excluded??[];let X=r.functions?.shared??[],xe=r.functions?.globallyShared??[];this.excludedAgentFunctions=r.functions?.excluded??[];let ne=[...this.agentFunctions,...X,...xe];for(let J of ne)if(!J.parameters)throw new Error(`Agent function "${J.name}" must define parameters schema for agent runtime usage.`);let re=new Set(["agents","llmQuery","final","ask_clarification"]);for(let J of ne){let oe=J.namespace??"utils";if(re.has(oe))throw new Error(`Agent function namespace "${oe}" is reserved`)}if(M.length>0&&$){let J=F.filter(oe=>M.includes(oe.name));for(let oe of $){if(!(oe instanceof i))continue;let Pe=new Set(oe.getExcludedSharedFields()),se=J.filter(Te=>!Pe.has(Te.name));se.length!==0&&oe._extendForSharedFields(se,this.rlmConfig.contextFields)}}if(Y.length>0&&$)for(let J of $)J instanceof i&&J._extendForSharedAgents(Y);if(L.length>0&&$){let J=F.filter(oe=>L.includes(oe.name));for(let oe of $){if(!(oe instanceof i))continue;let Pe=new Set(oe.getExcludedSharedFields()),se=J.filter(Te=>!Pe.has(Te.name));se.length!==0&&oe._extendForGlobalSharedFields(se,this.rlmConfig.contextFields)}}if(_.length>0&&$)for(let J of $)J instanceof i&&J._extendForGlobalSharedAgents(_);if(X.length>0&&$)for(let J of $)J instanceof i&&J._extendForSharedAgentFunctions(X);if(xe.length>0&&$)for(let J of $)J instanceof i&&J._extendForGlobalSharedAgentFunctions(xe);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.sharedFieldNames,...this.globalSharedFieldNames],r=e.filter(A=>t.includes(A.name)),o=r.filter(A=>this.contextPromptMaxCharsByField.has(A.name)&&!n.includes(A.name)).map(A=>({...A,isOptional:!0})),s=e.filter(A=>!t.includes(A.name)&&!n.includes(A.name)),a=this.program.getSignature().getOutputFields(),l=a.filter(A=>this.actorFieldNames.includes(A.name)),u=a.filter(A=>!this.actorFieldNames.includes(A.name)),c=ge().addInputFields(s).addInputFields(o).input("contextMetadata",ge.string("Metadata about pre-loaded context variables (type and size)")).input("actionLog",ge.string("Chronological trace of code executions or actions and their outputs so far")).output("javascriptCode",ge.code("JavaScript code to execute in runtime session"));l.length>0&&(c=c.addOutputFields(l));let p=c.build(),d=ge().addInputFields(s).input("contextData",ge.json("Context data to help synthesize the final answer.")).addOutputFields(u).build(),m=this.rlmConfig.maxSubAgentCalls??Pu,g=this.rlmConfig.maxTurns??Fu,h=this.agents?.map(A=>{let w=A.getFunction();return{name:w.name,description:w.description,parameters:w.parameters}})??[],f=this.agentFunctions.map(A=>({name:A.name,description:A.description,parameters:A.parameters,returns:A.returns,namespace:A.namespace??"utils"})),b=ps(this.actorDescription,r,u,{runtimeUsageInstructions:this.runtime.getUsageInstructions(),maxSubAgentCalls:m,maxTurns:g,hasInspectRuntime:!!this.rlmConfig.contextManagement?.stateInspection,agents:h,agentFunctions:f}),x=ds(this.responderDescription,r);this.actorProgram=new ve(p,{...this._genOptions,description:b}),this.responderProgram=new ve(d,{...this._genOptions,description:x})}_extendForSharedFields(e,t){let n=this.program.getSignature(),r=n.getInputFields(),o=!1;for(let s of e){if(r.some(a=>a.name===s.name)){if(this._parentSharedFields.has(s.name))throw new Error(`Duplicate shared field "${s.name}" \u2014 already propagated from a parent`);continue}this._parentSharedFields.add(s.name),n.addInputField(s),o=!0}o&&this.program.setSignature(n);for(let s of e)t.includes(s.name)&&!this.rlmConfig.contextFields.includes(s.name)&&this.rlmConfig.contextFields.push(s.name);this._buildSplitPrograms(),this.func&&(this.func.parameters=this._buildFuncParameters())}_extendForSharedAgents(e){if(e.length===0)return;let t=new Set((this.agents??[]).map(o=>o.getFunction().name)),n=new Set(this.excludedAgents),r=[];for(let o of e){if(o===this)continue;let s=o.getFunction().name;if(!n.has(s)){if(t.has(s)){if(this._parentSharedAgents.has(s))throw new Error(`Duplicate shared agent "${s}" \u2014 already propagated from a parent`);continue}this._parentSharedAgents.add(s),t.add(s),r.push(o)}}if(r.length!==0){this.agents=[...this.agents??[],...r];for(let o of r){let s=o.getFunction().name;this.program.register(o,s)}this._buildSplitPrograms()}}_extendForGlobalSharedFields(e,t){let n=this.program.getSignature(),r=n.getInputFields(),o=!1;for(let s of e){if(r.some(a=>a.name===s.name)){if(this._parentSharedFields.has(s.name))throw new Error(`Duplicate shared field "${s.name}" \u2014 already propagated from a parent`);continue}this._parentSharedFields.add(s.name),n.addInputField(s),o=!0}o&&this.program.setSignature(n);for(let s of e)t.includes(s.name)&&!this.rlmConfig.contextFields.includes(s.name)&&this.rlmConfig.contextFields.push(s.name);for(let s of e)this.sharedFieldNames.includes(s.name)||this.sharedFieldNames.push(s.name);if(this._buildSplitPrograms(),this.func&&(this.func.parameters=this._buildFuncParameters()),this.agents)for(let s of this.agents){if(!(s instanceof i))continue;let a=new Set(s.getExcludedSharedFields()),l=e.filter(u=>!a.has(u.name));l.length!==0&&s._extendForGlobalSharedFields(l,t)}}_extendForGlobalSharedAgents(e){let t=this.agents?this.agents.filter(n=>n instanceof i):[];this._extendForSharedAgents(e);for(let n of t)n._extendForGlobalSharedAgents(e)}_extendForSharedAgentFunctions(e){if(e.length===0)return;let t=new Set(this.agentFunctions.map(o=>`${o.namespace??"utils"}.${o.name}`)),n=new Set(this.excludedAgentFunctions),r=[];for(let o of e){if(n.has(o.name))continue;let s=`${o.namespace??"utils"}.${o.name}`;if(t.has(s)){if(this._parentSharedAgentFunctions.has(s))throw new Error(`Duplicate shared agent function "${s}" \u2014 already propagated from a parent`);continue}this._parentSharedAgentFunctions.add(s),t.add(s),r.push(o)}r.length!==0&&(this.agentFunctions=[...this.agentFunctions,...r],this._buildSplitPrograms())}_extendForGlobalSharedAgentFunctions(e){let t=this.agents?this.agents.filter(n=>n instanceof i):[];this._extendForSharedAgentFunctions(e);for(let n of t)n._extendForGlobalSharedAgentFunctions(e)}stop(){this._stopRequested=!0;for(let e of this.activeAbortControllers)e.abort("Stopped by user");this.program.stop(),this.actorProgram.stop(),this.responderProgram.stop()}getId(){return this.program.getId()}setId(e){this.program.setId(e)}namedPrograms(){return this.program.namedPrograms()}getTraces(){return this.program.getTraces()}setDemos(e,t){this.program.setDemos(e,t)}getUsage(){return this.program.getUsage()}resetUsage(){this.program.resetUsage()}getFunction(){if(!this.func)throw new Error("getFunction() requires agentIdentity to be set in the constructor");let e=this.forward.bind(this);return{...this.func,func:async(r,o)=>{let s=this.ai??o?.ai;if(!s)throw new Error("AI service is required to run the agent");let a=await e(s,r,o),u=this.program.getSignature().getOutputFields();return Object.keys(a).map(p=>{let d=u.find(m=>m.name===p);return d?`${d.title}: ${a[p]}`:`${p}: ${a[p]}`}).join(`
790
- `)}}}getExcludedSharedFields(){return this.excludedSharedFields}getExcludedAgents(){return this.excludedAgents}getExcludedAgentFunctions(){return this.excludedAgentFunctions}getSignature(){return this.program.getSignature()}setSignature(e){this.program.setSignature(e)}applyOptimization(e){this.program.applyOptimization?.(e)}async _runActorLoop(e,t,n,r){let o=this.rlmConfig,s=this.runtime,a=n?.debug??this.debug??e?.getOptions()?.debug??!1,l={},u={},c;Array.isArray(t)?c=t.filter(S=>S.role==="user").reduce((S,G)=>({...S,...G.values}),{}):c=t;let p=[...this.sharedFieldNames,...this.globalSharedFieldNames];for(let[S,G]of Object.entries(c))o.contextFields.includes(S)?l[S]=G:p.includes(S)||(u[S]=G);let d={};for(let S of p)S in c&&(d[S]=c[S]),S in l&&(d[S]=l[S]);let m=new Set(this.program.getSignature().getInputFields().filter(S=>o.contextFields.includes(S.name)&&S.isOptional).map(S=>S.name));for(let S of o.contextFields)if(!m.has(S)&&(!(S in l)||l[S]===void 0))throw new Error(`RLM contextField "${S}" is missing from input values`);let g={};for(let[S,G]of this.contextPromptMaxCharsByField){if(p.includes(S)||!(S in l))continue;let Z=l[S];rd(Z)<=G&&(g[S]=Z)}let h=o.maxSubAgentCalls??Pu,f=o.maxRuntimeChars??Xp,b=Math.max(1,o.maxBatchedLlmQueryConcurrency??Zp),x=o.maxTurns??Fu,A=0,w=Math.floor(h*.8),I=this.recursionForwardOptions?.maxDepth??ed,T=Math.max(0,I),{maxDepth:v,...F}=this.recursionForwardOptions??{},{functions:E,description:D,mem:U,sessionId:V,...k}=n??{},$={...this.recursionForwardOptions??{},maxDepth:Math.max(0,T-1)},N=["context"],Q=ge().input("task",ge.string("Task for recursive analysis")).input("context",ge.json("Optional context for the recursive task")).output("answer",ge.string("Answer from recursive analysis")).build(),L=(o.mode??"simple")==="advanced"&&($.maxDepth??0)>0?"advanced":"simple",Y;T>0&&(L==="advanced"?Y=new i({signature:Q},{debug:a,...o,agents:{local:this.agents},functions:{local:this.agentFunctions},contextFields:N,actorFields:void 0,recursionOptions:$,actorOptions:this.actorForwardOptions,responderOptions:this.responderForwardOptions}):Y=new ve(Q,$));let _=S=>{if(S==null)return"";if(typeof S=="string")return Tt(S,f);try{return Tt(JSON.stringify(S),f)}catch{return Tt(String(S),f)}},X=async(S,G)=>{if(!Array.isArray(S)&&typeof S=="object"&&S!==null&&"query"in S)return X(S.query,S.context??G);if(r?.aborted)throw new Fe("rlm-llm-query",r.reason?String(r.reason):"Aborted");if(Array.isArray(S))return id(S,b,async ae=>{try{return await X(ae.query,ae.context)}catch(Re){if(Re instanceof Fe)throw Re;return`[ERROR] ${Re instanceof Error?Re.message:String(Re)}`}});let ue=await(async(ae,Re)=>{let Be=Re===void 0?void 0:typeof Re=="string"?Tt(Re,f):Re;if(A++,A>h)return`[ERROR] Sub-query budget exhausted (${h}/${h}). Use the data you have already accumulated to produce your final answer.`;if(T<=0||!Y)return`[ERROR] Recursion depth limit reached (${I}).`;let Ie=3,Ne;for(let Ve=0;Ve<Ie;Ve++)try{let Me=await Y.forward(e,{task:ae,...Be!==void 0?{context:Be}:L==="advanced"?{context:""}:{}},{...k,...F,abortSignal:r,debug:a});return _(Me.answer)}catch(Me){if(Ne=Me,!nd(Me)||Ve>=Ie-1)throw Me;let xt=Math.min(6e4,1e3*Math.pow(2,Ve));await new Promise((wt,le)=>{let we=!1,je,qe=()=>{r&&je&&r.removeEventListener("abort",je)},Ht=setTimeout(()=>{we||(we=!0,qe(),wt())},xt);if(r){if(je=()=>{we||(we=!0,clearTimeout(Ht),qe(),le(new Fe("rlm-llm-query-retry-backoff",r.reason?String(r.reason):"Aborted during retry backoff")))},r.aborted){je();return}r.addEventListener("abort",je,{once:!0})}})}throw Ne})(S,G);return A===w?`${ue}
791
- [WARNING] ${A}/${h} sub-queries used. Plan to wrap up soon.`:ue},xe=this.buildRuntimeGlobals(r,d,e),ne,re=(S,G)=>{if(G.length===0)throw new Error(`${S}() requires at least one argument`);ne={type:S,args:G}},J=(...S)=>re("final",S),oe=(...S)=>re("ask_clarification",S),Pe=[...new Set(this.agentFunctions.map(S=>S.namespace??"utils"))],se={...c},Te=new Set(["inputs","llmQuery","agents","final","ask_clarification",...Pe,...o.contextManagement?.stateInspection?["inspect_runtime"]:[],...Object.keys(xe)]),et=Object.fromEntries(Object.entries(se).filter(([S])=>!Te.has(S))),_e=[...Te],Ge=[...Te,...Object.keys(et)],C=o.contextManagement?.stateInspection?async()=>{try{let S=Eu(Ge),G=await y.execute(S,{signal:r,reservedNames:Ge});return typeof G=="string"?G:String(G)}catch(S){return`[inspect_runtime error: ${S instanceof Error?S.message:String(S)}]`}}:void 0,O=()=>s.createSession({...et,inputs:se,llmQuery:X,final:J,ask_clarification:oe,...C?{inspect_runtime:C}:{},...xe}),R="[The JavaScript runtime was restarted; all global state was lost and must be recreated if needed.]",y=O(),P=!1,B=S=>S instanceof Error&&S.message==="Session is closed",j=S=>S instanceof Error&&S.message==="Execution timed out",H=S=>{if(S===void 0)return"(no output)";if(typeof S=="string")return Tt(S||"(no output)",f);try{return Tt(JSON.stringify(S,null,2),f)}catch{return Tt(String(S),f)}},q=S=>{let G=S,Z=G?.name??"Error",he=G?.message??String(S),ue=[`${Z}: ${he}`];if(G?.data!==void 0)try{ue.push(`Data: ${JSON.stringify(G.data,null,2)}`)}catch{ue.push(`Data: ${String(G.data)}`)}if(G?.cause!==void 0){let ae=(Re,Be)=>{if(Be>4)return"[cause chain truncated]";let Ie=Re,Ne=Ie?.name??"Error",Ve=Ie?.message??String(Re),Me=[`${Ne}: ${Ve}`];if(Ie?.data!==void 0)try{Me.push(`Data: ${JSON.stringify(Ie.data,null,2)}`)}catch{Me.push(`Data: ${String(Ie.data)}`)}return Ie?.cause!==void 0&&Me.push(`Caused by: ${ae(Ie.cause,Be+1)}`),Me.join(`
792
- `)};ue.push(`Caused by: ${ae(G.cause,1)}`)}return Tt(ue.join(`
793
- `),f)},K=async S=>{try{let G=await y.execute(S,{signal:r,reservedNames:_e});return{output:H(G),isError:!1}}catch(G){if(r?.aborted)throw new Fe("rlm-session",r.reason??"Aborted");if(G instanceof Error&&(G.name==="AbortError"||G.message.startsWith("Aborted")))throw G;if(j(G)&&(P=!0),B(G)){if(!P)return{output:q(G),isError:!0};try{P=!1,y=O(),ne=void 0;let Z=await y.execute(S,{signal:r,reservedNames:_e});return{output:Tt(`${R}
794
- ${H(Z)}`,f),isError:!1}}catch(Z){return j(Z)&&(P=!0),{output:Tt(`${R}
795
- ${q(Z)}`,f),isError:!0}}}if(j(G))return{output:q(G),isError:!0};throw G}},z=sd(l,{promptMaxCharsByField:this.contextPromptMaxCharsByField,inlinedFields:new Set(Object.keys(g))})||"(none)",W=o.contextManagement,ee={errorPruning:W?.errorPruning??o.trajectoryPruning??!1,hindsightEvaluation:W?.hindsightEvaluation??!1,tombstoning:W?.tombstoning,pruneRank:W?.pruneRank??2},ie=[],de={...this._genOptions,...this.actorForwardOptions,...n,debug:a,abortSignal:r},fe={},Ae=W?.stateInspection?.contextThreshold;try{for(let S=0;S<x;S++){let G=ms(ie)||"(no actions yet)";Ae&&G.length>Ae&&(G+="\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 Z=await this.actorProgram.forward(e,{...u,...g,contextMetadata:z,actionLog:G},de);S===0&&(de.debugHideSystemPrompt=!0),o.actorCallback&&await o.actorCallback(Z);for(let Ie of this.actorFieldNames)Ie in Z&&(fe[Ie]=Z[Ie]);let he=Z.javascriptCode,ue=he?.trim();if(!he||!ue)break;he=ue;let ae="";if(this.actorFieldNames.length>0){let Ie=this.actorFieldNames.filter(Ne=>Ne in Z).map(Ne=>`${Ne}: ${Z[Ne]}`).join(`
796
- `);Ie&&(ae=`
790
+ })()`}var _u=50,Xp=5e3,Zp=8,Nu=10,ed=2,td=1200,Fr=class i{ai;program;actorProgram;responderProgram;agents;agentFunctions;debug;options;rlmConfig;runtime;actorFieldNames;sharedFieldNames;globalSharedFieldNames;excludedSharedFields;excludedAgents;excludedAgentFunctions;actorDescription;responderDescription;recursionForwardOptions;actorForwardOptions;responderForwardOptions;contextPromptMaxCharsByField=new Map;activeAbortControllers=new Set;_stopRequested=!1;func;_parentSharedFields=new Set;_parentSharedAgents=new Set;_parentSharedAgentFunctions=new Set;constructor({ai:e,agentIdentity:t,signature:n},r){let{debug:o,contextFields:s,runtime:a,maxSubAgentCalls:l,maxRuntimeChars:u,maxBatchedLlmQueryConcurrency:c,maxTurns:p,trajectoryPruning:d,contextManagement:m,actorFields:g,actorCallback:h,mode:f,recursionOptions:I,actorOptions:x,responderOptions:A}=r;this.ai=e,this.agents=r.agents?.local,this.agentFunctions=r.functions?.local??[],this.debug=o,this.options=r,this.runtime=a??new zt;let{agents:w,fields:b,functions:T,...v}=r;this.program=new Se(n,v);let F=this.program.getSignature().getInputFields(),O=od(s,F,td);this.contextPromptMaxCharsByField=O.promptMaxCharsByField,this.rlmConfig={contextFields:O.contextFieldNames,sharedFields:r.fields?.shared,runtime:this.runtime,maxSubAgentCalls:l,maxRuntimeChars:u,maxBatchedLlmQueryConcurrency:c,maxTurns:p,trajectoryPruning:d,contextManagement:m,actorFields:g,actorCallback:h,mode:f},this.recursionForwardOptions=I;let{description:D,...B}=x??{},{description:Q,...S}=A??{};this.actorDescription=D,this.actorForwardOptions=B,this.responderDescription=Q,this.responderForwardOptions=S;let G=this.agents;for(let J of G??[]){let se=J.getFunction().name;this.program.register(J,se)}if(t&&(this.func={name:ld(t.name),description:t.description,parameters:this._buildFuncParameters(),func:async()=>{throw new Error("Use getFunction() to get a callable wrapper")}}),this.program.getSignature().getDescription())throw new Error("AxAgent does not support signature-level descriptions. Use setActorDescription() and/or setResponderDescription() to customize the actor and responder prompts independently.");let N=this.program.getSignature().getOutputFields(),Y=g??[];this.actorFieldNames=Y;for(let J of Y)if(!N.some(se=>se.name===J))throw new Error(`RLM actorField "${J}" not found in output signature`);let M=r.fields?.shared??[];this.sharedFieldNames=M;for(let J of M)if(!F.some(se=>se.name===J))throw new Error(`sharedField "${J}" not found in signature input fields`);this.excludedSharedFields=r.fields?.excluded??[];let L=r.fields?.globallyShared??[];this.globalSharedFieldNames=L;for(let J of L)if(!F.some(se=>se.name===J))throw new Error(`globalSharedField "${J}" not found in signature input fields`);let X=r.agents?.shared??[],_=r.agents?.globallyShared??[];this.excludedAgents=r.agents?.excluded??[];let Z=r.functions?.shared??[],Ae=r.functions?.globallyShared??[];this.excludedAgentFunctions=r.functions?.excluded??[];let re=[...this.agentFunctions,...Z,...Ae];for(let J of re)if(!J.parameters)throw new Error(`Agent function "${J.name}" must define parameters schema for agent runtime usage.`);let oe=new Set(["agents","llmQuery","final","ask_clarification"]);for(let J of re){let se=J.namespace??"utils";if(oe.has(se))throw new Error(`Agent function namespace "${se}" is reserved`)}if(M.length>0&&G){let J=F.filter(se=>M.includes(se.name));for(let se of G){if(!(se instanceof i))continue;let Ee=new Set(se.getExcludedSharedFields()),le=J.filter(Ce=>!Ee.has(Ce.name));le.length!==0&&se._extendForSharedFields(le,this.rlmConfig.contextFields)}}if(X.length>0&&G)for(let J of G)J instanceof i&&J._extendForSharedAgents(X);if(L.length>0&&G){let J=F.filter(se=>L.includes(se.name));for(let se of G){if(!(se instanceof i))continue;let Ee=new Set(se.getExcludedSharedFields()),le=J.filter(Ce=>!Ee.has(Ce.name));le.length!==0&&se._extendForGlobalSharedFields(le,this.rlmConfig.contextFields)}}if(_.length>0&&G)for(let J of G)J instanceof i&&J._extendForGlobalSharedAgents(_);if(Z.length>0&&G)for(let J of G)J instanceof i&&J._extendForSharedAgentFunctions(Z);if(Ae.length>0&&G)for(let J of G)J instanceof i&&J._extendForGlobalSharedAgentFunctions(Ae);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.sharedFieldNames,...this.globalSharedFieldNames],r=e.filter(A=>t.includes(A.name)),o=r.filter(A=>this.contextPromptMaxCharsByField.has(A.name)&&!n.includes(A.name)).map(A=>({...A,isOptional:!0})),s=e.filter(A=>!t.includes(A.name)&&!n.includes(A.name)),a=this.program.getSignature().getOutputFields(),l=a.filter(A=>this.actorFieldNames.includes(A.name)),u=a.filter(A=>!this.actorFieldNames.includes(A.name)),c=fe().addInputFields(s).addInputFields(o).input("contextMetadata",fe.string("Metadata about pre-loaded context variables (type and size)")).input("actionLog",fe.string("Chronological trace of code executions or actions and their outputs so far")).output("javascriptCode",fe.code("JavaScript code to execute in runtime session"));l.length>0&&(c=c.addOutputFields(l));let p=c.build(),d=fe().addInputFields(s).input("contextData",fe.json("Context data to help synthesize the final answer.")).addOutputFields(u).build(),m=this.rlmConfig.maxSubAgentCalls??_u,g=this.rlmConfig.maxTurns??Nu,h=this.agents?.map(A=>{let w=A.getFunction();return{name:w.name,description:w.description,parameters:w.parameters}})??[],f=this.agentFunctions.map(A=>({name:A.name,description:A.description,parameters:A.parameters,returns:A.returns,namespace:A.namespace??"utils"})),I=ps(this.actorDescription,r,u,{runtimeUsageInstructions:this.runtime.getUsageInstructions(),maxSubAgentCalls:m,maxTurns:g,hasInspectRuntime:!!this.rlmConfig.contextManagement?.stateInspection,agents:h,agentFunctions:f}),x=ds(this.responderDescription,r);this.actorProgram=new Se(p,{...this._genOptions,description:I}),this.responderProgram=new Se(d,{...this._genOptions,description:x})}_extendForSharedFields(e,t){let n=this.program.getSignature(),r=n.getInputFields(),o=!1;for(let s of e){if(r.some(a=>a.name===s.name)){if(this._parentSharedFields.has(s.name))throw new Error(`Duplicate shared field "${s.name}" \u2014 already propagated from a parent`);continue}this._parentSharedFields.add(s.name),n.addInputField(s),o=!0}o&&this.program.setSignature(n);for(let s of e)t.includes(s.name)&&!this.rlmConfig.contextFields.includes(s.name)&&this.rlmConfig.contextFields.push(s.name);this._buildSplitPrograms(),this.func&&(this.func.parameters=this._buildFuncParameters())}_extendForSharedAgents(e){if(e.length===0)return;let t=new Set((this.agents??[]).map(o=>o.getFunction().name)),n=new Set(this.excludedAgents),r=[];for(let o of e){if(o===this)continue;let s=o.getFunction().name;if(!n.has(s)){if(t.has(s)){if(this._parentSharedAgents.has(s))throw new Error(`Duplicate shared agent "${s}" \u2014 already propagated from a parent`);continue}this._parentSharedAgents.add(s),t.add(s),r.push(o)}}if(r.length!==0){this.agents=[...this.agents??[],...r];for(let o of r){let s=o.getFunction().name;this.program.register(o,s)}this._buildSplitPrograms()}}_extendForGlobalSharedFields(e,t){let n=this.program.getSignature(),r=n.getInputFields(),o=!1;for(let s of e){if(r.some(a=>a.name===s.name)){if(this._parentSharedFields.has(s.name))throw new Error(`Duplicate shared field "${s.name}" \u2014 already propagated from a parent`);continue}this._parentSharedFields.add(s.name),n.addInputField(s),o=!0}o&&this.program.setSignature(n);for(let s of e)t.includes(s.name)&&!this.rlmConfig.contextFields.includes(s.name)&&this.rlmConfig.contextFields.push(s.name);for(let s of e)this.sharedFieldNames.includes(s.name)||this.sharedFieldNames.push(s.name);if(this._buildSplitPrograms(),this.func&&(this.func.parameters=this._buildFuncParameters()),this.agents)for(let s of this.agents){if(!(s instanceof i))continue;let a=new Set(s.getExcludedSharedFields()),l=e.filter(u=>!a.has(u.name));l.length!==0&&s._extendForGlobalSharedFields(l,t)}}_extendForGlobalSharedAgents(e){let t=this.agents?this.agents.filter(n=>n instanceof i):[];this._extendForSharedAgents(e);for(let n of t)n._extendForGlobalSharedAgents(e)}_extendForSharedAgentFunctions(e){if(e.length===0)return;let t=new Set(this.agentFunctions.map(o=>`${o.namespace??"utils"}.${o.name}`)),n=new Set(this.excludedAgentFunctions),r=[];for(let o of e){if(n.has(o.name))continue;let s=`${o.namespace??"utils"}.${o.name}`;if(t.has(s)){if(this._parentSharedAgentFunctions.has(s))throw new Error(`Duplicate shared agent function "${s}" \u2014 already propagated from a parent`);continue}this._parentSharedAgentFunctions.add(s),t.add(s),r.push(o)}r.length!==0&&(this.agentFunctions=[...this.agentFunctions,...r],this._buildSplitPrograms())}_extendForGlobalSharedAgentFunctions(e){let t=this.agents?this.agents.filter(n=>n instanceof i):[];this._extendForSharedAgentFunctions(e);for(let n of t)n._extendForGlobalSharedAgentFunctions(e)}stop(){this._stopRequested=!0;for(let e of this.activeAbortControllers)e.abort("Stopped by user");this.program.stop(),this.actorProgram.stop(),this.responderProgram.stop()}getId(){return this.program.getId()}setId(e){this.program.setId(e)}namedPrograms(){return this.program.namedPrograms()}getTraces(){return this.program.getTraces()}setDemos(e,t){this.program.setDemos(e,t)}getUsage(){return this.program.getUsage()}resetUsage(){this.program.resetUsage()}getFunction(){if(!this.func)throw new Error("getFunction() requires agentIdentity to be set in the constructor");let e=this.forward.bind(this);return{...this.func,func:async(r,o)=>{let s=this.ai??o?.ai;if(!s)throw new Error("AI service is required to run the agent");let a=await e(s,r,o),u=this.program.getSignature().getOutputFields();return Object.keys(a).map(p=>{let d=u.find(m=>m.name===p);return d?`${d.title}: ${a[p]}`:`${p}: ${a[p]}`}).join(`
791
+ `)}}}getExcludedSharedFields(){return this.excludedSharedFields}getExcludedAgents(){return this.excludedAgents}getExcludedAgentFunctions(){return this.excludedAgentFunctions}getSignature(){return this.program.getSignature()}setSignature(e){this.program.setSignature(e)}applyOptimization(e){this.program.applyOptimization?.(e)}async _runActorLoop(e,t,n,r){let o=this.rlmConfig,s=this.runtime,a=n?.debug??this.debug??e?.getOptions()?.debug??!1,l={},u={},c;Array.isArray(t)?c=t.filter(P=>P.role==="user").reduce((P,$)=>({...P,...$.values}),{}):c=t;let p=[...this.sharedFieldNames,...this.globalSharedFieldNames];for(let[P,$]of Object.entries(c))o.contextFields.includes(P)?l[P]=$:p.includes(P)||(u[P]=$);let d={};for(let P of p)P in c&&(d[P]=c[P]),P in l&&(d[P]=l[P]);let m=new Set(this.program.getSignature().getInputFields().filter(P=>o.contextFields.includes(P.name)&&P.isOptional).map(P=>P.name));for(let P of o.contextFields)if(!m.has(P)&&(!(P in l)||l[P]===void 0))throw new Error(`RLM contextField "${P}" is missing from input values`);let g={};for(let[P,$]of this.contextPromptMaxCharsByField){if(p.includes(P)||!(P in l))continue;let V=l[P];rd(V)<=$&&(g[P]=V)}let h=o.maxSubAgentCalls??_u,f=o.maxRuntimeChars??Xp,I=Math.max(1,o.maxBatchedLlmQueryConcurrency??Zp),x=o.maxTurns??Nu,A=0,w=Math.floor(h*.8),b=this.recursionForwardOptions?.maxDepth??ed,T=Math.max(0,b),{maxDepth:v,...F}=this.recursionForwardOptions??{},{functions:O,description:D,mem:B,sessionId:Q,...S}=n??{},G={...this.recursionForwardOptions??{},maxDepth:Math.max(0,T-1)},N=["context"],Y=fe().input("task",fe.string("Task for recursive analysis")).input("context",fe.json("Optional context for the recursive task")).output("answer",fe.string("Answer from recursive analysis")).build(),L=(o.mode??"simple")==="advanced"&&(G.maxDepth??0)>0?"advanced":"simple",X;T>0&&(L==="advanced"?X=new i({signature:Y},{debug:a,...o,agents:{local:this.agents},functions:{local:this.agentFunctions},contextFields:N,actorFields:void 0,recursionOptions:G,actorOptions:this.actorForwardOptions,responderOptions:this.responderForwardOptions}):X=new Se(Y,G));let _=P=>{if(P==null)return"";if(typeof P=="string")return xt(P,f);try{return xt(JSON.stringify(P),f)}catch{return xt(String(P),f)}},Z=async(P,$)=>{if(!Array.isArray(P)&&typeof P=="object"&&P!==null&&"query"in P)return Z(P.query,P.context??$);if(r?.aborted)throw new Fe("rlm-llm-query",r.reason?String(r.reason):"Aborted");if(Array.isArray(P))return id(P,I,async pe=>{try{return await Z(pe.query,pe.context)}catch(ie){if(ie instanceof Fe)throw ie;return`[ERROR] ${ie instanceof Error?ie.message:String(ie)}`}});let he=await(async(pe,ie)=>{let Ge=ie===void 0?void 0:typeof ie=="string"?xt(ie,f):ie;if(A++,A>h)return`[ERROR] Sub-query budget exhausted (${h}/${h}). Use the data you have already accumulated to produce your final answer.`;if(T<=0||!X)return`[ERROR] Recursion depth limit reached (${b}).`;let we=3,ve;for(let He=0;He<we;He++)try{let Pe=await X.forward(e,{task:pe,...Ge!==void 0?{context:Ge}:L==="advanced"?{context:""}:{}},{...S,...F,abortSignal:r,debug:a});return _(Pe.answer)}catch(Pe){if(ve=Pe,!nd(Pe)||He>=we-1)throw Pe;let ct=Math.min(6e4,1e3*Math.pow(2,He));await new Promise((At,wt)=>{let ue=!1,Te,Ze=()=>{r&&Te&&r.removeEventListener("abort",Te)},Ke=setTimeout(()=>{ue||(ue=!0,Ze(),At())},ct);if(r){if(Te=()=>{ue||(ue=!0,clearTimeout(Ke),Ze(),wt(new Fe("rlm-llm-query-retry-backoff",r.reason?String(r.reason):"Aborted during retry backoff")))},r.aborted){Te();return}r.addEventListener("abort",Te,{once:!0})}})}throw ve})(P,$);return A===w?`${he}
792
+ [WARNING] ${A}/${h} sub-queries used. Plan to wrap up soon.`:he},Ae=this.buildRuntimeGlobals(r,d,e),re,oe=(P,$)=>{if($.length===0)throw new Error(`${P}() requires at least one argument`);re={type:P,args:$}},J=(...P)=>oe("final",P),se=(...P)=>oe("ask_clarification",P),Ee=[...new Set(this.agentFunctions.map(P=>P.namespace??"utils"))],le={...c},Ce=new Set(["inputs","llmQuery","agents","final","ask_clarification",...Ee,...o.contextManagement?.stateInspection?["inspect_runtime"]:[],...Object.keys(Ae)]),Xe=Object.fromEntries(Object.entries(le).filter(([P])=>!Ce.has(P))),_e=[...Ce],$e=[...Ce,...Object.keys(Xe)],C=o.contextManagement?.stateInspection?async()=>{try{let P=Fu($e),$=await y.execute(P,{signal:r,reservedNames:$e});return typeof $=="string"?$:String($)}catch(P){return`[inspect_runtime error: ${P instanceof Error?P.message:String(P)}]`}}:void 0,k=()=>s.createSession({...Xe,inputs:le,llmQuery:Z,final:J,ask_clarification:se,...C?{inspect_runtime:C}:{},...Ae}),R="[The JavaScript runtime was restarted; all global state was lost and must be recreated if needed.]",y=k(),E=P=>P instanceof Error&&P.message==="Session is closed",U=P=>P instanceof Error&&P.message==="Execution timed out",j=P=>{if(P===void 0)return"(no output)";if(typeof P=="string")return xt(P||"(no output)",f);try{return xt(JSON.stringify(P,null,2),f)}catch{return xt(String(P),f)}},K=P=>{let $=P,V=$?.name??"Error",ne=$?.message??String(P),he=[`${V}: ${ne}`];if($?.data!==void 0)try{he.push(`Data: ${JSON.stringify($.data,null,2)}`)}catch{he.push(`Data: ${String($.data)}`)}if($?.cause!==void 0){let pe=(ie,Ge)=>{if(Ge>4)return"[cause chain truncated]";let we=ie,ve=we?.name??"Error",He=we?.message??String(ie),Pe=[`${ve}: ${He}`];if(we?.data!==void 0)try{Pe.push(`Data: ${JSON.stringify(we.data,null,2)}`)}catch{Pe.push(`Data: ${String(we.data)}`)}return we?.cause!==void 0&&Pe.push(`Caused by: ${pe(we.cause,Ge+1)}`),Pe.join(`
793
+ `)};he.push(`Caused by: ${pe($.cause,1)}`)}return xt(he.join(`
794
+ `),f)},q=async P=>{try{let $=await y.execute(P,{signal:r,reservedNames:_e});return{output:j($),isError:!1}}catch($){if(r?.aborted)throw new Fe("rlm-session",r.reason??"Aborted");if($ instanceof Error&&($.name==="AbortError"||$.message.startsWith("Aborted")))throw $;if(U($))return{output:xt(`${R}
795
+ ${K($)}`,f),isError:!0};if(E($))try{y=k(),re=void 0;let V=await y.execute(P,{signal:r,reservedNames:_e});return{output:xt(`${R}
796
+ ${j(V)}`,f),isError:!1}}catch(V){return{output:xt(`${R}
797
+ ${K(V)}`,f),isError:!0}}throw $}},H=sd(l,{promptMaxCharsByField:this.contextPromptMaxCharsByField,inlinedFields:new Set(Object.keys(g))})||"(none)",z=o.contextManagement,W={errorPruning:z?.errorPruning??o.trajectoryPruning??!1,hindsightEvaluation:z?.hindsightEvaluation??!1,tombstoning:z?.tombstoning,pruneRank:z?.pruneRank??2},ee=[],ce={...this._genOptions,...this.actorForwardOptions,...n,debug:a,abortSignal:r},ae={},xe=z?.stateInspection?.contextThreshold;try{for(let P=0;P<x;P++){let $=ms(ee)||"(no actions yet)";xe&&$.length>xe&&($+="\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 V=await this.actorProgram.forward(e,{...u,...g,contextMetadata:H,actionLog:$},ce);P===0&&(ce.debugHideSystemPrompt=!0),o.actorCallback&&await o.actorCallback(V);for(let we of this.actorFieldNames)we in V&&(ae[we]=V[we]);let ne=V.javascriptCode,he=ne?.trim();if(!ne||!he)break;ne=he;let pe="";if(this.actorFieldNames.length>0){let we=this.actorFieldNames.filter(ve=>ve in V).map(ve=>`${ve}: ${V[ve]}`).join(`
798
+ `);we&&(pe=`
797
799
  Actor fields:
798
- ${Ie}`)}ne=void 0;let{output:Re,isError:Be}=await K(he);if(ie.push({turn:S+1,code:he,output:Re,actorFieldsOutput:ae,tags:Be?["error"]:[]}),await Mu(ie,ie.length-1,ee,e),ne)break}}finally{try{y.close()}catch{}}let ye=ne??{type:"final",args:[ms(ie)||"(no actions were taken)"]};return{nonContextValues:u,contextMetadata:z,actionLog:ms(ie),actorResult:ye,actorFieldValues:fe}}async forward(e,t,n){let r=new AbortController;this._stopRequested&&r.abort("Stopped by user (pre-forward)");let o=vt(r.signal,n?.abortSignal);this.activeAbortControllers.add(r);try{let s=this.ai??e,a=n?.debug??this.debug??s?.getOptions()?.debug??!1,{nonContextValues:l,actorResult:u,actorFieldValues:c}=await this._runActorLoop(s,t,n,o),p={...this._genOptions,...this.responderForwardOptions,...n,debug:a,abortSignal:o,maxSteps:1};return{...await this.responderProgram.forward(s,{...l,contextData:u},p),...c}}finally{this.activeAbortControllers.delete(r),this._stopRequested=!1}}async*streamingForward(e,t,n){let r=new AbortController;this._stopRequested&&r.abort("Stopped by user (pre-forward)");let o=vt(r.signal,n?.abortSignal);this.activeAbortControllers.add(r);try{let s=this.ai??e,a=n?.debug??this.debug??s?.getOptions()?.debug??!1,{nonContextValues:l,contextMetadata:u,actorResult:c,actorFieldValues:p}=await this._runActorLoop(s,t,n,o),d={...this._genOptions,...this.responderForwardOptions,...n,debug:a,abortSignal:o,maxSteps:1};for await(let m of this.responderProgram.streamingForward(s,{...l,contextMetadata:u,actorResult:c},d))yield m;Object.keys(p).length>0&&(yield{version:1,index:0,delta:p})}finally{this.activeAbortControllers.delete(r),this._stopRequested=!1}}static wrapFunction(e,t,n){return async(...r)=>{let o;if(r.length===1&&typeof r[0]=="object"&&r[0]!==null&&!Array.isArray(r[0]))o=r[0];else{let s=e.parameters?.properties?Object.keys(e.parameters.properties):[];o={},s.forEach((a,l)=>{l<r.length&&(o[a]=r[l])})}return await e.func(o,{abortSignal:t,ai:n})}}static wrapFunctionWithSharedFields(e,t,n,r){return!n||Object.keys(n).length===0?i.wrapFunction(e,t,r):async(...o)=>{let s;if(o.length===1&&typeof o[0]=="object"&&o[0]!==null&&!Array.isArray(o[0]))s=o[0];else{let l=e.parameters?.properties?Object.keys(e.parameters.properties):[];s={},l.forEach((u,c)=>{c<o.length&&(s[u]=o[c])})}let a={...n,...s};return await e.func(a,{abortSignal:t,ai:r})}}buildRuntimeGlobals(e,t,n){let r={};for(let o of this.agentFunctions){let s=o.namespace??"utils";(!r[s]||typeof r[s]!="object")&&(r[s]={}),r[s][o.name]=i.wrapFunction(o,e,n)}if(this.agents&&this.agents.length>0){let o={};for(let s of this.agents){let a=s.getFunction(),l=new Set(s.getExcludedSharedFields?.()??[]),u={};if(t)for(let[c,p]of Object.entries(t))l.has(c)||(u[c]=p);o[a.name]=i.wrapFunctionWithSharedFields(a,e,u,n)}r.agents=o}return r}get _genOptions(){if(!this.options)return{};let{agents:e,fields:t,functions:n,...r}=this.options;return r}_buildFuncParameters(){let e=this.program.getSignature().toInputJSONSchema();return this._parentSharedFields.size>0?ad(e,this._parentSharedFields):e}};function _u(i,e){let t=typeof i=="string"?Oe.create(i):i,{ai:n,agentIdentity:r,...o}=e;return new _r({ai:n,agentIdentity:r,signature:t},o)}function nd(i){return i instanceof Qe&&i.status>=500&&i.status<600?!0:i instanceof De||i instanceof nt}function Tt(i,e){return i.length<=e?i:`${i.slice(0,e)}
800
+ ${we}`)}re=void 0;let{output:ie,isError:Ge}=await q(ne);if(ee.push({turn:P+1,code:ne,output:ie,actorFieldsOutput:pe,tags:Ge?["error"]:[]}),await Pu(ee,ee.length-1,W,e),re)break}}finally{try{y.close()}catch{}}let ye=re??{type:"final",args:[ms(ee)||"(no actions were taken)"]};return{nonContextValues:u,contextMetadata:H,actionLog:ms(ee),actorResult:ye,actorFieldValues:ae}}async forward(e,t,n){let r=new AbortController;this._stopRequested&&r.abort("Stopped by user (pre-forward)");let o=vt(r.signal,n?.abortSignal);this.activeAbortControllers.add(r);try{let s=this.ai??e,a=n?.debug??this.debug??s?.getOptions()?.debug??!1,{nonContextValues:l,actorResult:u,actorFieldValues:c}=await this._runActorLoop(s,t,n,o),p={...this._genOptions,...this.responderForwardOptions,...n,debug:a,abortSignal:o,maxSteps:1};return{...await this.responderProgram.forward(s,{...l,contextData:u},p),...c}}finally{this.activeAbortControllers.delete(r),this._stopRequested=!1}}async*streamingForward(e,t,n){let r=new AbortController;this._stopRequested&&r.abort("Stopped by user (pre-forward)");let o=vt(r.signal,n?.abortSignal);this.activeAbortControllers.add(r);try{let s=this.ai??e,a=n?.debug??this.debug??s?.getOptions()?.debug??!1,{nonContextValues:l,contextMetadata:u,actorResult:c,actorFieldValues:p}=await this._runActorLoop(s,t,n,o),d={...this._genOptions,...this.responderForwardOptions,...n,debug:a,abortSignal:o,maxSteps:1};for await(let m of this.responderProgram.streamingForward(s,{...l,contextMetadata:u,actorResult:c},d))yield m;Object.keys(p).length>0&&(yield{version:1,index:0,delta:p})}finally{this.activeAbortControllers.delete(r),this._stopRequested=!1}}static wrapFunction(e,t,n){return async(...r)=>{let o;if(r.length===1&&typeof r[0]=="object"&&r[0]!==null&&!Array.isArray(r[0]))o=r[0];else{let s=e.parameters?.properties?Object.keys(e.parameters.properties):[];o={},s.forEach((a,l)=>{l<r.length&&(o[a]=r[l])})}return await e.func(o,{abortSignal:t,ai:n})}}static wrapFunctionWithSharedFields(e,t,n,r){return!n||Object.keys(n).length===0?i.wrapFunction(e,t,r):async(...o)=>{let s;if(o.length===1&&typeof o[0]=="object"&&o[0]!==null&&!Array.isArray(o[0]))s=o[0];else{let l=e.parameters?.properties?Object.keys(e.parameters.properties):[];s={},l.forEach((u,c)=>{c<o.length&&(s[u]=o[c])})}let a={...n,...s};return await e.func(a,{abortSignal:t,ai:r})}}buildRuntimeGlobals(e,t,n){let r={};for(let o of this.agentFunctions){let s=o.namespace??"utils";(!r[s]||typeof r[s]!="object")&&(r[s]={}),r[s][o.name]=i.wrapFunction(o,e,n)}if(this.agents&&this.agents.length>0){let o={};for(let s of this.agents){let a=s.getFunction(),l=new Set(s.getExcludedSharedFields?.()??[]),u={};if(t)for(let[c,p]of Object.entries(t))l.has(c)||(u[c]=p);o[a.name]=i.wrapFunctionWithSharedFields(a,e,u,n)}r.agents=o}return r}get _genOptions(){if(!this.options)return{};let{agents:e,fields:t,functions:n,...r}=this.options;return r}_buildFuncParameters(){let e=this.program.getSignature().toInputJSONSchema();return this._parentSharedFields.size>0?ad(e,this._parentSharedFields):e}};function Lu(i,e){let t=typeof i=="string"?Oe.create(i):i,{ai:n,agentIdentity:r,...o}=e;return new Fr({ai:n,agentIdentity:r,signature:t},o)}function nd(i){return i instanceof Ve&&i.status>=500&&i.status<600?!0:i instanceof De||i instanceof tt}function xt(i,e){return i.length<=e?i:`${i.slice(0,e)}
799
801
  ...[truncated ${i.length-e} chars]`}function rd(i){if(typeof i=="string")return i.length;try{return JSON.stringify(i).length}catch{return String(i).length}}function od(i,e,t){let n=new Set(e.map(a=>a.name)),r=new Set,o=[],s=new Map;for(let a of i){let l=typeof a=="string"?a:a.field;if(!n.has(l))throw new Error(`RLM contextField "${l}" not found in signature`);if(r.has(l))throw new Error(`Duplicate contextField "${l}"`);if(r.add(l),o.push(l),typeof a!="string"){let u=a.promptMaxChars??t;if(!Number.isFinite(u)||u<0)throw new Error(`contextField "${l}" promptMaxChars must be a finite number >= 0`);s.set(l,u)}}return{contextFieldNames:o,promptMaxCharsByField:s}}function sd(i,e){let t=[];for(let[n,r]of Object.entries(i)){let o=Array.isArray(r)?"array":typeof r,s=typeof r=="string"?`${r.length} chars`:Array.isArray(r)?`${r.length} items`:r&&typeof r=="object"?`${Object.keys(r).length} keys`:"n/a",a=e?.promptMaxCharsByField?.get(n),l=a===void 0?"runtime-only":e?.inlinedFields?.has(n)?`inline (<=${a} chars)`:`runtime-only (>${a} chars)`;t.push(`- ${n}: type=${o}, size=${s}, prompt=${l}`)}return t.join(`
800
- `)}async function id(i,e,t){if(i.length===0)return[];let n=new Array(i.length),r=0,o=Math.max(1,Math.min(e,i.length)),s=Array.from({length:o},async()=>{for(;;){let a=r++;if(a>=i.length)return;let l=i[a];if(l===void 0)return;n[a]=await t(l,a)}});return await Promise.all(s),n}function ad(i,e){if(!i.properties||e.size===0)return i;let t=Object.fromEntries(Object.entries(i.properties).filter(([r])=>!e.has(r))),n=i.required?.filter(r=>!e.has(r));return{...i,properties:t,...n!==void 0?{required:n}:{}}}function ld(i){return i.split(/[^a-zA-Z0-9]/).map((n,r)=>{let o=n.toLowerCase();return r>0&&o&&o[0]?o[0].toUpperCase()+o.slice(1):o}).join("")}var Nu=(i,e)=>{let t=e?.maxHops??3,n=e?.qualityThreshold??.8,r=e?.maxIterations??2,o=e?.qualityTarget??.85,s=e?.disableQualityHealing??!1;return ns({logger:e?.logger,debug:e?.debug}).node("queryGenerator","originalQuestion:string, previousContext?:string -> searchQuery:string, queryReasoning:string").node("contextualizer","retrievedDocument:string, accumulatedContext?:string -> enhancedContext:string").node("qualityAssessor","currentContext:string, originalQuestion:string -> completenessScore:number, missingAspects:string[]").node("questionDecomposer","complexQuestion:string -> subQuestions:string[], decompositionReason:string").node("evidenceSynthesizer","collectedEvidence:string[], originalQuestion:string -> synthesizedEvidence:string, evidenceGaps:string[]").node("gapAnalyzer","synthesizedEvidence:string, evidenceGaps:string[], originalQuestion:string -> needsMoreInfo:boolean, focusedQueries:string[]").node("answerGenerator","finalContext:string, originalQuestion:string -> comprehensiveAnswer:string, confidenceLevel:number").node("queryRefiner","originalQuestion:string, currentContext:string, missingAspects:string[] -> refinedQuery:string").node("qualityValidator","generatedAnswer:string, userQuery:string -> qualityScore:number, issues:string[]").node("answerHealer","originalAnswer:string, healingDocument:string, issues?:string[] -> healedAnswer:string").map(a=>({...a,maxHops:t,qualityThreshold:n,maxIterations:r,qualityTarget:o,disableQualityHealing:s,currentHop:0,accumulatedContext:"",retrievedContexts:[],completenessScore:0,searchQuery:a.originalQuestion,shouldContinue:!0,iteration:0,allEvidence:[],evidenceSources:[],needsMoreInfo:!0,healingAttempts:0,currentQuality:0,shouldContinueHealing:!0,currentAnswer:"",currentIssues:[]})).while(a=>a.currentHop<a.maxHops&&a.completenessScore<a.qualityThreshold&&a.shouldContinue).map(a=>({...a,currentHop:a.currentHop+1})).execute("queryGenerator",a=>({originalQuestion:a.originalQuestion,previousContext:a.accumulatedContext||void 0})).map(async a=>{let l=a.queryGeneratorResult?.searchQuery||a.searchQuery||a.originalQuestion,u=await i(l);return{...a,retrievalResult:{retrievedDocument:u,retrievalConfidence:.9}}}).execute("contextualizer",a=>({retrievedDocument:a.retrievalResult.retrievedDocument,accumulatedContext:a.accumulatedContext||void 0})).execute("qualityAssessor",a=>({currentContext:a.contextualizerResult.enhancedContext,originalQuestion:a.originalQuestion})).map(a=>({...a,accumulatedContext:a.contextualizerResult.enhancedContext,retrievedContexts:[...a.retrievedContexts,a.retrievalResult.retrievedDocument],completenessScore:a.qualityAssessorResult.completenessScore,searchQuery:a.queryGeneratorResult.searchQuery,shouldContinue:a.qualityAssessorResult.completenessScore<a.qualityThreshold})).branch(a=>a.shouldContinue&&a.currentHop<a.maxHops).when(!0).execute("queryRefiner",a=>({originalQuestion:a.originalQuestion,currentContext:a.accumulatedContext,missingAspects:a.qualityAssessorResult.missingAspects})).map(a=>({...a,searchQuery:a.queryRefinerResult?.refinedQuery||a.searchQuery})).when(!1).map(a=>a).merge().endWhile().map(a=>({...a,allEvidence:a.retrievedContexts.length>0?a.retrievedContexts:[]})).while(a=>a.iteration<a.maxIterations&&a.needsMoreInfo).map(a=>({...a,iteration:a.iteration+1})).branch(a=>a.iteration===1).when(!0).execute("questionDecomposer",a=>({complexQuestion:a.originalQuestion})).map(a=>({...a,currentQueries:a.questionDecomposerResult.subQuestions})).when(!1).map(a=>({...a,currentQueries:a.gapAnalyzerResult?.focusedQueries||[]})).merge().map(async a=>{let l=a.currentQueries||[],u=l.length>0?await Promise.all(l.filter(Boolean).map(c=>i(c))):[];return{...a,retrievalResults:u}}).execute("evidenceSynthesizer",a=>{let l=Array.isArray(a.allEvidence)?a.allEvidence:[],u=Array.isArray(a.retrievalResults)?a.retrievalResults:[],c=[...l,...u].filter(Boolean);return{collectedEvidence:c.length>0?c:["No evidence collected yet"],originalQuestion:a.originalQuestion}}).execute("gapAnalyzer",a=>({synthesizedEvidence:a.evidenceSynthesizerResult.synthesizedEvidence,evidenceGaps:a.evidenceSynthesizerResult.evidenceGaps,originalQuestion:a.originalQuestion})).map(a=>({...a,allEvidence:[...Array.isArray(a.allEvidence)?a.allEvidence:[],...Array.isArray(a.retrievalResults)?a.retrievalResults:[]],evidenceSources:[...Array.isArray(a.evidenceSources)?a.evidenceSources:[],`Iteration ${a.iteration} sources`],needsMoreInfo:a.gapAnalyzerResult.needsMoreInfo,synthesizedEvidence:a.evidenceSynthesizerResult.synthesizedEvidence})).endWhile().execute("answerGenerator",a=>({finalContext:(()=>{let l=(a.accumulatedContext||"").toString().trim();if(l.length>0)return l;let u=(a.synthesizedEvidence||"").toString().trim();if(u.length>0)return u;let p=(Array.isArray(a.allEvidence)?a.allEvidence.filter(Boolean).join(`
802
+ `)}async function id(i,e,t){if(i.length===0)return[];let n=new Array(i.length),r=0,o=Math.max(1,Math.min(e,i.length)),s=Array.from({length:o},async()=>{for(;;){let a=r++;if(a>=i.length)return;let l=i[a];if(l===void 0)return;n[a]=await t(l,a)}});return await Promise.all(s),n}function ad(i,e){if(!i.properties||e.size===0)return i;let t=Object.fromEntries(Object.entries(i.properties).filter(([r])=>!e.has(r))),n=i.required?.filter(r=>!e.has(r));return{...i,properties:t,...n!==void 0?{required:n}:{}}}function ld(i){return i.split(/[^a-zA-Z0-9]/).map((n,r)=>{let o=n.toLowerCase();return r>0&&o&&o[0]?o[0].toUpperCase()+o.slice(1):o}).join("")}var $u=(i,e)=>{let t=e?.maxHops??3,n=e?.qualityThreshold??.8,r=e?.maxIterations??2,o=e?.qualityTarget??.85,s=e?.disableQualityHealing??!1;return ns({logger:e?.logger,debug:e?.debug}).node("queryGenerator","originalQuestion:string, previousContext?:string -> searchQuery:string, queryReasoning:string").node("contextualizer","retrievedDocument:string, accumulatedContext?:string -> enhancedContext:string").node("qualityAssessor","currentContext:string, originalQuestion:string -> completenessScore:number, missingAspects:string[]").node("questionDecomposer","complexQuestion:string -> subQuestions:string[], decompositionReason:string").node("evidenceSynthesizer","collectedEvidence:string[], originalQuestion:string -> synthesizedEvidence:string, evidenceGaps:string[]").node("gapAnalyzer","synthesizedEvidence:string, evidenceGaps:string[], originalQuestion:string -> needsMoreInfo:boolean, focusedQueries:string[]").node("answerGenerator","finalContext:string, originalQuestion:string -> comprehensiveAnswer:string, confidenceLevel:number").node("queryRefiner","originalQuestion:string, currentContext:string, missingAspects:string[] -> refinedQuery:string").node("qualityValidator","generatedAnswer:string, userQuery:string -> qualityScore:number, issues:string[]").node("answerHealer","originalAnswer:string, healingDocument:string, issues?:string[] -> healedAnswer:string").map(a=>({...a,maxHops:t,qualityThreshold:n,maxIterations:r,qualityTarget:o,disableQualityHealing:s,currentHop:0,accumulatedContext:"",retrievedContexts:[],completenessScore:0,searchQuery:a.originalQuestion,shouldContinue:!0,iteration:0,allEvidence:[],evidenceSources:[],needsMoreInfo:!0,healingAttempts:0,currentQuality:0,shouldContinueHealing:!0,currentAnswer:"",currentIssues:[]})).while(a=>a.currentHop<a.maxHops&&a.completenessScore<a.qualityThreshold&&a.shouldContinue).map(a=>({...a,currentHop:a.currentHop+1})).execute("queryGenerator",a=>({originalQuestion:a.originalQuestion,previousContext:a.accumulatedContext||void 0})).map(async a=>{let l=a.queryGeneratorResult?.searchQuery||a.searchQuery||a.originalQuestion,u=await i(l);return{...a,retrievalResult:{retrievedDocument:u,retrievalConfidence:.9}}}).execute("contextualizer",a=>({retrievedDocument:a.retrievalResult.retrievedDocument,accumulatedContext:a.accumulatedContext||void 0})).execute("qualityAssessor",a=>({currentContext:a.contextualizerResult.enhancedContext,originalQuestion:a.originalQuestion})).map(a=>({...a,accumulatedContext:a.contextualizerResult.enhancedContext,retrievedContexts:[...a.retrievedContexts,a.retrievalResult.retrievedDocument],completenessScore:a.qualityAssessorResult.completenessScore,searchQuery:a.queryGeneratorResult.searchQuery,shouldContinue:a.qualityAssessorResult.completenessScore<a.qualityThreshold})).branch(a=>a.shouldContinue&&a.currentHop<a.maxHops).when(!0).execute("queryRefiner",a=>({originalQuestion:a.originalQuestion,currentContext:a.accumulatedContext,missingAspects:a.qualityAssessorResult.missingAspects})).map(a=>({...a,searchQuery:a.queryRefinerResult?.refinedQuery||a.searchQuery})).when(!1).map(a=>a).merge().endWhile().map(a=>({...a,allEvidence:a.retrievedContexts.length>0?a.retrievedContexts:[]})).while(a=>a.iteration<a.maxIterations&&a.needsMoreInfo).map(a=>({...a,iteration:a.iteration+1})).branch(a=>a.iteration===1).when(!0).execute("questionDecomposer",a=>({complexQuestion:a.originalQuestion})).map(a=>({...a,currentQueries:a.questionDecomposerResult.subQuestions})).when(!1).map(a=>({...a,currentQueries:a.gapAnalyzerResult?.focusedQueries||[]})).merge().map(async a=>{let l=a.currentQueries||[],u=l.length>0?await Promise.all(l.filter(Boolean).map(c=>i(c))):[];return{...a,retrievalResults:u}}).execute("evidenceSynthesizer",a=>{let l=Array.isArray(a.allEvidence)?a.allEvidence:[],u=Array.isArray(a.retrievalResults)?a.retrievalResults:[],c=[...l,...u].filter(Boolean);return{collectedEvidence:c.length>0?c:["No evidence collected yet"],originalQuestion:a.originalQuestion}}).execute("gapAnalyzer",a=>({synthesizedEvidence:a.evidenceSynthesizerResult.synthesizedEvidence,evidenceGaps:a.evidenceSynthesizerResult.evidenceGaps,originalQuestion:a.originalQuestion})).map(a=>({...a,allEvidence:[...Array.isArray(a.allEvidence)?a.allEvidence:[],...Array.isArray(a.retrievalResults)?a.retrievalResults:[]],evidenceSources:[...Array.isArray(a.evidenceSources)?a.evidenceSources:[],`Iteration ${a.iteration} sources`],needsMoreInfo:a.gapAnalyzerResult.needsMoreInfo,synthesizedEvidence:a.evidenceSynthesizerResult.synthesizedEvidence})).endWhile().execute("answerGenerator",a=>({finalContext:(()=>{let l=(a.accumulatedContext||"").toString().trim();if(l.length>0)return l;let u=(a.synthesizedEvidence||"").toString().trim();if(u.length>0)return u;let p=(Array.isArray(a.allEvidence)?a.allEvidence.filter(Boolean).join(`
801
803
  `):"").toString().trim();return p.length>0?p:"No context available."})(),originalQuestion:a.originalQuestion})).branch(a=>!a.disableQualityHealing).when(!0).execute("qualityValidator",a=>({generatedAnswer:a.answerGeneratorResult.comprehensiveAnswer,userQuery:a.originalQuestion})).map(a=>({...a,currentAnswer:a.answerGeneratorResult.comprehensiveAnswer,currentQuality:a.qualityValidatorResult.qualityScore,currentIssues:a.qualityValidatorResult.issues,shouldContinueHealing:a.qualityValidatorResult.qualityScore<a.qualityTarget})).while(a=>a.healingAttempts<3&&a.shouldContinueHealing).map(a=>({...a,healingAttempts:a.healingAttempts+1})).map(async a=>{let l=a.currentIssues||[],u=l.length>0?`${a.originalQuestion} addressing issues: ${l.join(", ")}`:`${a.originalQuestion} quality improvement`,c=await i(u);return{...a,healingResult:{healingDocument:c}}}).execute("answerHealer",a=>({originalAnswer:a.currentAnswer,healingDocument:a.healingResult.healingDocument,issues:a.currentIssues})).execute("qualityValidator",a=>({generatedAnswer:a.answerHealerResult.healedAnswer,userQuery:a.originalQuestion})).map(a=>({...a,currentAnswer:a.answerHealerResult.healedAnswer,currentQuality:a.qualityValidatorResult.qualityScore,currentIssues:a.qualityValidatorResult.issues,shouldContinueHealing:a.qualityValidatorResult.qualityScore<a.qualityTarget})).endWhile().when(!1).map(a=>({...a,currentAnswer:a.answerGeneratorResult.comprehensiveAnswer,currentQuality:1,currentIssues:[],shouldContinueHealing:!1})).merge().returns(a=>({finalAnswer:a.currentAnswer,totalHops:a.currentHop,retrievedContexts:a.retrievedContexts,iterationCount:a.iteration,healingAttempts:a.healingAttempts,qualityAchieved:a.currentQuality}))};
802
804
  //# sourceMappingURL=index.cjs.map