@ax-llm/ax 14.0.35 → 14.0.37

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
@@ -10,8 +10,8 @@
10
10
  `)}[Symbol.for("nodejs.util.inspect.custom")](t,n){return this.toString()}},Le=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=fe()}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
11
  `)}[Symbol.for("nodejs.util.inspect.custom")](t,n){return this.toString()}};async function Ds(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 $s(i,e){return Math.min(e.maxDelayMs,e.initialDelayMs*e.backoffFactor**i)*(.75+Math.random()*.5)}function vl(){return{startTime:Date.now(),retryCount:0}}function Gs(i){i.retryCount++,i.lastRetryTime=Date.now()}function Ls(i,e,t,n){return t>=n.maxRetries?!1:e&&n.retryableStatusCodes.includes(e)?!0:i instanceof je&&!(i instanceof rt)}var Ce=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={...wl,...i.retry},n=i.timeout,r=vl(),o,s=new URL(i.url),a=`${[s.pathname,i.name].filter(Boolean).join("/").replace(/\/+/g,"/")}${s.search}`,l=new URL(a,s);if(i.corsProxy){let u=l.href;l=new URL(`${i.corsProxy}?url=${encodeURIComponent(u)}`)}let c=fe();if(i.validateRequest&&!await i.validateRequest(e))throw new Xe("Invalid request data",l.href,e,{validation:"request"});i.span?.setAttributes({"http.request.method":i.put?"PUT":"POST","url.full":l.href,"request.id":c,"request.startTime":r.startTime});let p=0;for(;;){let u=new AbortController;if(i.abortSignal){if(i.abortSignal.aborted)throw new It(l.href,i.abortSignal.reason,e,{metrics:r});let d=()=>{u.abort(i.abortSignal.reason||"User aborted request")};i.abortSignal.addEventListener("abort",d,{once:!0});let m=u.abort.bind(u);u.abort=g=>{i.abortSignal.removeEventListener("abort",d),m(g)}}n&&(o=setTimeout(()=>{u.abort("Request timeout")},n));try{let d=await(i.fetch??fetch)(l,{method:i.put?"PUT":"POST",headers:{"Content-Type":"application/json","X-Request-ID":c,"X-Retry-Count":p.toString(),...i.headers},body:JSON.stringify(e),signal:u.signal});if(o&&clearTimeout(o),d.status===401||d.status===403){let x=await Ds(d);throw new rt(l.href,e,x,{metrics:r})}if(d.status>=400&&Ls(new Error,d.status,p,t)){let x=$s(p,t);p++,Gs(r),i.span?.addEvent("retry",{attempt:p,delay:x,status:d.status,"metrics.startTime":r.startTime,"metrics.retryCount":r.retryCount,"metrics.lastRetryTime":r.lastRetryTime}),await new Promise(y=>setTimeout(y,x));continue}if(d.status>=400){let x=await Ds(d);throw new ut(d.status,d.statusText,l.href,e,x,{metrics:r})}if(!i.stream){let x=await d.json();if(i.validateResponse&&!await i.validateResponse(x))throw new Xe("Invalid response data",l.href,e,{validation:"response"});return i.span?.setAttributes({"response.time":Date.now()-r.startTime,"response.retries":r.retryCount}),x}if(!d.body)throw new Xe("Response body is null",l.href,e,{metrics:r});let m,g=0;if(typeof window<"u"&&typeof EventSource<"u")return new ReadableStream({start(x){let y=d.body.getReader(),b=new TextDecoder,S="";async function I(){try{for(;;){let{done:T,value:C}=await y.read();if(T){A=!0,x.close();break}S+=b.decode(C,{stream:!0});let N=S.split(`
12
12
 
13
- `);S=N.pop()||"";for(let U of N){if(!U.trim())continue;let P=U.split(`
14
- `),w="",O="message";for(let R of P)R.startsWith("data: ")?w=R.slice(6):R.startsWith("event: ")&&(O=R.slice(7));if(w){if(w==="[DONE]"){x.close();return}try{let R=JSON.parse(w);m=R,g++,r.streamChunks=g,r.lastChunkTime=Date.now(),x.enqueue(R),i.span?.addEvent("stream.chunk",{"stream.chunks":g,"stream.duration":Date.now()-r.startTime,"response.retries":r.retryCount,"sse.event.type":O})}catch(R){i.debug&&console.warn("Skipping non-JSON SSE data:",w,R)}}}}}catch(T){let C=T,N={...r,streamDuration:Date.now()-r.startTime};C.name==="AbortError"||C.message?.includes("aborted")?x.error(new Ge(l.href,e,m,{streamMetrics:N})):x.error(new je(C,l.href,e,"[ReadableStream - consumed during streaming]",{streamMetrics:N}))}finally{y.releaseLock()}}I()}});let h=new TransformStream({transform(x,y){m=x,g++,r.streamChunks=g,r.lastChunkTime=Date.now(),y.enqueue(x),i.span?.addEvent("stream.chunk",{"stream.chunks":g,"stream.duration":Date.now()-r.startTime,"response.retries":r.retryCount})}}),A=!1;return new ReadableStream({start(x){let y=d.body.pipeThrough(new Sl).pipeThrough(new Jn).pipeThrough(h).getReader();async function b(){try{for(;;){let{done:S,value:I}=await y.read();if(S){A||(A=!0,x.close());break}if(A)break;x.enqueue(I)}}catch(S){let I=S,T={...r,streamDuration:Date.now()-r.startTime};throw I.name==="AbortError"||I.message?.includes("aborted")?x.error(new Ge(l.href,e,m,{streamMetrics:T})):I instanceof TypeError&&I.message.includes("cancelled")?x.error(new Ge(l.href,e,m,{streamMetrics:T,cancelReason:"Stream cancelled by client"})):x.error(new je(I,l.href,e,"[ReadableStream - consumed during streaming]",{streamMetrics:T})),I}finally{o&&clearTimeout(o),y.releaseLock()}}b()},cancel(){A=!0}})}catch(d){if(d instanceof Error&&d.name==="AbortError")throw i.abortSignal?.aborted?new It(l.href,i.abortSignal.reason,e,{metrics:r}):new dt(l.href,n||0,e,{metrics:r});if(i.span?.isRecording()&&(i.span.recordException(d),i.span.setAttributes({"error.time":Date.now()-r.startTime,"error.retries":r.retryCount})),d instanceof je&&Ls(d,void 0,p,t)){let m=$s(p,t);p++,Gs(r),i.span?.addEvent("retry",{attempt:p,delay:m,error:d.message,"metrics.startTime":r.startTime,"metrics.retryCount":r.retryCount,"metrics.lastRetryTime":r.lastRetryTime}),await new Promise(g=>setTimeout(g,m));continue}throw d instanceof Ie&&(d.context.metrics=r),d}finally{o!==void 0&&clearTimeout(o)}}};var Ct=require("@opentelemetry/api");var se={signatureStrict:!0,tracer:void 0,meter:void 0,logger:void 0,optimizerLogger:void 0,debug:void 0,functionResultFormatter:i=>typeof i=="string"?i:i==null?"":JSON.stringify(i,null,2)};var Te=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 cp=new Te,Us=i=>{console.log(i)},Bs=(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")}
13
+ `);S=N.pop()||"";for(let U of N){if(!U.trim())continue;let k=U.split(`
14
+ `),w="",E="message";for(let R of k)R.startsWith("data: ")?w=R.slice(6):R.startsWith("event: ")&&(E=R.slice(7));if(w){if(w==="[DONE]"){x.close();return}try{let R=JSON.parse(w);m=R,g++,r.streamChunks=g,r.lastChunkTime=Date.now(),x.enqueue(R),i.span?.addEvent("stream.chunk",{"stream.chunks":g,"stream.duration":Date.now()-r.startTime,"response.retries":r.retryCount,"sse.event.type":E})}catch(R){i.debug&&console.warn("Skipping non-JSON SSE data:",w,R)}}}}}catch(T){let C=T,N={...r,streamDuration:Date.now()-r.startTime};C.name==="AbortError"||C.message?.includes("aborted")?x.error(new Ge(l.href,e,m,{streamMetrics:N})):x.error(new je(C,l.href,e,"[ReadableStream - consumed during streaming]",{streamMetrics:N}))}finally{y.releaseLock()}}I()}});let h=new TransformStream({transform(x,y){m=x,g++,r.streamChunks=g,r.lastChunkTime=Date.now(),y.enqueue(x),i.span?.addEvent("stream.chunk",{"stream.chunks":g,"stream.duration":Date.now()-r.startTime,"response.retries":r.retryCount})}}),A=!1;return new ReadableStream({start(x){let y=d.body.pipeThrough(new Sl).pipeThrough(new Jn).pipeThrough(h).getReader();async function b(){try{for(;;){let{done:S,value:I}=await y.read();if(S){A||(A=!0,x.close());break}if(A)break;x.enqueue(I)}}catch(S){let I=S,T={...r,streamDuration:Date.now()-r.startTime};throw I.name==="AbortError"||I.message?.includes("aborted")?x.error(new Ge(l.href,e,m,{streamMetrics:T})):I instanceof TypeError&&I.message.includes("cancelled")?x.error(new Ge(l.href,e,m,{streamMetrics:T,cancelReason:"Stream cancelled by client"})):x.error(new je(I,l.href,e,"[ReadableStream - consumed during streaming]",{streamMetrics:T})),I}finally{o&&clearTimeout(o),y.releaseLock()}}b()},cancel(){A=!0}})}catch(d){if(d instanceof Error&&d.name==="AbortError")throw i.abortSignal?.aborted?new It(l.href,i.abortSignal.reason,e,{metrics:r}):new dt(l.href,n||0,e,{metrics:r});if(i.span?.isRecording()&&(i.span.recordException(d),i.span.setAttributes({"error.time":Date.now()-r.startTime,"error.retries":r.retryCount})),d instanceof je&&Ls(d,void 0,p,t)){let m=$s(p,t);p++,Gs(r),i.span?.addEvent("retry",{attempt:p,delay:m,error:d.message,"metrics.startTime":r.startTime,"metrics.retryCount":r.retryCount,"metrics.lastRetryTime":r.lastRetryTime}),await new Promise(g=>setTimeout(g,m));continue}throw d instanceof Ie&&(d.context.metrics=r),d}finally{o!==void 0&&clearTimeout(o)}}};var Ct=require("@opentelemetry/api");var se={signatureStrict:!0,tracer:void 0,meter:void 0,logger:void 0,optimizerLogger:void 0,debug:void 0,functionResultFormatter:i=>typeof i=="string"?i:i==null?"":JSON.stringify(i,null,2)};var Te=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 cp=new Te,Us=i=>{console.log(i)},Bs=(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")}
15
15
  ${n(i.content,"magenta")}`;case"function":return`${n("[ FUNCTION RESULT ]","yellow")}
16
16
  ${n(i.result??"[No result]","yellowDim")}`;case"user":{let r=`${n("[ USER ]","greenBright")}
17
17
  `;if(typeof i.content=="string")return r+n(i.content,"green");let o=i.content.map(s=>{if(s.type==="text")return n(s.text,"green");if(s.type==="image"){let a=e?"[Image]":`[Image: ${s.image}]`;return n(a,"green")}if(s.type==="audio"){let a=e?"[Audio]":`[Audio: ${s.data}]`;return n(a,"green")}return n("[Unknown content type]","gray")});return r+o.join(`
@@ -35,13 +35,17 @@ Result: ${s.result}`),a<r.value.length-1&&(o+=`
35
35
  ${t}
36
36
  `)});break;case"ChatResponseResults":o=`
37
37
  ${e.cyanBright("[ CHAT RESPONSE ]")}
38
- `,r.value.forEach((s,a)=>{let l=[];(s.thoughtBlock?.data||s.thought)&&l.push(e.gray(`[thought${s.thoughtBlock?.encrypted?" (redacted)":""}] `+(s.thoughtBlock?.data??s.thought??""))),s.content&&l.push(e.cyan(s.content)),l.length===0&&l.push(e.gray("[No content]")),o+=l.join(`
38
+ `,r.value.forEach((s,a)=>{let l=[];(s.thoughtBlock?.data||s.thought)&&l.push(e.gray(`[THOUGHT${s.thoughtBlock?.encrypted?" (redacted)":""}]
39
+ `+(s.thoughtBlock?.data??s.thought??""))),s.content&&l.push(e.cyan(s.content)),l.length===0&&l.push(e.gray("[No content]")),o+=l.join(`
39
40
  `),a<r.value.length-1&&(o+=`
40
41
  ${t}
41
- `)});break;case"ChatResponseStreamingResult":{let s=r.value.thought,a=s||r.value.delta||r.value.content||"";o=s?e.gray(`[thought] ${s}`):e.cyanBright(a);return}case"ChatResponseStreamingDoneResult":{o=`
42
+ `)});break;case"ChatResponseStreamingResult":{let s=r.value.thought,a=s||r.value.delta||r.value.content||"";o=s?e.gray(`[THOUGHT]
43
+ ${s}`):e.cyanBright(a);break}case"ChatResponseStreamingDoneResult":{o=`
42
44
  ${e.cyanBright("[ CHAT RESPONSE ]")}
43
45
  ${t}
44
- `,r.value.content&&(o+=e.cyanBright(r.value.content)),r.value.functionCalls&&(o+=e.cyanBright(JSON.stringify(r.value.functionCalls,null,2)));break}case"FunctionError":o=`
46
+ `,r.value.content&&(o+=e.cyanBright(r.value.content)),(r.value.thoughtBlock?.data||r.value.thought)&&(o+=`
47
+ `,o+=e.gray(`[THOUGHT${r.value.thoughtBlock?.encrypted?" (redacted)":""}]
48
+ `+(r.value.thoughtBlock?.data??r.value.thought??""))),r.value.functionCalls&&(o+=e.cyanBright(JSON.stringify(r.value.functionCalls,null,2)));break}case"FunctionError":o=`
45
49
  ${e.redBright(`[ FUNCTION ERROR #${r.index} ]`)}
46
50
  ${t}
47
51
  ${e.white(r.fixingInstructions)}
@@ -95,11 +99,13 @@ Result: ${o.result}`,s<n.value.length-1&&(r+=`
95
99
  ${e}
96
100
  `)});break;case"ChatResponseResults":r=`
97
101
  [ CHAT RESPONSE ]
98
- `,n.value.forEach((o,s)=>{r+=o.content||"[No content]",s<n.value.length-1&&(r+=`
102
+ `,n.value.forEach((o,s)=>{let a=[];(o.thoughtBlock?.data||o.thought)&&a.push(`[thought${o.thoughtBlock?.encrypted?" (redacted)":""}] `+(o.thoughtBlock?.data??o.thought??"")),o.content&&a.push(o.content),a.length===0&&a.push("[No content]"),r+=a.join(`
103
+ `),s<n.value.length-1&&(r+=`
99
104
  ${e}
100
105
  `)});break;case"ChatResponseStreamingResult":return;case"ChatResponseStreamingDoneResult":{r=`
101
106
  [ CHAT RESPONSE ]
102
- `,n.value.content&&(r+=n.value.content),n.value.functionCalls&&(r+=JSON.stringify(n.value.functionCalls,null,2));break}case"FunctionError":r=`
107
+ `,n.value.content&&(r+=n.value.content),(n.value.thoughtBlock?.data||n.value.thought)&&(r+=`
108
+ `,r+=`[thought${n.value.thoughtBlock?.encrypted?" (redacted)":""}] `+(n.value.thoughtBlock?.data??n.value.thought??"")),n.value.functionCalls&&(r+=JSON.stringify(n.value.functionCalls,null,2));break}case"FunctionError":r=`
103
109
  [ FUNCTION ERROR #${n.index} ]
104
110
  ${e}
105
111
  ${n.fixingInstructions}
@@ -142,8 +148,8 @@ ${e}
142
148
  `,o.description&&(r+=` ${o.description}
143
149
  `)}),r+=`${e}
144
150
  `;break}default:r=JSON.stringify(n,null,2)}i(r)}};var q={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"},Ue={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"},ko=(r=>(r.COMPLETION="completion",r.CHAT="chat",r.RERANK="rerank",r.UNKNOWN="unknown",r))(ko||{}),Mo=(o=>(o.WORKFLOW="workflow",o.TASK="task",o.AGENT="agent",o.TOOL="tool",o.UNKNOWN="unknown",o))(Mo||{});var Eo=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()}},Yn=class extends TransformStream{constructor(e,t){super(new Eo(e,t))}};function Xn(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 js=(i,e,t,n)=>{let r=n?i.filter(s=>s.role!=="system"):[...i];t({name:"ChatRequestChatPrompt",step:e,value:r})};var Hs=(i,e)=>{if(!i.results)return;let t={name:"ChatResponseResults",value:i.results};e(t)},Ks=(i,e,t)=>{t({name:"ChatResponseStreamingResult",index:e,value:i})};function Ws(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?Xn(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 Vs=(i,e)=>{e({name:"FunctionResults",value:i})},Po=(i,e,t,n)=>{n({name:"FunctionError",index:e,fixingInstructions:t,error:i})},Js=(i,e,t,n)=>{n({name:"ValidationError",index:e,fixingInstructions:t,error:i})},Qs=(i,e,t,n)=>{n({name:"AssertionError",index:e,fixingInstructions:t,error:i})},Ys=(i,e,t)=>{t({name:"RefusalError",index:e,error:i})};var Xs=(i,e,t)=>{t({name:"EmbedRequest",embedModel:e,value:i})},Zs=(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)},ei=(i,e,t,n)=>{n({name:"ResultPickerUsed",sampleCount:i,selectedIndex:e,latency:t})};var Fo=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},Zn,ti=i=>{if(Zn)return Zn;if(i)return Zn=Ol(i),Zn};var Ol=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)"})}),ni=(i,e,t,n,r)=>{try{if(i.latencyHistogram){let o=Fo({operation:e,ai_service:n,...r?{model:r}:{}});i.latencyHistogram.record(t,o)}}catch(o){console.warn("Failed to record latency metric:",o)}},ri=(i,e,t,n,r,o,s)=>{let a={operation:e,ai_service:o,...s?{model:s}:{}};i.meanLatencyGauge&&i.meanLatencyGauge.record(t,a),i.p95LatencyGauge&&i.p95LatencyGauge.record(n,a),i.p99LatencyGauge&&i.p99LatencyGauge.record(r,a)},oi=(i,e,t,n)=>{try{if(i.errorCounter){let r=Fo({operation:e,ai_service:t,...n?{model:n}:{}});i.errorCounter.add(1,r)}}catch(r){console.warn("Failed to record error metric:",r)}},si=(i,e,t,n,r)=>{i.errorRateGauge&&i.errorRateGauge.record(t*100,{operation:e,ai_service:n,...r?{model:r}:{}})},ii=(i,e,t,n)=>{i.requestCounter&&i.requestCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{}})},pn=(i,e,t,n,r)=>{try{let o=Fo({ai_service:n,...r?{model:r}:{}});i.tokenCounter&&i.tokenCounter.add(t,{token_type:e,...o}),e==="input"&&i.inputTokenCounter&&i.inputTokenCounter.add(t,o),e==="output"&&i.outputTokenCounter&&i.outputTokenCounter.add(t,o)}catch(o){console.warn("Failed to record token metric:",o)}},ai=(i,e,t,n,r)=>{t&&i.streamingRequestsCounter&&i.streamingRequestsCounter.add(1,{operation:e,ai_service:n,...r?{model:r}:{}})},li=(i,e,t,n,r)=>{let o={function_name:e,...n?{ai_service:n}:{},...r?{model:r}:{}};i.functionCallsCounter&&i.functionCallsCounter.add(1,o),t&&i.functionCallLatencyHistogram&&i.functionCallLatencyHistogram.record(t,o)},_o=(i,e,t,n,r)=>{i.requestSizeHistogram&&i.requestSizeHistogram.record(t,{operation:e,ai_service:n,...r?{model:r}:{}})},No=(i,e,t,n,r)=>{i.responseSizeHistogram&&i.responseSizeHistogram.record(t,{operation:e,ai_service:n,...r?{model:r}:{}})},ci=(i,e,t,n,r)=>{let o={...n?{ai_service:n}:{},...r?{model:r}:{}};e!==void 0&&i.temperatureGauge&&i.temperatureGauge.record(e,o),t!==void 0&&i.maxTokensGauge&&i.maxTokensGauge.record(t,o)},Do=(i,e,t,n,r)=>{i.estimatedCostCounter&&i.estimatedCostCounter.add(t,{operation:e,ai_service:n,...r?{model:r}:{}})},pi=(i,e,t,n)=>{i.promptLengthHistogram&&i.promptLengthHistogram.record(e,{ai_service:t,...n?{model:n}:{}})},ui=(i,e,t,n)=>{i.contextWindowUsageGauge&&i.contextWindowUsageGauge.record(e,{ai_service:t,...n?{model:n}:{}})},di=(i,e,t,n)=>{i.timeoutsCounter&&i.timeoutsCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{}})},mi=(i,e,t,n)=>{i.abortsCounter&&i.abortsCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{}})},gi=(i,e,t,n)=>{i.thinkingBudgetUsageCounter&&i.thinkingBudgetUsageCounter.add(e,{ai_service:t,...n?{model:n}:{}})},hi=(i,e,t,n,r)=>{(e||t)&&i.multimodalRequestsCounter&&i.multimodalRequestsCounter.add(1,{ai_service:n,has_images:e.toString(),has_audio:t.toString(),...r?{model:r}:{}})};function fi(i){try{return JSON.stringify(i,null,2)}catch{return String(i)}}function Be(i,e={}){let t=[i];throw e.fieldPath!==void 0&&t.push(`Field: ${e.fieldPath}`),e.value!==void 0&&t.push(`Value: ${fi(e.value)}`),e.note&&t.push(`Note: ${e.note}`),e.item!==void 0&&t.push(`Chat item: ${fi(e.item)}`),new Error(t.join(`
145
- `))}function Tt(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 p=o.fileUri;if(!p||p.trim()==="")throw new Error(`User message file content at index ${r} fileUri cannot be empty, received: ${e(p)}`)}if(l){let p=o.data;if(!p||p.trim()==="")throw new Error(`User message file content at index ${r} data cannot be empty, received: ${e(p)}`)}let c="mimeType"in o&&typeof o.mimeType=="string"?o.mimeType:null;if(!c||c.trim()==="")throw new Error(`User message file content at index ${r} must have a mimeType, received: ${e(c)}`);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 n=="string"&&n.trim()!=="",s=Array.isArray(r)&&r.length>0;if(!o&&!s&&Be("Assistant message must include non-empty content or at least one function call",{fieldPath:"content | functionCalls",value:{content:n,functionCalls:r},item:i}),n!==void 0&&typeof n!="string"&&Be("Assistant message content must be a string",{fieldPath:"content",value:n,item:i}),r!==void 0&&!Array.isArray(r)&&Be("Assistant message functionCalls must be an array when provided",{fieldPath:"functionCalls",value:r,item:i}),Array.isArray(r))for(let a=0;a<r.length;a++){let l=r[a];if((!l||typeof l!="object")&&Be("functionCalls entry must be an object",{fieldPath:`functionCalls[${a}]`,value:l,item:i}),(!("id"in l)||typeof l.id!="string"||l.id.trim()==="")&&Be("functionCalls entry must include a non-empty string id",{fieldPath:`functionCalls[${a}].id`,value:l.id,item:i}),(!("type"in l)||l.type!=="function")&&Be("functionCalls entry must have type 'function'",{fieldPath:`functionCalls[${a}].type`,value:l.type,item:i}),!("function"in l)||!l.function)Be("functionCalls entry must include a function object",{fieldPath:`functionCalls[${a}].function`,value:l.function,item:i});else{let c=l.function;(!("name"in c)||typeof c.name!="string"||c.name.trim()==="")&&Be("functionCalls entry must include a non-empty function name",{fieldPath:`functionCalls[${a}].function.name`,value:c?.name,item:i}),c.params!==void 0&&typeof c.params!="string"&&typeof c.params!="object"&&Be("functionCalls entry params must be a string or object when provided",{fieldPath:`functionCalls[${a}].function.params`,value:c.params,item:i})}}if(i.name!==void 0){let a=i.name;(typeof a!="string"||a.trim()==="")&&Be("Assistant message name must be a non-empty string when provided",{fieldPath:"name",value:a,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"&&Be("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 er(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.thoughtBlock&&!r.functionCalls&&!r.finishReason)throw new Error(`Chat response result at index ${n} must have at least one of: content, thought, functionCalls, or finishReason, received: ${e({content:r.content,thought:r.thought,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.thoughtBlock!==void 0){if(typeof r.thoughtBlock!="object"||r.thoughtBlock===null)throw new Error(`Chat response result thoughtBlock at index ${n} must be an object, received: ${e(r.thoughtBlock)}`);let o=r.thoughtBlock;if(typeof o.data!="string")throw new Error(`Chat response result thoughtBlock.data at index ${n} must be a string, received: ${e(o.data)}`);if(typeof o.encrypted!="boolean")throw new Error(`Chat response result thoughtBlock.encrypted at index ${n} must be a boolean, received: ${e(o.encrypted)}`);if(o.signature!==void 0&&typeof o.signature!="string")throw new Error(`Chat response result thoughtBlock.signature at index ${n} must be a string when provided, received: ${e(o.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 Q=()=>structuredClone({temperature:0}),me=()=>structuredClone({temperature:.4,frequencyPenalty:.2}),de=class{constructor(e,{name:t,apiURL:n,headers:r,modelInfo:o,defaults:s,options:a={},supportFor:l,models:c}){this.aiImpl=e;this.name=t,this.apiURL=n||"",this.headers=r,this.supportFor=l,this.tracer=a.tracer??se.tracer,this.meter=a.meter??se.meter,this.modelInfo=o,this.models=c,this.id=fe();let p=this.getModel(s.model)??s.model,u=this.getEmbedModel(s.embedModel)??s.embedModel;if(this.defaults={model:p,embedModel:u},!s.model||typeof s.model!="string"||s.model==="")throw new Error("No model defined");this.setOptions(a),c&&Ml(c)}#e=!1;rt;fetch;tracer;meter;timeout;excludeContentFromTrace;models;abortSignal;logger=se.logger??qs;corsProxy;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 ti(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??se.debug??!1,this.rt=e.rateLimiter,this.fetch=e.fetch,this.timeout=e.timeout,this.tracer=e.tracer??se.tracer,this.meter=e.meter??se.meter,this.excludeContentFromTrace=e.excludeContentFromTrace,this.abortSignal=e.abortSignal,this.logger=e.logger??se.logger??this.logger,this.corsProxy=e.corsProxy}getOptions(){return{debug:this.#e,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}}getLogger(){return this.logger}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){let n=this.metrics.latency[e];n.samples.push(t),n.samples.length>1e3&&n.samples.shift(),n.mean=n.samples.reduce((o,s)=>o+s,0)/n.samples.length,n.p95=this.calculatePercentile(n.samples,95),n.p99=this.calculatePercentile(n.samples,99);let r=this.getMetricsInstruments();if(r){let o=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel;ni(r,e,t,this.name,o),ri(r,e,n.mean,n.p95,n.p99,this.name,o)}}updateErrorMetrics(e,t){let n=this.metrics.errors[e];n.total++,t&&n.count++,n.rate=n.count/n.total;let r=this.getMetricsInstruments();if(r){let o=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel;ii(r,e,this.name,o),t&&oi(r,e,this.name,o),si(r,e,n.rate,this.name,o)}}recordTokenUsage(e){let t=this.getMetricsInstruments();if(t&&e?.tokens){let{promptTokens:n,completionTokens:r,totalTokens:o,thoughtsTokens:s}=e.tokens;n&&pn(t,"input",n,this.name,e.model),r&&pn(t,"output",r,this.name,e.model),o&&pn(t,"total",o,this.name,e.model),s&&pn(t,"thoughts",s,this.name,e.model)}}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){let n=this.getMetricsInstruments();if(!(!n||!e))for(let r of e)r&&typeof r=="object"&&"function"in r&&r.function&&typeof r.function=="object"&&"name"in r.function&&li(n,r.function.name,void 0,this.name,t)}recordTimeoutMetric(e){let t=this.getMetricsInstruments();if(t){let n=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel;di(t,e,this.name,n)}}recordAbortMetric(e){let t=this.getMetricsInstruments();if(t){let n=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel;mi(t,e,this.name,n)}}recordChatMetrics(e,t,n){let r=this.getMetricsInstruments();if(!r)return;let o=this.lastUsedChatModel,s=this.lastUsedModelConfig,a=s?.stream??!1;ai(r,"chat",a,this.name,o);let{hasImages:l,hasAudio:c}=this.detectMultimodalContent(e);hi(r,l,c,this.name,o);let p=this.calculatePromptLength(e);pi(r,p,this.name,o),ci(r,s?.temperature,s?.maxTokens,this.name,o),t?.thinkingTokenBudget&&this.modelUsage?.tokens?.thoughtsTokens&&gi(r,this.modelUsage.tokens.thoughtsTokens,this.name,o);let u=this.calculateRequestSize(e);if(_o(r,"chat",u,this.name,o),n&&!a){let d=n,m=this.calculateResponseSize(d);if(No(r,"chat",m,this.name,o),d.results)for(let h of d.results)h.functionCalls&&this.recordFunctionCallMetrics(h.functionCalls,this.lastUsedChatModel);let g=this.calculateContextWindowUsage(this.lastUsedChatModel,d.modelUsage);g>0&&ui(r,g,this.name,o);let f=this.estimateCost(this.lastUsedChatModel,d.modelUsage);f>0&&Do(r,"chat",f,this.name,o)}}recordEmbedMetrics(e,t){let n=this.getMetricsInstruments();if(!n)return;let r=this.lastUsedEmbedModel,o=this.calculateRequestSize(e);_o(n,"embed",o,this.name,r);let s=this.calculateResponseSize(t);No(n,"embed",s,this.name,r);let a=this.estimateCostByName(r,t.modelUsage);a>0&&Do(n,"embed",a,this.name,r)}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(([,c])=>c!==void 0))};try{return o=await this._chat1(e,l),o}catch(c){throw r=!0,c instanceof Error&&(c.message.includes("timeout")||c.name==="TimeoutError"?this.recordTimeoutMetric("chat"):(c.message.includes("abort")||c.name==="AbortError")&&this.recordAbortMetric("chat")),c}finally{let c=performance.now()-n;this.updateLatencyMetrics("chat",c),this.updateErrorMetrics("chat",r),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 c of e.chatPrompt)Tt(c);let r=this.getModelByKey(e.model),o={...this.aiImpl.getModelConfig(),...r?r.modelConfig:void 0,...e.modelConfig},s=this.modelInfo.find(c=>c.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(c=>c.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:Ct.SpanKind.SERVER,attributes:{[q.LLM_SYSTEM]:this.name,[q.LLM_OPERATION_NAME]:"chat",[q.LLM_REQUEST_MODEL]:n,[q.LLM_REQUEST_MAX_TOKENS]:o.maxTokens??"Not set",[q.LLM_REQUEST_TEMPERATURE]:o.temperature,[q.LLM_REQUEST_TOP_P]:o.topP??"Not set",[q.LLM_REQUEST_TOP_K]:o.topK??"Not set",[q.LLM_REQUEST_FREQUENCY_PENALTY]:o.frequencyPenalty??"Not set",[q.LLM_REQUEST_PRESENCE_PENALTY]:o.presencePenalty??"Not set",[q.LLM_REQUEST_STOP_SEQUENCES]:o.stopSequences?.join(", ")??"Not set",[q.LLM_REQUEST_LLM_IS_STREAMING]:o.stream??"Not set"}},t?.traceContext??Ct.context.active(),async c=>await this._chat2(n,o,e,t,c)):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;n.functions&&n.functions.length>0&&(a=n.functions.map(A=>this.cleanupFunctionSchema(A)));let l={...n,model:e,functions:a,modelConfig:t};this.lastUsedChatModel=e,this.lastUsedModelConfig=t,s&&js(l.chatPrompt,r?.stepIndex??0,r?.logger??this.logger,r?.debugHideSystemPrompt);let c=this.getFeatures(e).functions,p=r?.functionCallMode??"auto",d=p==="prompt"||p==="auto"&&!c?{...l,chatPrompt:l.chatPrompt.map(A=>{if(A.role==="assistant"){let{content:x,name:y,cache:b}=A;return{role:"assistant",content:x,name:y,cache:b}}return A.role==="function"?{role:"user",content:A.result}:A}),functions:[]}:l,m=async()=>{let[A,x]=await this.aiImpl.createChatReq(d,r);return o?.isRecording()&&kl(n,o,this.excludeContentFromTrace),await Ce({name:A.name,url:this.apiURL,localCall:A.localCall,headers:await this.buildHeaders(A.headers),stream:t.stream,timeout:this.timeout,debug:s,fetch:this.fetch,span:o,abortSignal:r?.abortSignal??this.abortSignal,corsProxy:this.corsProxy},x)},g=r?.rateLimiter??this.rt,f=g?await g(m,{modelUsage:this.modelUsage}):await m();if(t.stream){if(!this.aiImpl.createChatStreamResp)throw new Error("createChatStreamResp not implemented");let A=this.aiImpl.createChatStreamResp.bind(this),x=I=>T=>{let C=A(T,I);if(C.sessionId=r?.sessionId,!C.modelUsage){let N=this.aiImpl.getTokenUsage();N&&(C.modelUsage={ai:this.name,model:e,tokens:N})}if(this.modelUsage=C.modelUsage,this.recordTokenUsage(C.modelUsage),o?.isRecording()&&Ai(C,o,this.excludeContentFromTrace),s)for(let N of C.results)Ks(N,N.index,r?.logger??this.logger);return C},y=async I=>{o?.isRecording()&&o.end(),s&&Ws(I,r?.logger??this.logger)};if(typeof window<"u"){let I=f,T={},C=[],N=r?.abortSignal??this.abortSignal;return new ReadableStream({start:U=>{let P=I.getReader(),w=()=>{try{P.cancel().catch(()=>{})}catch{}try{this.recordAbortMetric("chat")}catch{}try{o?.isRecording()&&o.end()}catch{}try{U.error(new DOMException("Aborted","AbortError"))}catch{U.error(new Error("Aborted"))}};if(N){if(N.aborted){w();return}N.addEventListener("abort",w,{once:!0})}async function O(){try{for(;;){let{done:R,value:v}=await P.read();if(R){y&&await y(C),U.close();break}let k=x(T)(v);k&&(C.push(k),U.enqueue(k))}}catch(R){if(U.error(R),o?.isRecording())try{o.end()}catch{}}finally{if(P.releaseLock(),N)try{N.removeEventListener("abort",w)}catch{}}}O()}})}return f.pipeThrough(new Yn(x({}),y))}if(!this.aiImpl.createChatResp)throw new Error("createChatResp not implemented");let h=this.aiImpl.createChatResp(f);if(h.sessionId=r?.sessionId,!h.modelUsage){let A=this.aiImpl.getTokenUsage();A&&(h.modelUsage={ai:this.name,model:e,tokens:A})}return h.modelUsage&&(this.modelUsage=h.modelUsage,this.recordTokenUsage(h.modelUsage)),o?.isRecording()&&(Ai(h,o,this.excludeContentFromTrace),o.end()),s&&Hs(h,r?.logger??this.logger),h}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"):(l.message.includes("abort")||l.name==="AbortError")&&this.recordAbortMetric("embed")),l}finally{let l=performance.now()-n;this.updateLatencyMetrics("embed",l),this.updateErrorMetrics("embed",r),!r&&o&&this.recordEmbedMetrics(e,o)}}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:Ct.SpanKind.SERVER,attributes:{[q.LLM_SYSTEM]:this.name,[q.LLM_OPERATION_NAME]:"embeddings",[q.LLM_REQUEST_MODEL]:n}},t?.traceContext??Ct.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={...t,embedModel:e};this.lastUsedEmbedModel=e,s&&Xs(a.texts??[],e,n?.logger??this.logger);let l=async()=>{let[d,m]=await o(a);return await Ce({name:d.name,url:this.apiURL,localCall:d.localCall,headers:await this.buildHeaders(d.headers),debug:s,fetch:this.fetch,timeout:this.timeout,span:r,abortSignal:n?.abortSignal??this.abortSignal,corsProxy:this.corsProxy},m)},c=n?.rateLimiter??this.rt,p=c?await c(l,{modelUsage:this.embedModelUsage}):await l(),u=this.aiImpl.createEmbedResp?.(p);if(u.sessionId=n?.sessionId,!u.modelUsage){let d=this.aiImpl.getTokenUsage();d&&(u.modelUsage={ai:this.name,model:e,tokens:d})}return this.embedModelUsage=u.modelUsage,this.recordTokenUsage(u.modelUsage),r?.isRecording()&&u.modelUsage?.tokens&&r.addEvent(Ue.GEN_AI_USAGE,{[q.LLM_USAGE_INPUT_TOKENS]:u.modelUsage.tokens.promptTokens,[q.LLM_USAGE_OUTPUT_TOKENS]:u.modelUsage.tokens.completionTokens??0,[q.LLM_USAGE_TOTAL_TOKENS]:u.modelUsage.tokens.totalTokens}),s&&Zs(u.embeddings,n?.logger??this.logger),r?.end(),u}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}};function kl(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(Ue.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(Ue.GEN_AI_ASSISTANT_MESSAGE,a)}else if(o.content){let a={};t||(a.content=o.content),e.addEvent(Ue.GEN_AI_ASSISTANT_MESSAGE,a)}break}case"function":{let s={id:o.functionId};t||(s.content=o.result),e.addEvent(Ue.GEN_AI_TOOL_MESSAGE,s);break}}let r={};t||(r.content=n.join(`
146
- `)),e.addEvent(Ue.GEN_AI_USER_MESSAGE,r)}function Ai(i,e,t){if(i.modelUsage?.tokens){let n=i.modelUsage.tokens.thoughtsTokens?{[q.LLM_USAGE_THOUGHTS_TOKENS]:i.modelUsage.tokens.thoughtsTokens}:{};e.addEvent(Ue.GEN_AI_USAGE,{[q.LLM_USAGE_INPUT_TOKENS]:i.modelUsage.tokens.promptTokens,[q.LLM_USAGE_OUTPUT_TOKENS]:i.modelUsage.tokens.completionTokens??0,[q.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(Ue.GEN_AI_CHOICE,{finish_reason:r.finishReason,index:n,message:JSON.stringify(s,null,2)})}}function xi(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 Ml(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 un=(u=>(u.Claude41Opus="claude-opus-4-1-20250805",u.Claude4Opus="claude-opus-4-20250514",u.Claude4Sonnet="claude-sonnet-4-20250514",u.Claude37Sonnet="claude-3-7-sonnet-latest",u.Claude35Sonnet="claude-3-5-sonnet-latest",u.Claude35Haiku="claude-3-5-haiku-latest",u.Claude3Opus="claude-3-opus-latest",u.Claude3Sonnet="claude-3-sonnet-20240229",u.Claude3Haiku="claude-3-haiku-20240307",u.Claude21="claude-2.1",u.ClaudeInstant12="claude-instant-1.2",u))(un||{}),tr=(s=>(s.Claude37Sonnet="claude-3-7-sonnet",s.Claude35Haiku="claude-3-5-haiku",s.Claude35Sonnet="claude-3-5-sonnet",s.Claude35SonnetV2="claude-3-5-sonnet-v2",s.Claude3Haiku="claude-3-haiku",s.Claude3Opus="claude-3-opus",s))(tr||{});var dn=[{name:"claude-opus-4-1-20250805",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:75,maxTokens:32e3,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"claude-opus-4-20250514",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:75,maxTokens:32e3,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"claude-sonnet-4-20250514",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"claude-3-7-sonnet-latest",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"claude-3-5-sonnet-latest",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:8192},{name:"claude-3-5-haiku-latest",currency:"usd",promptTokenCostPer1M:.8,completionTokenCostPer1M:4,maxTokens:8192},{name:"claude-3-opus-latest",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:75,maxTokens:4096},{name:"claude-3-sonnet-20240229",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,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 nr=i=>{if(!i||typeof i!="object")return i;let e={...i};return delete e.additionalProperties,delete e.default,delete e.optional,delete e.oneOf,delete e.anyOf,delete e.allOf,e.properties&&typeof e.properties=="object"&&(e.properties=Object.fromEntries(Object.entries(e.properties).map(([t,n])=>[t,nr(n)]))),e.items&&(e.items=nr(e.items)),e},Go=()=>structuredClone({model:"claude-3-7-sonnet-latest",maxTokens:4e4,thinkingTokenBudgetLevels:{minimal:1024,low:5e3,medium:1e4,high:2e4,highest:32e3},...Q()}),bi=()=>structuredClone({model:"claude-3-7-sonnet",maxTokens:4e4,thinkingTokenBudgetLevels:{minimal:1024,low:5e3,medium:1e4,high:2e4,highest:32e3},...Q()}),$o=class{constructor(e,t){this.config=e;this.isVertex=t}tokensUsed;currentPromptConfig;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.filter(T=>T.role==="system").map(T=>({type:"text",text:T.content,...T.cache?{cache_control:{type:"ephemeral"}}:{}})),l=e.chatPrompt.filter(T=>T.role!=="system"),c=e.functions?.map(T=>({name:T.name,description:T.description,input_schema:T.parameters?nr(T.parameters):void 0})),u=(this.config.tools??[]).map(T=>T&&typeof T=="object"&&"type"in T?T:{name:T.name,description:T.description,input_schema:T.input_schema?nr(T.input_schema):void 0,...T.cache_control?{cache_control:T.cache_control}:{}}),d=[...c??[],...u];this.isVertex&&d.length>0&&(d=d.filter(T=>!(T&&typeof T=="object"&&"type"in T))),d.length===0&&(d=void 0);let m=e.modelConfig?.maxTokens??this.config.maxTokens,g=e.modelConfig?.stopSequences??this.config.stopSequences,f=e.modelConfig?.temperature,h=e.modelConfig?.topP,A=e.modelConfig?.topK??this.config.topK,x=e.modelConfig?.n??this.config.n;if(x&&x>1)throw new Error("Anthropic does not support sampling (n > 1)");let y;if(this.config.thinking?.budget_tokens&&(y=this.config.thinking),t?.thinkingTokenBudget){let T=this.config.thinkingTokenBudgetLevels;switch(t.thinkingTokenBudget){case"none":y=void 0;break;case"minimal":y={type:"enabled",budget_tokens:T?.minimal??1024};break;case"low":y={type:"enabled",budget_tokens:T?.low??5e3};break;case"medium":y={type:"enabled",budget_tokens:T?.medium??1e4};break;case"high":y={type:"enabled",budget_tokens:T?.high??2e4};break;case"highest":y={type:"enabled",budget_tokens:T?.highest??32e3};break}}if(!y&&t?.thinkingTokenBudget===void 0){let T=this.config.thinkingTokenBudgetLevels}let b=El(l,!!y);b.some(T=>T.role==="assistant"&&Array.isArray(T.content)&&T.content.length>0&&T.content[0]?.type==="tool_use")&&(y=void 0);let I={...this.isVertex?{anthropic_version:"vertex-2023-10-16"}:{model:n},...m?{max_tokens:m}:{},...g&&g.length>0?{stop_sequences:g}:{},...f!==void 0&&!y?{temperature:f}:{},...h!==void 0&&(!y||h>=.95)?{top_p:h}:{},...A&&!y?{top_k:A}:{},...s,...d?{tools:d}:{},...r?{stream:!0}:{},...a?{system:a}:{},...y?{thinking:y}:{},messages:b};return[o,I]};createChatResp=e=>{if(e.type==="error")throw new ae(e.error.message,void 0,void 0);let t=yi(e.stop_reason),n=this.currentPromptConfig?.thinkingTokenBudget!=="none"&&this.currentPromptConfig?.showThoughts!==!1,r="",o="",s=!1,a,l=[],c=[];for(let d of e.content)switch(d.type){case"text":if(r+=d.text??"",Array.isArray(d.citations))for(let m of d.citations)m?.url&&c.push({url:String(m.url),title:typeof m.title=="string"?m.title:void 0,snippet:typeof m.cited_text=="string"?m.cited_text:void 0});break;case"thinking":case"redacted_thinking":n&&(o+=d.thinking??d.data??""),d.type==="redacted_thinking"&&(s=!0),typeof d.signature=="string"&&(a=d.signature);break;case"tool_use":l.push({id:d.id,type:"function",function:{name:d.name,params:d.input}});break}let p={index:0,id:e.id,finishReason:t};r&&(p.content=r),o&&(p.thought=o,p.thoughtBlock={data:o,encrypted:s,...a?{signature:a}:{}}),l.length>0&&(p.functionCalls=l),c.length>0&&(p.citations=c);let u=[p];return this.tokensUsed={promptTokens:e.usage.input_tokens,completionTokens:e.usage.output_tokens,totalTokens:e.usage.input_tokens+e.usage.output_tokens,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 ae(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)},{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,thoughtBlock:{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,thoughtBlock:{data:o.thinking,encrypted:!1}}]}:{results:[{index:r,content:""}]};if(o.type==="signature_delta")return{results:[{index:r,content:""}]};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:0,completionTokens:s.output_tokens,totalTokens:s.output_tokens},{results:[{index:r,content:"",finishReason:yi(o.stop_reason)}]}}return{results:[{index:r,content:""}]}}},Rt=class i extends de{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,c;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}-aiplatform.googleapis.com/v1/projects/${t}/locations/${n}/publishers/anthropic/`,c=async()=>({Authorization:`Bearer ${await e()}`})}else{if(!e)throw new Error("Anthropic API key not set");l="https://api.anthropic.com/v1",c=async()=>({"anthropic-version":"2023-06-01","anthropic-beta":"prompt-caching-2024-07-31","x-api-key":typeof e=="function"?await e():e})}let p={...Go(),...r},u=new $o(p,a),d=g=>{let f=Me({model:g,modelInfo:dn,models:s});return{functions:!0,streaming:!0,hasThinkingBudget:f?.supported?.thinkingBudget??!1,hasShowThoughts:f?.supported?.showThoughts??!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"]},thinking:f?.supported?.thinkingBudget??!1,multiTurn:!0}},m=s?.map(g=>{let f=g,h=f?.config;if(!h)return g;let A={};h.maxTokens!==void 0&&(A.maxTokens=h.maxTokens),h.temperature!==void 0&&(A.temperature=h.temperature),h.topP!==void 0&&(A.topP=h.topP),h.topK!==void 0&&(A.topK=h.topK),h.presencePenalty!==void 0&&(A.presencePenalty=h.presencePenalty),h.frequencyPenalty!==void 0&&(A.frequencyPenalty=h.frequencyPenalty),h.stopSequences!==void 0&&(A.stopSequences=h.stopSequences),h.endSequences!==void 0&&(A.endSequences=h.endSequences),h.stream!==void 0&&(A.stream=h.stream),h.n!==void 0&&(A.n=h.n);let x={...f};Object.keys(A).length>0&&(x.modelConfig={...f.modelConfig??{},...A});let y=h.thinking?.thinkingTokenBudget;if(typeof y=="number"){let b=p.thinkingTokenBudgetLevels,S=[["minimal",b?.minimal??200],["low",b?.low??800],["medium",b?.medium??5e3],["high",b?.high??1e4],["highest",b?.highest??24500]],I="minimal",T=Number.POSITIVE_INFINITY;for(let[C,N]of S){let U=Math.abs(y-N);U<T&&(T=U,I=C)}x.thinkingTokenBudget=I}return h.thinking?.includeThoughts!==void 0&&(x.showThoughts=!!h.thinking.includeThoughts),x});super(u,{name:"Anthropic",apiURL:l,headers:c,modelInfo:dn,defaults:{model:p.model},options:o,supportFor:d,models:m??s})}};function El(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}:{role:"user",content:r.content.map(s=>{switch(s.type){case"text":return{type:"text",text:s.text,...s.cache?{cache:{type:"ephemeral"}}:{}};case"image":return{type:"image",source:{type:"base64",media_type:s.mimeType,data:s.image},...s.cache?{cache:{type:"ephemeral"}}:{}};default:throw new Error("Invalid content type")}})};case"assistant":{let o="",s=[],a=r.thoughtBlock;return a&&typeof a.data=="string"&&a.data.length>0&&(a.encrypted?s.push(a.signature?{type:"redacted_thinking",data:a.data,signature:a.signature}:{type:"redacted_thinking",data:a.data}):s.push(a.signature?{type:"thinking",thinking:a.data,signature:a.signature}:{type:"thinking",thinking:a.data})),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 c={};return typeof l.function.params=="string"?c=JSON.parse(l.function.params):typeof l.function.params=="object"&&(c=l.function.params),{type:"tool_use",id:l.id,name:l.function.name,input:c,...r.cache?{cache:{type:"ephemeral"}}:{}}}),Array.isArray(o)&&s.length>0&&(o=[...s,...o])),{role:"assistant",content:o}}default:throw new Error("Invalid role")}}),n=Pl(t);return Fl(n)}function Pl(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 Fl(i){return i.map(e=>e.role==="assistant"&&typeof e.content=="string"?{...e,content:e.content.replace(/\s+$/,"")}:e)}function yi(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 mn=(I=>(I.GPT4="gpt-4",I.GPT41="gpt-4.1",I.GPT41Mini="gpt-4.1-mini",I.GPT41Nano="gpt-4.1-nano",I.GPT4O="gpt-4o",I.GPT4OMini="gpt-4o-mini",I.GPT4ChatGPT4O="chatgpt-4o-latest",I.GPT4Turbo="gpt-4-turbo",I.GPT35Turbo="gpt-3.5-turbo",I.GPT35TurboInstruct="gpt-3.5-turbo-instruct",I.GPT35TextDavinci002="text-davinci-002",I.GPT3TextBabbage002="text-babbage-002",I.GPT3TextAda001="text-ada-001",I.GPT5="gpt-5",I.GPT5Nano="gpt-5-nano",I.GPT5Mini="gpt-5-mini",I.GPT5Chat="gpt-5-chat",I.O1="o1",I.O1Mini="o1-mini",I.O3="o3",I.O3Mini="o3-mini",I.O4Mini="o4-mini",I))(mn||{}),wt=(n=>(n.TextEmbeddingAda002="text-embedding-ada-002",n.TextEmbedding3Small="text-embedding-3-small",n.TextEmbedding3Large="text-embedding-3-large",n))(wt||{});var St=(C=>(C.GPT4="gpt-4",C.GPT41="gpt-4.1",C.GPT41Mini="gpt-4.1-mini",C.GPT41Nano="gpt-4.1-nano",C.GPT4O="gpt-4o",C.GPT4OMini="gpt-4o-mini",C.GPT4ChatGPT4O="chatgpt-4o-latest",C.GPT4Turbo="gpt-4-turbo",C.GPT35Turbo="gpt-3.5-turbo",C.GPT35TurboInstruct="gpt-3.5-turbo-instruct",C.GPT35TextDavinci002="text-davinci-002",C.GPT3TextBabbage002="text-babbage-002",C.GPT3TextAda001="text-ada-001",C.GPT5="gpt-5",C.GPT5Nano="gpt-5-nano",C.GPT5Mini="gpt-5-mini",C.GPT5Chat="gpt-5-chat",C.O1Pro="o1-pro",C.O1="o1",C.O1Mini="o1-mini",C.O3Pro="o3-pro",C.O3="o3",C.O3Mini="o3-mini",C.O4Mini="o4-mini",C))(St||{});var vt=[{name:"gpt-4",currency:"usd",promptTokenCostPer1M:30,completionTokenCostPer1M:60},{name:"gpt-4.1",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:8},{name:"gpt-4.1-mini",currency:"usd",promptTokenCostPer1M:.4,completionTokenCostPer1M:1.6},{name:"gpt-4.1-nano",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.4},{name:"gpt-4o",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:15},{name:"gpt-4o-mini",currency:"usd",promptTokenCostPer1M:.15,completionTokenCostPer1M:.6},{name:"chatgpt-4o-latest",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:15},{name:"gpt-4-turbo",currency:"usd",promptTokenCostPer1M:10,completionTokenCostPer1M:30},{name:"gpt-3.5-turbo",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5},{name:"gpt-5-nano",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5,notSupported:{temperature:!0,topP:!0}},{name:"gpt-5-mini",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:6,notSupported:{temperature:!0,topP:!0}},{name:"gpt-5",currency:"usd",promptTokenCostPer1M:10,completionTokenCostPer1M:30,notSupported:{temperature:!0,topP:!0}},{name:"gpt-5-chat",currency:"usd",promptTokenCostPer1M:12,completionTokenCostPer1M:36,notSupported:{temperature:!0,topP:!0}},{name:"gpt-5",currency:"usd",promptTokenCostPer1M:20,completionTokenCostPer1M:60,notSupported:{temperature:!0,topP:!0}},{name:"o1",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:60},{name:"o1-mini",currency:"usd",promptTokenCostPer1M:1.1,completionTokenCostPer1M:14.4},{name:"o3",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:60},{name:"o4-mini",currency:"usd",promptTokenCostPer1M:1.1,completionTokenCostPer1M:4.4},{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}],rr=[{name:"gpt-4",currency:"usd",promptTokenCostPer1M:30,completionTokenCostPer1M:60},{name:"gpt-4.1",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:8},{name:"gpt-4.1-mini",currency:"usd",promptTokenCostPer1M:.4,completionTokenCostPer1M:1.6},{name:"gpt-4.1-nano",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.4},{name:"gpt-4o",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:15},{name:"gpt-4o-mini",currency:"usd",promptTokenCostPer1M:.15,completionTokenCostPer1M:.6},{name:"chatgpt-4o-latest",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:15},{name:"gpt-4-turbo",currency:"usd",promptTokenCostPer1M:10,completionTokenCostPer1M:30},{name:"gpt-3.5-turbo",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5},{name:"gpt-5-nano",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gpt-5-mini",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:6,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gpt-5",currency:"usd",promptTokenCostPer1M:10,completionTokenCostPer1M:30,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gpt-5-chat",currency:"usd",promptTokenCostPer1M:12,completionTokenCostPer1M:36,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gpt-5",currency:"usd",promptTokenCostPer1M:20,completionTokenCostPer1M:60,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0}},{name:"o1-pro",currency:"usd",promptTokenCostPer1M:150,completionTokenCostPer1M:600,supported:{thinkingBudget:!0,showThoughts:!0},isExpensive:!0},{name:"o1",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:60,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"o3-pro",currency:"usd",promptTokenCostPer1M:20,completionTokenCostPer1M:80,supported:{thinkingBudget:!0,showThoughts:!0},isExpensive:!0},{name:"o3",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:60,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"o3-mini",currency:"usd",promptTokenCostPer1M:1.1,completionTokenCostPer1M:4.4,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"o4-mini",currency:"usd",promptTokenCostPer1M:1.1,completionTokenCostPer1M:4.4,supported:{thinkingBudget:!0,showThoughts:!0}}];var _l=i=>{let e=["o1","o1-mini","o3","o3-mini","o4-mini","o1-pro","o3-pro"];return e.includes(i)||e.includes(i)},mt=()=>structuredClone({model:"gpt-5-mini",embedModel:"text-embedding-3-small",...Q()}),or=()=>structuredClone({...mt(),model:"gpt-5"}),sr=()=>structuredClone({model:"gpt-5-mini",embedModel:"text-embedding-3-small",...me()}),ir=()=>({...mt(),model:"gpt-5-nano"}),Lo=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=Nl(e),l=e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,c=e.modelConfig?.stream??this.config.stream,p=this.config.store,u=_l(n),d={model:n,messages:a,...this.config?.responseFormat?{response_format:{type:this.config.responseFormat}}:{},...o?{tools:o}:{},...s?{tool_choice:s}:{},...u?{}:{...(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}:{},...c&&this.streamingUsage?{stream:!0,stream_options:{include_usage:!0}}:{},...p?{store:p}:{},...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 ae(a.message.refusal,e.model,e.id);let l=Ii(a.finish_reason),c=a.message.tool_calls?.map(({id:p,function:{arguments:u,name:d}})=>({id:p,type:"function",function:{name:d,params:u}}));return{index:a.index,id:`${a.index}`,content:a.message.content??void 0,thought:a.message.reasoning_content,citations:a.message.annotations?.filter(p=>p?.type==="url_citation"&&p.url_citation).map(p=>({url:p.url_citation?.url,title:p.url_citation?.title,description:p.url_citation?.description})),functionCalls:c,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:c,role:p,refusal:u,tool_calls:d,reasoning_content:m,annotations:g},finish_reason:f})=>{if(u)throw new ae(u,void 0,n);let h=Ii(f),A=d?.map(({id:x,index:y,function:{name:b,arguments:S}})=>{typeof x=="string"&&typeof y=="number"&&!s.indexIdMap[y]&&(s.indexIdMap[y]=x);let I=s.indexIdMap[y];return I?{id:I,type:"function",function:{name:b,params:S}}:null}).filter(x=>x!==null);return{index:l,content:c??void 0,role:p,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:A,finishReason:h,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)}}},Ii=i=>{switch(i){case"stop":return"stop";case"length":return"length";case"content_filter":return"error";case"tool_calls":return"function_call"}};function Nl(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 ue=class extends de{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 c=new Lo(t,n?.streamingUsage??!0,a);super(c,{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})}},Ot=class extends ue{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=[...vt,...s??[]];let a=c=>{let p=Me({model:c,modelInfo:s,models:o});return{functions:!0,streaming:!0,hasThinkingBudget:p?.supported?.thinkingBudget??!1,hasShowThoughts:p?.supported?.showThoughts??!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:p?.supported?.thinkingBudget??!1,multiTurn:!0}},l=o?.map(c=>{let p=c,u=p?.config;if(!u)return c;let d={};u.maxTokens!==void 0&&(d.maxTokens=u.maxTokens),u.temperature!==void 0&&(d.temperature=u.temperature),u.topP!==void 0&&(d.topP=u.topP),u.presencePenalty!==void 0&&(d.presencePenalty=u.presencePenalty),u.frequencyPenalty!==void 0&&(d.frequencyPenalty=u.frequencyPenalty);let m=u.stopSequences??u.stop;m!==void 0&&(d.stopSequences=m),u.n!==void 0&&(d.n=u.n),u.stream!==void 0&&(d.stream=u.stream);let g={...p};Object.keys(d).length>0&&(g.modelConfig={...p.modelConfig??{},...d});let f=u?.thinking?.thinkingTokenBudget;if(typeof f=="number"){let h=[["minimal",200],["low",800],["medium",5e3],["high",1e4],["highest",24500]],A="minimal",x=Number.POSITIVE_INFINITY;for(let[y,b]of h){let S=Math.abs(f-b);S<x&&(x=S,A=y)}g.thinkingTokenBudget=A}return u?.thinking?.includeThoughts!==void 0&&(g.showThoughts=!!u.thinking.includeThoughts),g});super({apiKey:e,apiURL:t,config:{...mt(),...n},options:r,modelInfo:s,models:l??o,supportFor:a}),super.setName("OpenAI")}};var Uo=mt,Ti=sr,Ci=ir,Ri=or,kt=class extends ue{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 c={...Uo(),...o};l=[...vt,...l??[]];let p=d=>{let m=Me({model:d,modelInfo:l,models:a});return{functions:!0,streaming:!0,hasThinkingBudget:m?.supported?.thinkingBudget??!1,hasShowThoughts:m?.supported?.showThoughts??!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:m?.supported?.thinkingBudget??!1,multiTurn:!0}};super({apiKey:e,config:c,options:s,models:a,modelInfo:l,supportFor:p});let u=t.includes("://")?t:`https://${t}.openai.azure.com/`;super.setName("Azure OpenAI"),super.setAPIURL(new URL(`/openai/deployments/${n}?api-version=${r}`,u).href),super.setHeaders(async()=>({"api-key":e}))}};var ar=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.");Dl(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(){return this.currentService.getModelList()}getNextService(){let e=this.services[++this.currentServiceIndex];return e===void 0?!1:(this.currentService=e,!0)}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){return this.currentService.getFeatures(e)}getMetrics(){return this.currentService.getMetrics()}canRetryService(){let e=this.serviceFailures.get(this.currentService.getId());if(!e)return!0;let{retries:t,lastFailureTime:n}=e,r=Date.now()-n,o=Math.min(this.initialBackoffMs*2**t,this.maxBackoffMs);return r>=o}handleFailure(e){let n=(this.serviceFailures.get(this.currentService.getId())?.retries??0)+1;if(this.serviceFailures.set(this.currentService.getId(),{retries:n,lastFailureTime:Date.now()}),this.debug&&console.warn(`AxBalancer: Service ${this.currentService.getName()} failed (retry ${n}/${this.maxRetries})`,e),n>=this.maxRetries){let r=this.getNextService();return this.debug&&console.warn(`AxBalancer: Switching to service ${this.currentService.getName()}`,e),r}return!0}handleSuccess(){this.serviceFailures.delete(this.currentService.getId())}async chat(e,t){for(this.reset();;){if(!this.canRetryService()){if(!this.getNextService())throw new Error("All services exhausted");continue}try{let n=await this.currentService.chat(e,t);return this.handleSuccess(),n}catch(n){if(!(n instanceof Ie))throw n;switch(n.constructor){case rt:throw n;case ut:break;case je:break;case Xe:break;case Ge:break;case dt:break;default:throw n}if(!this.handleFailure(n))throw n}}}async embed(e,t){for(this.reset();;){if(!this.canRetryService()){if(!this.getNextService())throw new Error("All services exhausted");continue}try{let n=await this.currentService.embed(e,t);return this.handleSuccess(),n}catch(n){if(!(n instanceof Ie)||!this.handleFailure(n))throw n}}}setOptions(e){this.currentService.setOptions(e)}getOptions(){return this.currentService.getOptions()}getLogger(){return this.currentService.getLogger()}};function Dl(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 gt(i){let e=!1,t=!1,n=!1,r=!1,o=!1,s=!1,a=!1,l=new Set,c=0;if(i.chatPrompt&&Array.isArray(i.chatPrompt))for(let p of i.chatPrompt){if(p.role==="user"&&Array.isArray(p.content))for(let u of p.content)switch(l.add(u.type),u.type){case"image":e=!0,u.cache&&(a=!0),c+=85;break;case"audio":t=!0,u.cache&&(a=!0),c+=u.duration||60;break;case"file":n=!0,u.cache&&(a=!0),c+=Math.ceil((u.extractedText?.length||1e3)/4);break;case"url":r=!0,u.cache&&(a=!0),c+=Math.ceil((u.cachedContent?.length||2e3)/4);break;case"text":u.cache&&(a=!0),c+=Math.ceil(u.text.length/4);break}else"content"in p&&typeof p.content=="string"&&(c+=Math.ceil(p.content.length/4));"cache"in p&&p.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:c}}function Bo(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 lr(i,e){return i.map(t=>{let n=t.getFeatures(),r=Bo(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 cr(i,e,t={}){if(e.length===0)throw new Error("No providers available");let n=gt(i),r=lr(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 wi(i,e){let t=gt(i),n=lr(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 Si(i,e){return i.filter(t=>t.getFeatures().media[e].supported)}function vi(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 gn=(r=>(r.CommandRPlus="command-r-plus",r.CommandR="command-r",r.Command="command",r.CommandLight="command-light",r))(gn||{}),hn=(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))(hn||{});var pr=[{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}];var zo=()=>structuredClone({model:"command-r-plus",embedModel:"embed-english-v3.0",...Q()}),ki=()=>structuredClone({model:"command-r",embedModel:"embed-english-v3.0",...me()}),qo=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=$l(r),a=e.functions?.map(u=>{let d={};if(u.parameters?.properties)for(let[m,g]of Object.entries(u.parameters.properties))d[m]={description:g.description,type:g.type,required:u.parameters.required?.includes(m)??!1};return{name:u.name,description:u.description,parameter_definitions:d}}),l=e.chatPrompt.filter(u=>u.role==="function").map(u=>{let d=a?.find(m=>m.name===u.functionId);if(!d)throw new Error("Function not found");return{call:{name:d.name,parameters:d.parameter_definitions},outputs:[{result:u.result??""}]}}),c={name:"/chat"},p={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[c,p]}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}}},Mt=class extends de{constructor({apiKey:e,config:t,options:n,models:r}){if(!e||e==="")throw new Error("Cohere API key not set");let o={...zo(),...t},s=new qo(o),a=r?.map(l=>{let c=l,p=c?.config;if(!p)return l;let u={};p.maxTokens!==void 0&&(u.maxTokens=p.maxTokens),p.temperature!==void 0&&(u.temperature=p.temperature),p.topP!==void 0&&(u.topP=p.topP),p.topK!==void 0&&(u.topK=p.topK),p.presencePenalty!==void 0&&(u.presencePenalty=p.presencePenalty),p.frequencyPenalty!==void 0&&(u.frequencyPenalty=p.frequencyPenalty),p.stopSequences!==void 0&&(u.stopSequences=p.stopSequences),p.endSequences!==void 0&&(u.endSequences=p.endSequences),p.stream!==void 0&&(u.stream=p.stream),p.n!==void 0&&(u.n=p.n);let d={...c};return Object.keys(u).length>0&&(d.modelConfig={...c.modelConfig??{},...u}),d});super(s,{name:"Cohere",apiURL:"https://api.cohere.ai/v1",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:pr,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 $l(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=Oi(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=Oi(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 Oi(i){return i?.map(e=>{let t=typeof e.function.params=="string"?JSON.parse(e.function.params):e.function.params;return{name:e.function.name,parameters:t}})}var fn=(n=>(n.DeepSeekChat="deepseek-chat",n.DeepSeekCoder="deepseek-coder",n.DeepSeekReasoner="deepseek-reasoner",n))(fn||{});var ur=[{name:"deepseek-chat",currency:"USD",promptTokenCostPer1M:.27,completionTokenCostPer1M:1.1},{name:"deepseek-reasoner",currency:"USD",promptTokenCostPer1M:.55,completionTokenCostPer1M:2.19}];var jo=()=>structuredClone({model:"deepseek-chat",...Q()}),Mi=()=>structuredClone({model:"deepseek-coder",...me()}),Et=class extends ue{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("DeepSeek API key not set");let s={...jo(),...t};o=[...ur,...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 An=(m=>(m.Gemini25Pro="gemini-2.5-pro",m.Gemini25Flash="gemini-2.5-flash",m.Gemini25FlashLite="gemini-2.5-flash-lite",m.Gemini20Flash="gemini-2.0-flash",m.Gemini20FlashLite="gemini-2.0-flash-lite",m.Gemini1Pro="gemini-1.0-pro",m.Gemini15Flash="gemini-1.5-flash",m.Gemini15Flash002="gemini-1.5-flash-002",m.Gemini15Flash8B="gemini-1.5-flash-8b",m.Gemini15Pro="gemini-1.5-pro",m.GeminiFlashLatest="gemini-flash-latest",m.GeminiFlashLiteLatest="gemini-flash-lite-latest",m.GeminiProLatest="gemini-pro-latest",m))(An||{}),dr=(r=>(r.GeminiEmbedding="gemini-embedding-exp",r.TextEmbeddingLarge="text-embedding-large-exp-03-07",r.TextEmbedding004="text-embedding-004",r.TextEmbedding005="text-embedding-005",r))(dr||{}),mr=(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))(mr||{}),gr=(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))(gr||{}),Ho=(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))(Ho||{});var hr=[{name:"gemini-2.5-pro",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:2.5,completionTokenCostPer1M:15,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gemini-2.5-flash",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:15,completionTokenCostPer1M:3.5,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gemini-2.5-flash-lite",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.1,completionTokenCostPer1M:.4,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gemini-2.0-flash",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.01,completionTokenCostPer1M:.4},{name:"gemini-2.0-flash-lite",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:0,completionTokenCostPer1M:0},{name:"gemini-1.5-flash",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.075,completionTokenCostPer1M:.3},{name:"gemini-1.5-flash-8b",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.0375,completionTokenCostPer1M:.15},{name:"gemini-1.5-pro",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:1.25,completionTokenCostPer1M:5},{name:"gemini-1.0-pro",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5},{name:"gemini-flash-latest",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.075,completionTokenCostPer1M:.3,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gemini-flash-lite-latest",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:0,completionTokenCostPer1M:0,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gemini-pro-latest",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:1.25,completionTokenCostPer1M:5,supported:{thinkingBudget:!0,showThoughts:!0}}];var Ko=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,e.properties&&typeof e.properties=="object"&&(e.properties=Object.fromEntries(Object.entries(e.properties).map(([t,n])=>[t,Ko(n)]))),e.items&&(e.items=Ko(e.items)),e},Ei=[{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"}],Vo=()=>structuredClone({model:"gemini-2.5-flash",embedModel:"text-embedding-005",safetySettings:Ei,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},...Q()}),Pi=()=>structuredClone({model:"gemini-2.0-flash",embedModel:"text-embedding-005",safetySettings:Ei,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},...me()}),Wo=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")}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,endSequences:e.endSequences,stream:e.stream,n:e.n}}createChatReq=async(e,t)=>{let n=e.model,r=e.modelConfig?.stream??this.config.stream;if(!e.chatPrompt||e.chatPrompt.length===0)throw new Error("Chat prompt is empty");let o;if(this.endpointId?o={name:r?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:o={name:r?`/models/${n}:streamGenerateContent?alt=sse`:`/models/${n}:generateContent`},!this.isVertex){let h=r?"&":"?",A=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;o.name+=`${h}key=${A}`}let s=e.chatPrompt.filter(h=>h.role==="system").map(h=>h.content),a=s.length>0?{role:"user",parts:[{text:s.join(" ")}]}:void 0,l=e.chatPrompt.filter(h=>h.role!=="system").map((h,A)=>{switch(h.role){case"user":return{role:"user",parts:Array.isArray(h.content)?h.content.map((y,b)=>{switch(y.type){case"text":return{text:y.text};case"image":return{inlineData:{mimeType:y.mimeType,data:y.image}};case"audio":return{inlineData:{mimeType:`audio/${y.format??"mp3"}`,data:y.data}};case"file":return"fileUri"in y?{fileData:{mimeType:y.mimeType,fileUri:y.fileUri}}:{inlineData:{mimeType:y.mimeType,data:y.data}};default:throw new Error(`Chat prompt content type not supported (index: ${b})`)}}):[{text:h.content}]};case"assistant":{let x=[];if(h.functionCalls){if(x=h.functionCalls.map(y=>{let b=typeof y.function.params=="string"?JSON.parse(y.function.params):y.function.params;return{functionCall:{name:y.function.name,args:b}}}),!x)throw new Error("Function call is empty");return{role:"model",parts:x}}if(!h.content)throw new Error("Assistant content is empty");return x=[{text:h.content}],{role:"model",parts:x}}case"function":{if(!("functionId"in h))throw new Error(`Chat prompt functionId is empty (index: ${A})`);return{role:"user",parts:[{functionResponse:{name:h.functionId,response:{result:h.result}}}]}}default:throw new Error(`Invalid role: ${JSON.stringify(h)} (index: ${A})`)}}),c=[];if(e.functions&&e.functions.length>0){let h=e.functions.map(A=>({...A,parameters:A.parameters?Ko(A.parameters):void 0}));c.push({function_declarations:h})}if(this.options?.codeExecution&&c.push({code_execution:{}}),this.options?.googleSearchRetrieval&&c.push({google_search_retrieval:{dynamic_retrieval_config:this.options.googleSearchRetrieval}}),this.options?.googleSearch&&c.push({google_search:{}}),this.options?.googleMaps){let h=this.options.googleMaps,A=h?.enableWidget!==void 0?{enableWidget:h.enableWidget}:{};c.push({google_maps:A})}this.options?.urlContext&&c.push({url_context:{}}),c.length===0&&(c=void 0);let p,u=Array.isArray(c)?c.some(h=>h&&Array.isArray(h.function_declarations)&&h.function_declarations.length>0):!1;if(e.functionCall)if(e.functionCall==="none")p={function_calling_config:{mode:"NONE"}};else if(e.functionCall==="auto")p={function_calling_config:{mode:"AUTO"}};else if(e.functionCall==="required")p={function_calling_config:{mode:"ANY"}};else{let h=e.functionCall.function?.name?{allowedFunctionNames:[e.functionCall.function.name]}:{};p={function_calling_config:{mode:"ANY"},...h}}else u&&(p={function_calling_config:{mode:"AUTO"}});this.options?.retrievalConfig&&(p={...p??{},retrievalConfig:{...this.options.retrievalConfig.latLng?{latLng:this.options.retrievalConfig.latLng}:{}}});let d={};if(this.config.thinking?.includeThoughts&&(d.includeThoughts=!0),this.config.thinking?.thinkingTokenBudget&&(d.thinkingBudget=this.config.thinking.thinkingTokenBudget),t?.thinkingTokenBudget){let h=this.config.thinkingTokenBudgetLevels;switch(t.thinkingTokenBudget){case"none":d.thinkingBudget=0,d.includeThoughts=!1;break;case"minimal":d.thinkingBudget=h?.minimal??200;break;case"low":d.thinkingBudget=h?.low??800;break;case"medium":d.thinkingBudget=h?.medium??5e3;break;case"high":d.thinkingBudget=h?.high??1e4;break;case"highest":d.thinkingBudget=h?.highest??24500;break}}t?.showThoughts!==void 0&&t?.thinkingTokenBudget!=="none"&&(d.includeThoughts=t.showThoughts);let m={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.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(d).length>0?{thinkingConfig:d}:{}},g=this.config.safetySettings;return[o,{contents:l,tools:c,toolConfig:p,systemInstruction:a,generationConfig:m,safetySettings:g}]};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 ae("Content was blocked due to safety settings",void 0,void 0);case"RECITATION":throw new ae("Content was blocked due to recitation policy",void 0,void 0);case"MALFORMED_FUNCTION_CALL":throw new ae("Function call was malformed and blocked",void 0,void 0);case"UNEXPECTED_TOOL_CALL":throw new ae("Unexpected tool call",void 0,void 0);case"FINISH_REASON_UNSPECIFIED":throw new ae("Finish reason unspecified",void 0,void 0);case"BLOCKLIST":throw new ae("Content was blocked due to blocklist",void 0,void 0);case"PROHIBITED_CONTENT":throw new ae("Content was blocked due to prohibited content",void 0,void 0);case"SPII":throw new ae("Content was blocked due to SPII",void 0,void 0);case"OTHER":throw new ae("Other finish reason",void 0,void 0)}if(!o.content||!o.content.parts)return s;for(let c of o.content.parts){if("text"in c){"thought"in c&&c.thought?s.thought=c.text:s.content=c.text;continue}"functionCall"in c&&(s.functionCalls=[{id:fe(),type:"function",function:{name:c.functionCall.name,params:c.functionCall.args}}])}let a=o.citationMetadata?.citations;if(Array.isArray(a)&&a.length){let c=p=>p?`${p.year}-${String(p.month).padStart(2,"0")}-${String(p.day).padStart(2,"0")}`:void 0;s.citations=a.filter(p=>typeof p?.uri=="string").map(p=>({url:p.uri,title:p.title,license:p.license,publicationDate:c(p.publicationDate)}))}let l=o.groundingMetadata;if(l){if(Array.isArray(l.groundingChunks)){let c=l.groundingChunks.map(p=>p?.maps).filter(p=>p&&typeof p.uri=="string").map(p=>({url:p.uri,title:p.title}));c.length&&(s.citations=[...s.citations??[],...c])}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});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}}},Pt=class i extends de{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 c=t!==void 0&&n!==void 0,p,u;if(c){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 h;r?h="endpoints":h="publishers/google",p=`https://${n==="global"?"aiplatform":`${n}-aiplatform`}.googleapis.com/v1/projects/${t}/locations/${n}/${h}`,u=async()=>({Authorization:`Bearer ${typeof e=="function"?await e():e}`})}else{if(!e)throw new Error("GoogleGemini AI API key not set");p="https://generativelanguage.googleapis.com/v1beta",u=async()=>({})}let d={...Vo(),...o},m=new Wo(d,c,r,e,s);l=[...hr,...l??[]];let g=h=>{let A=Me({model:h,modelInfo:l,models:a});return{functions:!0,streaming:!0,hasThinkingBudget:A?.supported?.thinkingBudget??!1,hasShowThoughts:A?.supported?.showThoughts??!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:!1,types:[]},thinking:A?.supported?.thinkingBudget??!1,multiTurn:!0}},f=a?.map(h=>{let A=h,x=A?.config;if(!x)return h;let y={};x.maxTokens!==void 0&&(y.maxTokens=x.maxTokens),x.temperature!==void 0&&(y.temperature=x.temperature),x.topP!==void 0&&(y.topP=x.topP),x.topK!==void 0&&(y.topK=x.topK),x.presencePenalty!==void 0&&(y.presencePenalty=x.presencePenalty),x.frequencyPenalty!==void 0&&(y.frequencyPenalty=x.frequencyPenalty),x.stopSequences!==void 0&&(y.stopSequences=x.stopSequences),x.endSequences!==void 0&&(y.endSequences=x.endSequences),x.stream!==void 0&&(y.stream=x.stream),x.n!==void 0&&(y.n=x.n);let b={...A};Object.keys(y).length>0&&(b.modelConfig={...A.modelConfig??{},...y});let S=x.thinking?.thinkingTokenBudget;if(typeof S=="number"){let I=d.thinkingTokenBudgetLevels,T=[["minimal",I?.minimal??200],["low",I?.low??800],["medium",I?.medium??5e3],["high",I?.high??1e4],["highest",I?.highest??24500]],C="minimal",N=Number.POSITIVE_INFINITY;for(let[U,P]of T){let w=Math.abs(S-P);w<N&&(N=w,C=U)}b.thinkingTokenBudget=C}return x.thinking?.includeThoughts!==void 0&&(b.showThoughts=!!x.thinking.includeThoughts),b});super(m,{name:"GoogleGeminiAI",apiURL:p,headers:u,modelInfo:l,defaults:{model:d.model,embedModel:d.embedModel},options:s,supportFor:g,models:f??a})}};var Gl=new Te,Ft=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(Gl.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 xn=(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))(xn||{});var fr=[{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 Ll=()=>structuredClone({model:"llama-3.3-70b-versatile",...Q()}),_t=class extends ue{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Groq API key not set");let s={...Ll(),...t},a={...n,streamingUsage:!1};o=[...fr,...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 Ft(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 Ar=[];var xr=(e=>(e.MetaLlama270BChatHF="meta-llama/Llama-2-70b-chat-hf",e))(xr||{});var Qo=()=>structuredClone({model:"meta-llama/Llama-2-70b-chat-hf",...Q()}),Fi=()=>structuredClone({model:"meta-llama/Llama-2-70b-chat-hf",...me()}),Jo=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:
151
+ `))}function Tt(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 p=o.fileUri;if(!p||p.trim()==="")throw new Error(`User message file content at index ${r} fileUri cannot be empty, received: ${e(p)}`)}if(l){let p=o.data;if(!p||p.trim()==="")throw new Error(`User message file content at index ${r} data cannot be empty, received: ${e(p)}`)}let c="mimeType"in o&&typeof o.mimeType=="string"?o.mimeType:null;if(!c||c.trim()==="")throw new Error(`User message file content at index ${r} must have a mimeType, received: ${e(c)}`);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 n=="string"&&n.trim()!=="",s=Array.isArray(r)&&r.length>0;if(!o&&!s&&Be("Assistant message must include non-empty content or at least one function call",{fieldPath:"content | functionCalls",value:{content:n,functionCalls:r},item:i}),n!==void 0&&typeof n!="string"&&Be("Assistant message content must be a string",{fieldPath:"content",value:n,item:i}),r!==void 0&&!Array.isArray(r)&&Be("Assistant message functionCalls must be an array when provided",{fieldPath:"functionCalls",value:r,item:i}),Array.isArray(r))for(let a=0;a<r.length;a++){let l=r[a];if((!l||typeof l!="object")&&Be("functionCalls entry must be an object",{fieldPath:`functionCalls[${a}]`,value:l,item:i}),(!("id"in l)||typeof l.id!="string"||l.id.trim()==="")&&Be("functionCalls entry must include a non-empty string id",{fieldPath:`functionCalls[${a}].id`,value:l.id,item:i}),(!("type"in l)||l.type!=="function")&&Be("functionCalls entry must have type 'function'",{fieldPath:`functionCalls[${a}].type`,value:l.type,item:i}),!("function"in l)||!l.function)Be("functionCalls entry must include a function object",{fieldPath:`functionCalls[${a}].function`,value:l.function,item:i});else{let c=l.function;(!("name"in c)||typeof c.name!="string"||c.name.trim()==="")&&Be("functionCalls entry must include a non-empty function name",{fieldPath:`functionCalls[${a}].function.name`,value:c?.name,item:i}),c.params!==void 0&&typeof c.params!="string"&&typeof c.params!="object"&&Be("functionCalls entry params must be a string or object when provided",{fieldPath:`functionCalls[${a}].function.params`,value:c.params,item:i})}}if(i.name!==void 0){let a=i.name;(typeof a!="string"||a.trim()==="")&&Be("Assistant message name must be a non-empty string when provided",{fieldPath:"name",value:a,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"&&Be("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 er(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.thoughtBlock&&!r.functionCalls&&!r.finishReason)throw new Error(`Chat response result at index ${n} must have at least one of: content, thought, functionCalls, or finishReason, received: ${e({content:r.content,thought:r.thought,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.thoughtBlock!==void 0){if(typeof r.thoughtBlock!="object"||r.thoughtBlock===null)throw new Error(`Chat response result thoughtBlock at index ${n} must be an object, received: ${e(r.thoughtBlock)}`);let o=r.thoughtBlock;if(typeof o.data!="string")throw new Error(`Chat response result thoughtBlock.data at index ${n} must be a string, received: ${e(o.data)}`);if(typeof o.encrypted!="boolean")throw new Error(`Chat response result thoughtBlock.encrypted at index ${n} must be a boolean, received: ${e(o.encrypted)}`);if(o.signature!==void 0&&typeof o.signature!="string")throw new Error(`Chat response result thoughtBlock.signature at index ${n} must be a string when provided, received: ${e(o.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 Q=()=>structuredClone({temperature:0}),me=()=>structuredClone({temperature:.4,frequencyPenalty:.2}),de=class{constructor(e,{name:t,apiURL:n,headers:r,modelInfo:o,defaults:s,options:a={},supportFor:l,models:c}){this.aiImpl=e;this.name=t,this.apiURL=n||"",this.headers=r,this.supportFor=l,this.tracer=a.tracer??se.tracer,this.meter=a.meter??se.meter,this.modelInfo=o,this.models=c,this.id=fe();let p=this.getModel(s.model)??s.model,u=this.getEmbedModel(s.embedModel)??s.embedModel;if(this.defaults={model:p,embedModel:u},!s.model||typeof s.model!="string"||s.model==="")throw new Error("No model defined");this.setOptions(a),c&&Ml(c)}#e=!1;rt;fetch;tracer;meter;timeout;excludeContentFromTrace;models;abortSignal;logger=se.logger??qs;corsProxy;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 ti(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??se.debug??!1,this.rt=e.rateLimiter,this.fetch=e.fetch,this.timeout=e.timeout,this.tracer=e.tracer??se.tracer,this.meter=e.meter??se.meter,this.excludeContentFromTrace=e.excludeContentFromTrace,this.abortSignal=e.abortSignal,this.logger=e.logger??se.logger??this.logger,this.corsProxy=e.corsProxy}getOptions(){return{debug:this.#e,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}}getLogger(){return this.logger}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){let n=this.metrics.latency[e];n.samples.push(t),n.samples.length>1e3&&n.samples.shift(),n.mean=n.samples.reduce((o,s)=>o+s,0)/n.samples.length,n.p95=this.calculatePercentile(n.samples,95),n.p99=this.calculatePercentile(n.samples,99);let r=this.getMetricsInstruments();if(r){let o=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel;ni(r,e,t,this.name,o),ri(r,e,n.mean,n.p95,n.p99,this.name,o)}}updateErrorMetrics(e,t){let n=this.metrics.errors[e];n.total++,t&&n.count++,n.rate=n.count/n.total;let r=this.getMetricsInstruments();if(r){let o=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel;ii(r,e,this.name,o),t&&oi(r,e,this.name,o),si(r,e,n.rate,this.name,o)}}recordTokenUsage(e){let t=this.getMetricsInstruments();if(t&&e?.tokens){let{promptTokens:n,completionTokens:r,totalTokens:o,thoughtsTokens:s}=e.tokens;n&&pn(t,"input",n,this.name,e.model),r&&pn(t,"output",r,this.name,e.model),o&&pn(t,"total",o,this.name,e.model),s&&pn(t,"thoughts",s,this.name,e.model)}}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){let n=this.getMetricsInstruments();if(!(!n||!e))for(let r of e)r&&typeof r=="object"&&"function"in r&&r.function&&typeof r.function=="object"&&"name"in r.function&&li(n,r.function.name,void 0,this.name,t)}recordTimeoutMetric(e){let t=this.getMetricsInstruments();if(t){let n=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel;di(t,e,this.name,n)}}recordAbortMetric(e){let t=this.getMetricsInstruments();if(t){let n=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel;mi(t,e,this.name,n)}}recordChatMetrics(e,t,n){let r=this.getMetricsInstruments();if(!r)return;let o=this.lastUsedChatModel,s=this.lastUsedModelConfig,a=s?.stream??!1;ai(r,"chat",a,this.name,o);let{hasImages:l,hasAudio:c}=this.detectMultimodalContent(e);hi(r,l,c,this.name,o);let p=this.calculatePromptLength(e);pi(r,p,this.name,o),ci(r,s?.temperature,s?.maxTokens,this.name,o),t?.thinkingTokenBudget&&this.modelUsage?.tokens?.thoughtsTokens&&gi(r,this.modelUsage.tokens.thoughtsTokens,this.name,o);let u=this.calculateRequestSize(e);if(_o(r,"chat",u,this.name,o),n&&!a){let d=n,m=this.calculateResponseSize(d);if(No(r,"chat",m,this.name,o),d.results)for(let h of d.results)h.functionCalls&&this.recordFunctionCallMetrics(h.functionCalls,this.lastUsedChatModel);let g=this.calculateContextWindowUsage(this.lastUsedChatModel,d.modelUsage);g>0&&ui(r,g,this.name,o);let f=this.estimateCost(this.lastUsedChatModel,d.modelUsage);f>0&&Do(r,"chat",f,this.name,o)}}recordEmbedMetrics(e,t){let n=this.getMetricsInstruments();if(!n)return;let r=this.lastUsedEmbedModel,o=this.calculateRequestSize(e);_o(n,"embed",o,this.name,r);let s=this.calculateResponseSize(t);No(n,"embed",s,this.name,r);let a=this.estimateCostByName(r,t.modelUsage);a>0&&Do(n,"embed",a,this.name,r)}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(([,c])=>c!==void 0))};try{return o=await this._chat1(e,l),o}catch(c){throw r=!0,c instanceof Error&&(c.message.includes("timeout")||c.name==="TimeoutError"?this.recordTimeoutMetric("chat"):(c.message.includes("abort")||c.name==="AbortError")&&this.recordAbortMetric("chat")),c}finally{let c=performance.now()-n;this.updateLatencyMetrics("chat",c),this.updateErrorMetrics("chat",r),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 c of e.chatPrompt)Tt(c);let r=this.getModelByKey(e.model),o={...this.aiImpl.getModelConfig(),...r?r.modelConfig:void 0,...e.modelConfig},s=this.modelInfo.find(c=>c.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(c=>c.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:Ct.SpanKind.SERVER,attributes:{[q.LLM_SYSTEM]:this.name,[q.LLM_OPERATION_NAME]:"chat",[q.LLM_REQUEST_MODEL]:n,[q.LLM_REQUEST_MAX_TOKENS]:o.maxTokens??"Not set",[q.LLM_REQUEST_TEMPERATURE]:o.temperature,[q.LLM_REQUEST_TOP_P]:o.topP??"Not set",[q.LLM_REQUEST_TOP_K]:o.topK??"Not set",[q.LLM_REQUEST_FREQUENCY_PENALTY]:o.frequencyPenalty??"Not set",[q.LLM_REQUEST_PRESENCE_PENALTY]:o.presencePenalty??"Not set",[q.LLM_REQUEST_STOP_SEQUENCES]:o.stopSequences?.join(", ")??"Not set",[q.LLM_REQUEST_LLM_IS_STREAMING]:o.stream??"Not set"}},t?.traceContext??Ct.context.active(),async c=>await this._chat2(n,o,e,t,c)):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;n.functions&&n.functions.length>0&&(a=n.functions.map(A=>this.cleanupFunctionSchema(A)));let l={...n,model:e,functions:a,modelConfig:t};this.lastUsedChatModel=e,this.lastUsedModelConfig=t,s&&js(l.chatPrompt,r?.stepIndex??0,r?.logger??this.logger,r?.debugHideSystemPrompt);let c=this.getFeatures(e).functions,p=r?.functionCallMode??"auto",d=p==="prompt"||p==="auto"&&!c?{...l,chatPrompt:l.chatPrompt.map(A=>{if(A.role==="assistant"){let{content:x,name:y,cache:b}=A;return{role:"assistant",content:x,name:y,cache:b}}return A.role==="function"?{role:"user",content:A.result}:A}),functions:[]}:l,m=async()=>{let[A,x]=await this.aiImpl.createChatReq(d,r);return o?.isRecording()&&kl(n,o,this.excludeContentFromTrace),await Ce({name:A.name,url:this.apiURL,localCall:A.localCall,headers:await this.buildHeaders(A.headers),stream:t.stream,timeout:this.timeout,debug:s,fetch:this.fetch,span:o,abortSignal:r?.abortSignal??this.abortSignal,corsProxy:this.corsProxy},x)},g=r?.rateLimiter??this.rt,f=g?await g(m,{modelUsage:this.modelUsage}):await m();if(t.stream){if(!this.aiImpl.createChatStreamResp)throw new Error("createChatStreamResp not implemented");let A=this.aiImpl.createChatStreamResp.bind(this),x=I=>T=>{let C=A(T,I);if(C.sessionId=r?.sessionId,!C.modelUsage){let N=this.aiImpl.getTokenUsage();N&&(C.modelUsage={ai:this.name,model:e,tokens:N})}if(this.modelUsage=C.modelUsage,this.recordTokenUsage(C.modelUsage),o?.isRecording()&&Ai(C,o,this.excludeContentFromTrace),s)for(let N of C.results)Ks(N,N.index,r?.logger??this.logger);return C},y=async I=>{o?.isRecording()&&o.end(),s&&Ws(I,r?.logger??this.logger)};if(typeof window<"u"){let I=f,T={},C=[],N=r?.abortSignal??this.abortSignal;return new ReadableStream({start:U=>{let k=I.getReader(),w=()=>{try{k.cancel().catch(()=>{})}catch{}try{this.recordAbortMetric("chat")}catch{}try{o?.isRecording()&&o.end()}catch{}try{U.error(new DOMException("Aborted","AbortError"))}catch{U.error(new Error("Aborted"))}};if(N){if(N.aborted){w();return}N.addEventListener("abort",w,{once:!0})}async function E(){try{for(;;){let{done:R,value:M}=await k.read();if(R){y&&await y(C),U.close();break}let F=x(T)(M);F&&(C.push(F),U.enqueue(F))}}catch(R){if(U.error(R),o?.isRecording())try{o.end()}catch{}}finally{if(k.releaseLock(),N)try{N.removeEventListener("abort",w)}catch{}}}E()}})}return f.pipeThrough(new Yn(x({}),y))}if(!this.aiImpl.createChatResp)throw new Error("createChatResp not implemented");let h=this.aiImpl.createChatResp(f);if(h.sessionId=r?.sessionId,!h.modelUsage){let A=this.aiImpl.getTokenUsage();A&&(h.modelUsage={ai:this.name,model:e,tokens:A})}return h.modelUsage&&(this.modelUsage=h.modelUsage,this.recordTokenUsage(h.modelUsage)),o?.isRecording()&&(Ai(h,o,this.excludeContentFromTrace),o.end()),s&&Hs(h,r?.logger??this.logger),h}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"):(l.message.includes("abort")||l.name==="AbortError")&&this.recordAbortMetric("embed")),l}finally{let l=performance.now()-n;this.updateLatencyMetrics("embed",l),this.updateErrorMetrics("embed",r),!r&&o&&this.recordEmbedMetrics(e,o)}}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:Ct.SpanKind.SERVER,attributes:{[q.LLM_SYSTEM]:this.name,[q.LLM_OPERATION_NAME]:"embeddings",[q.LLM_REQUEST_MODEL]:n}},t?.traceContext??Ct.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={...t,embedModel:e};this.lastUsedEmbedModel=e,s&&Xs(a.texts??[],e,n?.logger??this.logger);let l=async()=>{let[d,m]=await o(a);return await Ce({name:d.name,url:this.apiURL,localCall:d.localCall,headers:await this.buildHeaders(d.headers),debug:s,fetch:this.fetch,timeout:this.timeout,span:r,abortSignal:n?.abortSignal??this.abortSignal,corsProxy:this.corsProxy},m)},c=n?.rateLimiter??this.rt,p=c?await c(l,{modelUsage:this.embedModelUsage}):await l(),u=this.aiImpl.createEmbedResp?.(p);if(u.sessionId=n?.sessionId,!u.modelUsage){let d=this.aiImpl.getTokenUsage();d&&(u.modelUsage={ai:this.name,model:e,tokens:d})}return this.embedModelUsage=u.modelUsage,this.recordTokenUsage(u.modelUsage),r?.isRecording()&&u.modelUsage?.tokens&&r.addEvent(Ue.GEN_AI_USAGE,{[q.LLM_USAGE_INPUT_TOKENS]:u.modelUsage.tokens.promptTokens,[q.LLM_USAGE_OUTPUT_TOKENS]:u.modelUsage.tokens.completionTokens??0,[q.LLM_USAGE_TOTAL_TOKENS]:u.modelUsage.tokens.totalTokens}),s&&Zs(u.embeddings,n?.logger??this.logger),r?.end(),u}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}};function kl(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(Ue.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(Ue.GEN_AI_ASSISTANT_MESSAGE,a)}else if(o.content){let a={};t||(a.content=o.content),e.addEvent(Ue.GEN_AI_ASSISTANT_MESSAGE,a)}break}case"function":{let s={id:o.functionId};t||(s.content=o.result),e.addEvent(Ue.GEN_AI_TOOL_MESSAGE,s);break}}let r={};t||(r.content=n.join(`
152
+ `)),e.addEvent(Ue.GEN_AI_USER_MESSAGE,r)}function Ai(i,e,t){if(i.modelUsage?.tokens){let n=i.modelUsage.tokens.thoughtsTokens?{[q.LLM_USAGE_THOUGHTS_TOKENS]:i.modelUsage.tokens.thoughtsTokens}:{};e.addEvent(Ue.GEN_AI_USAGE,{[q.LLM_USAGE_INPUT_TOKENS]:i.modelUsage.tokens.promptTokens,[q.LLM_USAGE_OUTPUT_TOKENS]:i.modelUsage.tokens.completionTokens??0,[q.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(Ue.GEN_AI_CHOICE,{finish_reason:r.finishReason,index:n,message:JSON.stringify(s,null,2)})}}function xi(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 Ml(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 un=(m=>(m.Claude41Opus="claude-opus-4-1-20250805",m.Claude4Opus="claude-opus-4-20250514",m.Claude4Sonnet="claude-sonnet-4-20250514",m.Claude45Sonnet="claude-sonnet-4-5-20250929",m.Claude45Haiku="claude-haiku-4-5",m.Claude37Sonnet="claude-3-7-sonnet-latest",m.Claude35Sonnet="claude-3-5-sonnet-latest",m.Claude35Haiku="claude-3-5-haiku-latest",m.Claude3Opus="claude-3-opus-latest",m.Claude3Sonnet="claude-3-sonnet-20240229",m.Claude3Haiku="claude-3-haiku-20240307",m.Claude21="claude-2.1",m.ClaudeInstant12="claude-instant-1.2",m))(un||{}),tr=(s=>(s.Claude37Sonnet="claude-3-7-sonnet",s.Claude35Haiku="claude-3-5-haiku",s.Claude35Sonnet="claude-3-5-sonnet",s.Claude35SonnetV2="claude-3-5-sonnet-v2",s.Claude3Haiku="claude-3-haiku",s.Claude3Opus="claude-3-opus",s))(tr||{});var dn=[{name:"claude-sonnet-4-5-20250929",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:2e5,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"claude-haiku-4-5",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}},{name:"claude-opus-4-20250514",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:75,maxTokens:32e3,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"claude-sonnet-4-20250514",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"claude-3-7-sonnet-latest",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"claude-3-5-sonnet-latest",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:8192},{name:"claude-3-5-haiku-latest",currency:"usd",promptTokenCostPer1M:.8,completionTokenCostPer1M:4,maxTokens:8192},{name:"claude-3-opus-latest",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:75,maxTokens:4096},{name:"claude-3-sonnet-20240229",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,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 nr=i=>{if(!i||typeof i!="object")return i;let e={...i};return delete e.additionalProperties,delete e.default,delete e.optional,delete e.oneOf,delete e.anyOf,delete e.allOf,e.properties&&typeof e.properties=="object"&&(e.properties=Object.fromEntries(Object.entries(e.properties).map(([t,n])=>[t,nr(n)]))),e.items&&(e.items=nr(e.items)),e},Go=()=>structuredClone({model:"claude-3-7-sonnet-latest",maxTokens:4e4,thinkingTokenBudgetLevels:{minimal:1024,low:5e3,medium:1e4,high:2e4,highest:32e3},...Q()}),bi=()=>structuredClone({model:"claude-3-7-sonnet",maxTokens:4e4,thinkingTokenBudgetLevels:{minimal:1024,low:5e3,medium:1e4,high:2e4,highest:32e3},...Q()}),$o=class{constructor(e,t){this.config=e;this.isVertex=t}tokensUsed;currentPromptConfig;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.filter(T=>T.role==="system").map(T=>({type:"text",text:T.content,...T.cache?{cache_control:{type:"ephemeral"}}:{}})),l=e.chatPrompt.filter(T=>T.role!=="system"),c=e.functions?.map(T=>({name:T.name,description:T.description,input_schema:T.parameters?nr(T.parameters):void 0})),u=(this.config.tools??[]).map(T=>T&&typeof T=="object"&&"type"in T?T:{name:T.name,description:T.description,input_schema:T.input_schema?nr(T.input_schema):void 0,...T.cache_control?{cache_control:T.cache_control}:{}}),d=[...c??[],...u];this.isVertex&&d.length>0&&(d=d.filter(T=>!(T&&typeof T=="object"&&"type"in T))),d.length===0&&(d=void 0);let m=e.modelConfig?.maxTokens??this.config.maxTokens,g=e.modelConfig?.stopSequences??this.config.stopSequences,f=e.modelConfig?.temperature,h=e.modelConfig?.topP,A=e.modelConfig?.topK??this.config.topK,x=e.modelConfig?.n??this.config.n;if(x&&x>1)throw new Error("Anthropic does not support sampling (n > 1)");let y;if(this.config.thinking?.budget_tokens&&(y=this.config.thinking),t?.thinkingTokenBudget){let T=this.config.thinkingTokenBudgetLevels;switch(t.thinkingTokenBudget){case"none":y=void 0;break;case"minimal":y={type:"enabled",budget_tokens:T?.minimal??1024};break;case"low":y={type:"enabled",budget_tokens:T?.low??5e3};break;case"medium":y={type:"enabled",budget_tokens:T?.medium??1e4};break;case"high":y={type:"enabled",budget_tokens:T?.high??2e4};break;case"highest":y={type:"enabled",budget_tokens:T?.highest??32e3};break}}if(!y&&t?.thinkingTokenBudget===void 0){let T=this.config.thinkingTokenBudgetLevels}let b=El(l,!!y);b.some(T=>T.role==="assistant"&&Array.isArray(T.content)&&T.content.length>0&&T.content[0]?.type==="tool_use")&&(y=void 0);let I={...this.isVertex?{anthropic_version:"vertex-2023-10-16"}:{model:n},...m?{max_tokens:m}:{},...g&&g.length>0?{stop_sequences:g}:{},...f!==void 0&&!y?{temperature:f}:{},...h!==void 0&&(!y||h>=.95)?{top_p:h}:{},...A&&!y?{top_k:A}:{},...s,...d?{tools:d}:{},...r?{stream:!0}:{},...a?{system:a}:{},...y?{thinking:y}:{},messages:b};return[o,I]};createChatResp=e=>{if(e.type==="error")throw new ae(e.error.message,void 0,void 0);let t=yi(e.stop_reason),n=this.currentPromptConfig?.thinkingTokenBudget!=="none"&&this.currentPromptConfig?.showThoughts!==!1,r="",o="",s=!1,a,l=[],c=[];for(let d of e.content)switch(d.type){case"text":if(r+=d.text??"",Array.isArray(d.citations))for(let m of d.citations)m?.url&&c.push({url:String(m.url),title:typeof m.title=="string"?m.title:void 0,snippet:typeof m.cited_text=="string"?m.cited_text:void 0});break;case"thinking":case"redacted_thinking":n&&(o+=d.thinking??d.data??""),d.type==="redacted_thinking"&&(s=!0),typeof d.signature=="string"&&(a=d.signature);break;case"tool_use":l.push({id:d.id,type:"function",function:{name:d.name,params:d.input}});break}let p={index:0,id:e.id,finishReason:t};r&&(p.content=r),o&&(p.thought=o,p.thoughtBlock={data:o,encrypted:s,...a?{signature:a}:{}}),l.length>0&&(p.functionCalls=l),c.length>0&&(p.citations=c);let u=[p];return this.tokensUsed={promptTokens:e.usage.input_tokens,completionTokens:e.usage.output_tokens,totalTokens:e.usage.input_tokens+e.usage.output_tokens,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 ae(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)},{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,thoughtBlock:{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,thoughtBlock:{data:o.thinking,encrypted:!1}}]}:{results:[{index:r,content:""}]};if(o.type==="signature_delta")return{results:[{index:r,content:""}]};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:0,completionTokens:s.output_tokens,totalTokens:s.output_tokens},{results:[{index:r,content:"",finishReason:yi(o.stop_reason)}]}}return{results:[{index:r,content:""}]}}},Rt=class i extends de{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,c;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}-aiplatform.googleapis.com/v1/projects/${t}/locations/${n}/publishers/anthropic/`,c=async()=>({Authorization:`Bearer ${await e()}`})}else{if(!e)throw new Error("Anthropic API key not set");l="https://api.anthropic.com/v1",c=async()=>({"anthropic-version":"2023-06-01","anthropic-beta":"prompt-caching-2024-07-31","x-api-key":typeof e=="function"?await e():e})}let p={...Go(),...r},u=new $o(p,a),d=g=>{let f=Me({model:g,modelInfo:dn,models:s});return{functions:!0,streaming:!0,hasThinkingBudget:f?.supported?.thinkingBudget??!1,hasShowThoughts:f?.supported?.showThoughts??!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"]},thinking:f?.supported?.thinkingBudget??!1,multiTurn:!0}},m=s?.map(g=>{let f=g,h=f?.config;if(!h)return g;let A={};h.maxTokens!==void 0&&(A.maxTokens=h.maxTokens),h.temperature!==void 0&&(A.temperature=h.temperature),h.topP!==void 0&&(A.topP=h.topP),h.topK!==void 0&&(A.topK=h.topK),h.presencePenalty!==void 0&&(A.presencePenalty=h.presencePenalty),h.frequencyPenalty!==void 0&&(A.frequencyPenalty=h.frequencyPenalty),h.stopSequences!==void 0&&(A.stopSequences=h.stopSequences),h.endSequences!==void 0&&(A.endSequences=h.endSequences),h.stream!==void 0&&(A.stream=h.stream),h.n!==void 0&&(A.n=h.n);let x={...f};Object.keys(A).length>0&&(x.modelConfig={...f.modelConfig??{},...A});let y=h.thinking?.thinkingTokenBudget;if(typeof y=="number"){let b=p.thinkingTokenBudgetLevels,S=[["minimal",b?.minimal??200],["low",b?.low??800],["medium",b?.medium??5e3],["high",b?.high??1e4],["highest",b?.highest??24500]],I="minimal",T=Number.POSITIVE_INFINITY;for(let[C,N]of S){let U=Math.abs(y-N);U<T&&(T=U,I=C)}x.thinkingTokenBudget=I}return h.thinking?.includeThoughts!==void 0&&(x.showThoughts=!!h.thinking.includeThoughts),x});super(u,{name:"Anthropic",apiURL:l,headers:c,modelInfo:dn,defaults:{model:p.model},options:o,supportFor:d,models:m??s})}};function El(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}:{role:"user",content:r.content.map(s=>{switch(s.type){case"text":return{type:"text",text:s.text,...s.cache?{cache:{type:"ephemeral"}}:{}};case"image":return{type:"image",source:{type:"base64",media_type:s.mimeType,data:s.image},...s.cache?{cache:{type:"ephemeral"}}:{}};default:throw new Error("Invalid content type")}})};case"assistant":{let o="",s=[],a=r.thoughtBlock;return a&&typeof a.data=="string"&&a.data.length>0&&(a.encrypted?s.push(a.signature?{type:"redacted_thinking",data:a.data,signature:a.signature}:{type:"redacted_thinking",data:a.data}):s.push(a.signature?{type:"thinking",thinking:a.data,signature:a.signature}:{type:"thinking",thinking:a.data})),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 c={};return typeof l.function.params=="string"?c=JSON.parse(l.function.params):typeof l.function.params=="object"&&(c=l.function.params),{type:"tool_use",id:l.id,name:l.function.name,input:c,...r.cache?{cache:{type:"ephemeral"}}:{}}}),Array.isArray(o)&&s.length>0&&(o=[...s,...o])),{role:"assistant",content:o}}default:throw new Error("Invalid role")}}),n=Pl(t);return Fl(n)}function Pl(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 Fl(i){return i.map(e=>e.role==="assistant"&&typeof e.content=="string"?{...e,content:e.content.replace(/\s+$/,"")}:e)}function yi(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 mn=(I=>(I.GPT4="gpt-4",I.GPT41="gpt-4.1",I.GPT41Mini="gpt-4.1-mini",I.GPT41Nano="gpt-4.1-nano",I.GPT4O="gpt-4o",I.GPT4OMini="gpt-4o-mini",I.GPT4ChatGPT4O="chatgpt-4o-latest",I.GPT4Turbo="gpt-4-turbo",I.GPT35Turbo="gpt-3.5-turbo",I.GPT35TurboInstruct="gpt-3.5-turbo-instruct",I.GPT35TextDavinci002="text-davinci-002",I.GPT3TextBabbage002="text-babbage-002",I.GPT3TextAda001="text-ada-001",I.GPT5="gpt-5",I.GPT5Nano="gpt-5-nano",I.GPT5Mini="gpt-5-mini",I.GPT5Chat="gpt-5-chat",I.O1="o1",I.O1Mini="o1-mini",I.O3="o3",I.O3Mini="o3-mini",I.O4Mini="o4-mini",I))(mn||{}),wt=(n=>(n.TextEmbeddingAda002="text-embedding-ada-002",n.TextEmbedding3Small="text-embedding-3-small",n.TextEmbedding3Large="text-embedding-3-large",n))(wt||{});var St=(C=>(C.GPT4="gpt-4",C.GPT41="gpt-4.1",C.GPT41Mini="gpt-4.1-mini",C.GPT41Nano="gpt-4.1-nano",C.GPT4O="gpt-4o",C.GPT4OMini="gpt-4o-mini",C.GPT4ChatGPT4O="chatgpt-4o-latest",C.GPT4Turbo="gpt-4-turbo",C.GPT35Turbo="gpt-3.5-turbo",C.GPT35TurboInstruct="gpt-3.5-turbo-instruct",C.GPT35TextDavinci002="text-davinci-002",C.GPT3TextBabbage002="text-babbage-002",C.GPT3TextAda001="text-ada-001",C.GPT5="gpt-5",C.GPT5Nano="gpt-5-nano",C.GPT5Mini="gpt-5-mini",C.GPT5Chat="gpt-5-chat",C.O1Pro="o1-pro",C.O1="o1",C.O1Mini="o1-mini",C.O3Pro="o3-pro",C.O3="o3",C.O3Mini="o3-mini",C.O4Mini="o4-mini",C))(St||{});var vt=[{name:"gpt-4",currency:"usd",promptTokenCostPer1M:30,completionTokenCostPer1M:60},{name:"gpt-4.1",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:8},{name:"gpt-4.1-mini",currency:"usd",promptTokenCostPer1M:.4,completionTokenCostPer1M:1.6},{name:"gpt-4.1-nano",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.4},{name:"gpt-4o",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:15},{name:"gpt-4o-mini",currency:"usd",promptTokenCostPer1M:.15,completionTokenCostPer1M:.6},{name:"chatgpt-4o-latest",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:15},{name:"gpt-4-turbo",currency:"usd",promptTokenCostPer1M:10,completionTokenCostPer1M:30},{name:"gpt-3.5-turbo",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5},{name:"gpt-5-nano",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5,notSupported:{temperature:!0,topP:!0}},{name:"gpt-5-mini",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:6,notSupported:{temperature:!0,topP:!0}},{name:"gpt-5",currency:"usd",promptTokenCostPer1M:10,completionTokenCostPer1M:30,notSupported:{temperature:!0,topP:!0}},{name:"gpt-5-chat",currency:"usd",promptTokenCostPer1M:12,completionTokenCostPer1M:36,notSupported:{temperature:!0,topP:!0}},{name:"gpt-5",currency:"usd",promptTokenCostPer1M:20,completionTokenCostPer1M:60,notSupported:{temperature:!0,topP:!0}},{name:"o1",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:60},{name:"o1-mini",currency:"usd",promptTokenCostPer1M:1.1,completionTokenCostPer1M:14.4},{name:"o3",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:60},{name:"o4-mini",currency:"usd",promptTokenCostPer1M:1.1,completionTokenCostPer1M:4.4},{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}],rr=[{name:"gpt-4",currency:"usd",promptTokenCostPer1M:30,completionTokenCostPer1M:60},{name:"gpt-4.1",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:8},{name:"gpt-4.1-mini",currency:"usd",promptTokenCostPer1M:.4,completionTokenCostPer1M:1.6},{name:"gpt-4.1-nano",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.4},{name:"gpt-4o",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:15},{name:"gpt-4o-mini",currency:"usd",promptTokenCostPer1M:.15,completionTokenCostPer1M:.6},{name:"chatgpt-4o-latest",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:15},{name:"gpt-4-turbo",currency:"usd",promptTokenCostPer1M:10,completionTokenCostPer1M:30},{name:"gpt-3.5-turbo",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5},{name:"gpt-5-nano",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gpt-5-mini",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:6,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gpt-5",currency:"usd",promptTokenCostPer1M:10,completionTokenCostPer1M:30,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gpt-5-chat",currency:"usd",promptTokenCostPer1M:12,completionTokenCostPer1M:36,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gpt-5",currency:"usd",promptTokenCostPer1M:20,completionTokenCostPer1M:60,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0}},{name:"o1-pro",currency:"usd",promptTokenCostPer1M:150,completionTokenCostPer1M:600,supported:{thinkingBudget:!0,showThoughts:!0},isExpensive:!0},{name:"o1",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:60,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"o3-pro",currency:"usd",promptTokenCostPer1M:20,completionTokenCostPer1M:80,supported:{thinkingBudget:!0,showThoughts:!0},isExpensive:!0},{name:"o3",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:60,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"o3-mini",currency:"usd",promptTokenCostPer1M:1.1,completionTokenCostPer1M:4.4,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"o4-mini",currency:"usd",promptTokenCostPer1M:1.1,completionTokenCostPer1M:4.4,supported:{thinkingBudget:!0,showThoughts:!0}}];var _l=i=>{let e=["o1","o1-mini","o3","o3-mini","o4-mini","o1-pro","o3-pro"];return e.includes(i)||e.includes(i)},mt=()=>structuredClone({model:"gpt-5-mini",embedModel:"text-embedding-3-small",...Q()}),or=()=>structuredClone({...mt(),model:"gpt-5"}),sr=()=>structuredClone({model:"gpt-5-mini",embedModel:"text-embedding-3-small",...me()}),ir=()=>({...mt(),model:"gpt-5-nano"}),Lo=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=Nl(e),l=e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,c=e.modelConfig?.stream??this.config.stream,p=this.config.store,u=_l(n),d={model:n,messages:a,...this.config?.responseFormat?{response_format:{type:this.config.responseFormat}}:{},...o?{tools:o}:{},...s?{tool_choice:s}:{},...u?{}:{...(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}:{},...c&&this.streamingUsage?{stream:!0,stream_options:{include_usage:!0}}:{},...p?{store:p}:{},...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 ae(a.message.refusal,e.model,e.id);let l=Ii(a.finish_reason),c=a.message.tool_calls?.map(({id:p,function:{arguments:u,name:d}})=>({id:p,type:"function",function:{name:d,params:u}}));return{index:a.index,id:`${a.index}`,content:a.message.content??void 0,thought:a.message.reasoning_content,citations:a.message.annotations?.filter(p=>p?.type==="url_citation"&&p.url_citation).map(p=>({url:p.url_citation?.url,title:p.url_citation?.title,description:p.url_citation?.description})),functionCalls:c,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:c,role:p,refusal:u,tool_calls:d,reasoning_content:m,annotations:g},finish_reason:f})=>{if(u)throw new ae(u,void 0,n);let h=Ii(f),A=d?.map(({id:x,index:y,function:{name:b,arguments:S}})=>{typeof x=="string"&&typeof y=="number"&&!s.indexIdMap[y]&&(s.indexIdMap[y]=x);let I=s.indexIdMap[y];return I?{id:I,type:"function",function:{name:b,params:S}}:null}).filter(x=>x!==null);return{index:l,content:c??void 0,role:p,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:A,finishReason:h,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)}}},Ii=i=>{switch(i){case"stop":return"stop";case"length":return"length";case"content_filter":return"error";case"tool_calls":return"function_call"}};function Nl(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 ue=class extends de{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 c=new Lo(t,n?.streamingUsage??!0,a);super(c,{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})}},Ot=class extends ue{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=[...vt,...s??[]];let a=c=>{let p=Me({model:c,modelInfo:s,models:o});return{functions:!0,streaming:!0,hasThinkingBudget:p?.supported?.thinkingBudget??!1,hasShowThoughts:p?.supported?.showThoughts??!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:p?.supported?.thinkingBudget??!1,multiTurn:!0}},l=o?.map(c=>{let p=c,u=p?.config;if(!u)return c;let d={};u.maxTokens!==void 0&&(d.maxTokens=u.maxTokens),u.temperature!==void 0&&(d.temperature=u.temperature),u.topP!==void 0&&(d.topP=u.topP),u.presencePenalty!==void 0&&(d.presencePenalty=u.presencePenalty),u.frequencyPenalty!==void 0&&(d.frequencyPenalty=u.frequencyPenalty);let m=u.stopSequences??u.stop;m!==void 0&&(d.stopSequences=m),u.n!==void 0&&(d.n=u.n),u.stream!==void 0&&(d.stream=u.stream);let g={...p};Object.keys(d).length>0&&(g.modelConfig={...p.modelConfig??{},...d});let f=u?.thinking?.thinkingTokenBudget;if(typeof f=="number"){let h=[["minimal",200],["low",800],["medium",5e3],["high",1e4],["highest",24500]],A="minimal",x=Number.POSITIVE_INFINITY;for(let[y,b]of h){let S=Math.abs(f-b);S<x&&(x=S,A=y)}g.thinkingTokenBudget=A}return u?.thinking?.includeThoughts!==void 0&&(g.showThoughts=!!u.thinking.includeThoughts),g});super({apiKey:e,apiURL:t,config:{...mt(),...n},options:r,modelInfo:s,models:l??o,supportFor:a}),super.setName("OpenAI")}};var Uo=mt,Ti=sr,Ci=ir,Ri=or,kt=class extends ue{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 c={...Uo(),...o};l=[...vt,...l??[]];let p=d=>{let m=Me({model:d,modelInfo:l,models:a});return{functions:!0,streaming:!0,hasThinkingBudget:m?.supported?.thinkingBudget??!1,hasShowThoughts:m?.supported?.showThoughts??!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:m?.supported?.thinkingBudget??!1,multiTurn:!0}};super({apiKey:e,config:c,options:s,models:a,modelInfo:l,supportFor:p});let u=t.includes("://")?t:`https://${t}.openai.azure.com/`;super.setName("Azure OpenAI"),super.setAPIURL(new URL(`/openai/deployments/${n}?api-version=${r}`,u).href),super.setHeaders(async()=>({"api-key":e}))}};var ar=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.");Dl(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(){return this.currentService.getModelList()}getNextService(){let e=this.services[++this.currentServiceIndex];return e===void 0?!1:(this.currentService=e,!0)}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){return this.currentService.getFeatures(e)}getMetrics(){return this.currentService.getMetrics()}canRetryService(){let e=this.serviceFailures.get(this.currentService.getId());if(!e)return!0;let{retries:t,lastFailureTime:n}=e,r=Date.now()-n,o=Math.min(this.initialBackoffMs*2**t,this.maxBackoffMs);return r>=o}handleFailure(e){let n=(this.serviceFailures.get(this.currentService.getId())?.retries??0)+1;if(this.serviceFailures.set(this.currentService.getId(),{retries:n,lastFailureTime:Date.now()}),this.debug&&console.warn(`AxBalancer: Service ${this.currentService.getName()} failed (retry ${n}/${this.maxRetries})`,e),n>=this.maxRetries){let r=this.getNextService();return this.debug&&console.warn(`AxBalancer: Switching to service ${this.currentService.getName()}`,e),r}return!0}handleSuccess(){this.serviceFailures.delete(this.currentService.getId())}async chat(e,t){for(this.reset();;){if(!this.canRetryService()){if(!this.getNextService())throw new Error("All services exhausted");continue}try{let n=await this.currentService.chat(e,t);return this.handleSuccess(),n}catch(n){if(!(n instanceof Ie))throw n;switch(n.constructor){case rt:throw n;case ut:break;case je:break;case Xe:break;case Ge:break;case dt:break;default:throw n}if(!this.handleFailure(n))throw n}}}async embed(e,t){for(this.reset();;){if(!this.canRetryService()){if(!this.getNextService())throw new Error("All services exhausted");continue}try{let n=await this.currentService.embed(e,t);return this.handleSuccess(),n}catch(n){if(!(n instanceof Ie)||!this.handleFailure(n))throw n}}}setOptions(e){this.currentService.setOptions(e)}getOptions(){return this.currentService.getOptions()}getLogger(){return this.currentService.getLogger()}};function Dl(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 gt(i){let e=!1,t=!1,n=!1,r=!1,o=!1,s=!1,a=!1,l=new Set,c=0;if(i.chatPrompt&&Array.isArray(i.chatPrompt))for(let p of i.chatPrompt){if(p.role==="user"&&Array.isArray(p.content))for(let u of p.content)switch(l.add(u.type),u.type){case"image":e=!0,u.cache&&(a=!0),c+=85;break;case"audio":t=!0,u.cache&&(a=!0),c+=u.duration||60;break;case"file":n=!0,u.cache&&(a=!0),c+=Math.ceil((u.extractedText?.length||1e3)/4);break;case"url":r=!0,u.cache&&(a=!0),c+=Math.ceil((u.cachedContent?.length||2e3)/4);break;case"text":u.cache&&(a=!0),c+=Math.ceil(u.text.length/4);break}else"content"in p&&typeof p.content=="string"&&(c+=Math.ceil(p.content.length/4));"cache"in p&&p.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:c}}function Bo(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 lr(i,e){return i.map(t=>{let n=t.getFeatures(),r=Bo(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 cr(i,e,t={}){if(e.length===0)throw new Error("No providers available");let n=gt(i),r=lr(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 wi(i,e){let t=gt(i),n=lr(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 Si(i,e){return i.filter(t=>t.getFeatures().media[e].supported)}function vi(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 gn=(r=>(r.CommandRPlus="command-r-plus",r.CommandR="command-r",r.Command="command",r.CommandLight="command-light",r))(gn||{}),hn=(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))(hn||{});var pr=[{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}];var zo=()=>structuredClone({model:"command-r-plus",embedModel:"embed-english-v3.0",...Q()}),ki=()=>structuredClone({model:"command-r",embedModel:"embed-english-v3.0",...me()}),qo=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=$l(r),a=e.functions?.map(u=>{let d={};if(u.parameters?.properties)for(let[m,g]of Object.entries(u.parameters.properties))d[m]={description:g.description,type:g.type,required:u.parameters.required?.includes(m)??!1};return{name:u.name,description:u.description,parameter_definitions:d}}),l=e.chatPrompt.filter(u=>u.role==="function").map(u=>{let d=a?.find(m=>m.name===u.functionId);if(!d)throw new Error("Function not found");return{call:{name:d.name,parameters:d.parameter_definitions},outputs:[{result:u.result??""}]}}),c={name:"/chat"},p={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[c,p]}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}}},Mt=class extends de{constructor({apiKey:e,config:t,options:n,models:r}){if(!e||e==="")throw new Error("Cohere API key not set");let o={...zo(),...t},s=new qo(o),a=r?.map(l=>{let c=l,p=c?.config;if(!p)return l;let u={};p.maxTokens!==void 0&&(u.maxTokens=p.maxTokens),p.temperature!==void 0&&(u.temperature=p.temperature),p.topP!==void 0&&(u.topP=p.topP),p.topK!==void 0&&(u.topK=p.topK),p.presencePenalty!==void 0&&(u.presencePenalty=p.presencePenalty),p.frequencyPenalty!==void 0&&(u.frequencyPenalty=p.frequencyPenalty),p.stopSequences!==void 0&&(u.stopSequences=p.stopSequences),p.endSequences!==void 0&&(u.endSequences=p.endSequences),p.stream!==void 0&&(u.stream=p.stream),p.n!==void 0&&(u.n=p.n);let d={...c};return Object.keys(u).length>0&&(d.modelConfig={...c.modelConfig??{},...u}),d});super(s,{name:"Cohere",apiURL:"https://api.cohere.ai/v1",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:pr,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 $l(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=Oi(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=Oi(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 Oi(i){return i?.map(e=>{let t=typeof e.function.params=="string"?JSON.parse(e.function.params):e.function.params;return{name:e.function.name,parameters:t}})}var fn=(n=>(n.DeepSeekChat="deepseek-chat",n.DeepSeekCoder="deepseek-coder",n.DeepSeekReasoner="deepseek-reasoner",n))(fn||{});var ur=[{name:"deepseek-chat",currency:"USD",promptTokenCostPer1M:.27,completionTokenCostPer1M:1.1},{name:"deepseek-reasoner",currency:"USD",promptTokenCostPer1M:.55,completionTokenCostPer1M:2.19}];var jo=()=>structuredClone({model:"deepseek-chat",...Q()}),Mi=()=>structuredClone({model:"deepseek-coder",...me()}),Et=class extends ue{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("DeepSeek API key not set");let s={...jo(),...t};o=[...ur,...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 An=(m=>(m.Gemini25Pro="gemini-2.5-pro",m.Gemini25Flash="gemini-2.5-flash",m.Gemini25FlashLite="gemini-2.5-flash-lite",m.Gemini20Flash="gemini-2.0-flash",m.Gemini20FlashLite="gemini-2.0-flash-lite",m.Gemini1Pro="gemini-1.0-pro",m.Gemini15Flash="gemini-1.5-flash",m.Gemini15Flash002="gemini-1.5-flash-002",m.Gemini15Flash8B="gemini-1.5-flash-8b",m.Gemini15Pro="gemini-1.5-pro",m.GeminiFlashLatest="gemini-flash-latest",m.GeminiFlashLiteLatest="gemini-flash-lite-latest",m.GeminiProLatest="gemini-pro-latest",m))(An||{}),dr=(r=>(r.GeminiEmbedding="gemini-embedding-exp",r.TextEmbeddingLarge="text-embedding-large-exp-03-07",r.TextEmbedding004="text-embedding-004",r.TextEmbedding005="text-embedding-005",r))(dr||{}),mr=(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))(mr||{}),gr=(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))(gr||{}),Ho=(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))(Ho||{});var hr=[{name:"gemini-2.5-pro",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:2.5,completionTokenCostPer1M:15,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gemini-2.5-flash",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:15,completionTokenCostPer1M:3.5,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gemini-2.5-flash-lite",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.1,completionTokenCostPer1M:.4,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gemini-2.0-flash",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.01,completionTokenCostPer1M:.4},{name:"gemini-2.0-flash-lite",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:0,completionTokenCostPer1M:0},{name:"gemini-1.5-flash",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.075,completionTokenCostPer1M:.3},{name:"gemini-1.5-flash-8b",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.0375,completionTokenCostPer1M:.15},{name:"gemini-1.5-pro",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:1.25,completionTokenCostPer1M:5},{name:"gemini-1.0-pro",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5},{name:"gemini-flash-latest",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.075,completionTokenCostPer1M:.3,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gemini-flash-lite-latest",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:0,completionTokenCostPer1M:0,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"gemini-pro-latest",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:1.25,completionTokenCostPer1M:5,supported:{thinkingBudget:!0,showThoughts:!0}}];var Ko=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,e.properties&&typeof e.properties=="object"&&(e.properties=Object.fromEntries(Object.entries(e.properties).map(([t,n])=>[t,Ko(n)]))),e.items&&(e.items=Ko(e.items)),e},Ei=[{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"}],Vo=()=>structuredClone({model:"gemini-2.5-flash",embedModel:"text-embedding-005",safetySettings:Ei,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},...Q()}),Pi=()=>structuredClone({model:"gemini-2.0-flash",embedModel:"text-embedding-005",safetySettings:Ei,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},...me()}),Wo=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")}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,endSequences:e.endSequences,stream:e.stream,n:e.n}}createChatReq=async(e,t)=>{let n=e.model,r=e.modelConfig?.stream??this.config.stream;if(!e.chatPrompt||e.chatPrompt.length===0)throw new Error("Chat prompt is empty");let o;if(this.endpointId?o={name:r?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:o={name:r?`/models/${n}:streamGenerateContent?alt=sse`:`/models/${n}:generateContent`},!this.isVertex){let h=r?"&":"?",A=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;o.name+=`${h}key=${A}`}let s=e.chatPrompt.filter(h=>h.role==="system").map(h=>h.content),a=s.length>0?{role:"user",parts:[{text:s.join(" ")}]}:void 0,l=e.chatPrompt.filter(h=>h.role!=="system").map((h,A)=>{switch(h.role){case"user":return{role:"user",parts:Array.isArray(h.content)?h.content.map((y,b)=>{switch(y.type){case"text":return{text:y.text};case"image":return{inlineData:{mimeType:y.mimeType,data:y.image}};case"audio":return{inlineData:{mimeType:`audio/${y.format??"mp3"}`,data:y.data}};case"file":return"fileUri"in y?{fileData:{mimeType:y.mimeType,fileUri:y.fileUri}}:{inlineData:{mimeType:y.mimeType,data:y.data}};default:throw new Error(`Chat prompt content type not supported (index: ${b})`)}}):[{text:h.content}]};case"assistant":{let x=[];if(h.functionCalls){if(x=h.functionCalls.map(y=>{let b=typeof y.function.params=="string"?JSON.parse(y.function.params):y.function.params;return{functionCall:{name:y.function.name,args:b}}}),!x)throw new Error("Function call is empty");return{role:"model",parts:x}}if(!h.content)throw new Error("Assistant content is empty");return x=[{text:h.content}],{role:"model",parts:x}}case"function":{if(!("functionId"in h))throw new Error(`Chat prompt functionId is empty (index: ${A})`);return{role:"user",parts:[{functionResponse:{name:h.functionId,response:{result:h.result}}}]}}default:throw new Error(`Invalid role: ${JSON.stringify(h)} (index: ${A})`)}}),c=[];if(e.functions&&e.functions.length>0){let h=e.functions.map(A=>({...A,parameters:A.parameters?Ko(A.parameters):void 0}));c.push({function_declarations:h})}if(this.options?.codeExecution&&c.push({code_execution:{}}),this.options?.googleSearchRetrieval&&c.push({google_search_retrieval:{dynamic_retrieval_config:this.options.googleSearchRetrieval}}),this.options?.googleSearch&&c.push({google_search:{}}),this.options?.googleMaps){let h=this.options.googleMaps,A=h?.enableWidget!==void 0?{enableWidget:h.enableWidget}:{};c.push({google_maps:A})}this.options?.urlContext&&c.push({url_context:{}}),c.length===0&&(c=void 0);let p,u=Array.isArray(c)?c.some(h=>h&&Array.isArray(h.function_declarations)&&h.function_declarations.length>0):!1;if(e.functionCall)if(e.functionCall==="none")p={function_calling_config:{mode:"NONE"}};else if(e.functionCall==="auto")p={function_calling_config:{mode:"AUTO"}};else if(e.functionCall==="required")p={function_calling_config:{mode:"ANY"}};else{let h=e.functionCall.function?.name?{allowedFunctionNames:[e.functionCall.function.name]}:{};p={function_calling_config:{mode:"ANY"},...h}}else u&&(p={function_calling_config:{mode:"AUTO"}});this.options?.retrievalConfig&&(p={...p??{},retrievalConfig:{...this.options.retrievalConfig.latLng?{latLng:this.options.retrievalConfig.latLng}:{}}});let d={};if(this.config.thinking?.includeThoughts&&(d.includeThoughts=!0),this.config.thinking?.thinkingTokenBudget&&(d.thinkingBudget=this.config.thinking.thinkingTokenBudget),t?.thinkingTokenBudget){let h=this.config.thinkingTokenBudgetLevels;switch(t.thinkingTokenBudget){case"none":d.thinkingBudget=0,d.includeThoughts=!1;break;case"minimal":d.thinkingBudget=h?.minimal??200;break;case"low":d.thinkingBudget=h?.low??800;break;case"medium":d.thinkingBudget=h?.medium??5e3;break;case"high":d.thinkingBudget=h?.high??1e4;break;case"highest":d.thinkingBudget=h?.highest??24500;break}}t?.showThoughts!==void 0&&t?.thinkingTokenBudget!=="none"&&(d.includeThoughts=t.showThoughts);let m={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.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(d).length>0?{thinkingConfig:d}:{}},g=this.config.safetySettings;return[o,{contents:l,tools:c,toolConfig:p,systemInstruction:a,generationConfig:m,safetySettings:g}]};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 ae("Content was blocked due to safety settings",void 0,void 0);case"RECITATION":throw new ae("Content was blocked due to recitation policy",void 0,void 0);case"MALFORMED_FUNCTION_CALL":throw new ae("Function call was malformed and blocked",void 0,void 0);case"UNEXPECTED_TOOL_CALL":throw new ae("Unexpected tool call",void 0,void 0);case"FINISH_REASON_UNSPECIFIED":throw new ae("Finish reason unspecified",void 0,void 0);case"BLOCKLIST":throw new ae("Content was blocked due to blocklist",void 0,void 0);case"PROHIBITED_CONTENT":throw new ae("Content was blocked due to prohibited content",void 0,void 0);case"SPII":throw new ae("Content was blocked due to SPII",void 0,void 0);case"OTHER":throw new ae("Other finish reason",void 0,void 0)}if(!o.content||!o.content.parts)return s;for(let c of o.content.parts){if("text"in c){"thought"in c&&c.thought?s.thought=c.text:s.content=c.text;continue}"functionCall"in c&&(s.functionCalls=[{id:fe(),type:"function",function:{name:c.functionCall.name,params:c.functionCall.args}}])}let a=o.citationMetadata?.citations;if(Array.isArray(a)&&a.length){let c=p=>p?`${p.year}-${String(p.month).padStart(2,"0")}-${String(p.day).padStart(2,"0")}`:void 0;s.citations=a.filter(p=>typeof p?.uri=="string").map(p=>({url:p.uri,title:p.title,license:p.license,publicationDate:c(p.publicationDate)}))}let l=o.groundingMetadata;if(l){if(Array.isArray(l.groundingChunks)){let c=l.groundingChunks.map(p=>p?.maps).filter(p=>p&&typeof p.uri=="string").map(p=>({url:p.uri,title:p.title}));c.length&&(s.citations=[...s.citations??[],...c])}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});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}}},Pt=class i extends de{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 c=t!==void 0&&n!==void 0,p,u;if(c){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 h;r?h="endpoints":h="publishers/google",p=`https://${n==="global"?"aiplatform":`${n}-aiplatform`}.googleapis.com/v1/projects/${t}/locations/${n}/${h}`,u=async()=>({Authorization:`Bearer ${typeof e=="function"?await e():e}`})}else{if(!e)throw new Error("GoogleGemini AI API key not set");p="https://generativelanguage.googleapis.com/v1beta",u=async()=>({})}let d={...Vo(),...o},m=new Wo(d,c,r,e,s);l=[...hr,...l??[]];let g=h=>{let A=Me({model:h,modelInfo:l,models:a});return{functions:!0,streaming:!0,hasThinkingBudget:A?.supported?.thinkingBudget??!1,hasShowThoughts:A?.supported?.showThoughts??!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:!1,types:[]},thinking:A?.supported?.thinkingBudget??!1,multiTurn:!0}},f=a?.map(h=>{let A=h,x=A?.config;if(!x)return h;let y={};x.maxTokens!==void 0&&(y.maxTokens=x.maxTokens),x.temperature!==void 0&&(y.temperature=x.temperature),x.topP!==void 0&&(y.topP=x.topP),x.topK!==void 0&&(y.topK=x.topK),x.presencePenalty!==void 0&&(y.presencePenalty=x.presencePenalty),x.frequencyPenalty!==void 0&&(y.frequencyPenalty=x.frequencyPenalty),x.stopSequences!==void 0&&(y.stopSequences=x.stopSequences),x.endSequences!==void 0&&(y.endSequences=x.endSequences),x.stream!==void 0&&(y.stream=x.stream),x.n!==void 0&&(y.n=x.n);let b={...A};Object.keys(y).length>0&&(b.modelConfig={...A.modelConfig??{},...y});let S=x.thinking?.thinkingTokenBudget;if(typeof S=="number"){let I=d.thinkingTokenBudgetLevels,T=[["minimal",I?.minimal??200],["low",I?.low??800],["medium",I?.medium??5e3],["high",I?.high??1e4],["highest",I?.highest??24500]],C="minimal",N=Number.POSITIVE_INFINITY;for(let[U,k]of T){let w=Math.abs(S-k);w<N&&(N=w,C=U)}b.thinkingTokenBudget=C}return x.thinking?.includeThoughts!==void 0&&(b.showThoughts=!!x.thinking.includeThoughts),b});super(m,{name:"GoogleGeminiAI",apiURL:p,headers:u,modelInfo:l,defaults:{model:d.model,embedModel:d.embedModel},options:s,supportFor:g,models:f??a})}};var Gl=new Te,Ft=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(Gl.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 xn=(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))(xn||{});var fr=[{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 Ll=()=>structuredClone({model:"llama-3.3-70b-versatile",...Q()}),_t=class extends ue{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Groq API key not set");let s={...Ll(),...t},a={...n,streamingUsage:!1};o=[...fr,...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 Ft(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 Ar=[];var xr=(e=>(e.MetaLlama270BChatHF="meta-llama/Llama-2-70b-chat-hf",e))(xr||{});var Qo=()=>structuredClone({model:"meta-llama/Llama-2-70b-chat-hf",...Q()}),Fi=()=>structuredClone({model:"meta-llama/Llama-2-70b-chat-hf",...me()}),Jo=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:
147
153
  ${JSON.stringify(e.functions,null,2)}
148
154
  `:"",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 c=l.functionCalls?.map(p=>{let u=typeof p.function.params=="string"?p.function.params:JSON.stringify(p.function.params);return`${p.function.name}(${u})`}).join(`
149
155
  `);return c?`Assistant: ${l.content}
@@ -192,9 +198,9 @@ Tip: Arrays must include an "items" schema (e.g., { items: { type: "string" } }
192
198
  ${m.title}:`,f=n.indexOf(g,t.s);f!==-1&&f<c&&(c=f)}let u=n.substring(t.s,c).trim(),d=qr(t.currField,u);d!==void 0&&(e[t.currField.name]=d)}if(o&&!t.currField&&t.extractedFields.length===0&&n.trim()){let u=i.getOutputFields().find(d=>!d.isOptional);if(u)throw Gr(u)}if(Ql(i,e,n,t),!s){let c=t.currField!==void 0||(t.extractedFields?.length??0)>0;o||l?fs(t,e,i.getOutputFields()):c||fs(t,e,i.getOutputFields())}},Ql=(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 c=l+a.length,p=`
193
199
  ${s.title}:`,u=t.indexOf(p,c),d=t.substring(c,u===-1?t.length:u).trim();if(d)try{let m=qr(s,d);if(m!==void 0){e[s.name]=m;return}}catch{}}}}let o=t.split(`
194
200
  `);for(let s of r){if(s.name in e)continue;let a=`${s.title}:`;for(let l of o){let c=l.trim();if(c.startsWith(a)){let p=c.substring(a.length).trim();if(p)try{let u=qr(s,p);if(u!==void 0){e[s.name]=u;break}}catch(u){if(!s.isOptional)throw u}break}}}},Ta=(i,e,t=!1)=>{switch(i.type?.name){case"code":return Ra(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 Aa(i,e,t);case"datetime":return xa(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*Ca(i,e,t,n,r,o){let{name:s,isInternal:a}=e,{isArray:l,name:c}=e.type??{};if(a||l||c&&c!=="string"&&c!=="code")return;let p=r.streamedIndex[s]??0,u=p===0,d=(t<0?0:t)+p,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 f=u?g.trimStart():g;r.currField?.type?.name==="code"&&(f=f.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/,"")),f.length>0&&(yield{index:o,delta:{[s]:f}},r.streamedIndex[s]=p+g.length)}function*bs(i,e,t,n,r){for(let s of n.prevFields??[]){let{field:a,s:l,e:c}=s;yield*Ca(e,a,l,c,n,r)}if(n.prevFields=void 0,n.inAssumedField&&!(i.getOutputFields().filter(l=>!l.isInternal).length===1)||!n.currField||n.currField.isInternal)return;yield*Ca(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}n.streamedIndex[s]||(yield{index:r,delta:{[s]:l}},n.streamedIndex[s]=1)}}function qr(i,e){if(!e||e===""||/^(null|undefined)\s*$/i.test(e)){if(i.isOptional)return;throw ra(i)}let t;if(i.type?.name==="json")try{let n=Ra(e);return t=JSON.parse(n),t}catch(n){throw oa(i,n.message)}if(i.type?.isArray)try{try{t=JSON.parse(e)}catch{t=ba(e)}if(!Array.isArray(t))throw new Error("Expected an array")}catch(n){throw sa(i,n.message)}try{if(Array.isArray(t)){for(let[n,r]of t.entries())if(r!==void 0){let o=typeof r=="string"?r.trim():r;t[n]=Ta(i,o,!0)}}else t=Ta(i,e)}catch(n){throw ia(i,e,n.message)}if(!(typeof t=="string"&&t===""))return t}var Ra=i=>{let t=/```([A-Za-z]*)\n([\s\S]*?)\n```/g.exec(i);return t?t.length===3?t[2]:t.length===2?t[1]:i:i};async function Is(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});wa(r.field,t,s,n)}}async function Ts(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 c=a.process,p=await c(l,{sessionId:o,values:r,done:s});wa(t.currField,n,p,o)}}var wa=(i,e,t,n)=>{if(t===void 0||typeof t=="string"&&(t===""||/^(null|undefined)\s*$/i.test(t)))return;let r=JSON.stringify(t,(s,a)=>typeof a=="bigint"?Number(a):a,2),o=Yl(i,r);e.addRequest([{role:"user",content:[{type:"text",text:o}]}],n),e.addTag("processor",n)};function Yl(i,e){let t=i.type?.name==="code",n=i.title;return t?`Code in the field "${n}" was executed. The code execution produced the following output: ${e}`:`The field "${n}" was processed. The field contents were transformed into the following output: ${e}`}async function*Sa({res:i,usage:e,states:t,debug:n,...r}){let o=(r.ai.getFeatures().functionCot??!1)&&r.functions!==void 0&&r.functions.length>0,s,a=[],l=i.getReader();try{for(;;){let{done:c,value:p}=await l.read();if(c)break;let u=p;u.modelUsage&&(s=u.modelUsage);for(let d of u.results){if(Array.isArray(d.citations))for(let g of d.citations)g?.url&&a.push({url:g.url,title:g.title,description:g.description,license:g.license,publicationDate:g.publicationDate,snippet:g.snippet});if((!d.content||d.content==="")&&(!d.thought||d.thought==="")&&(!d.functionCalls||d.functionCalls.length===0))continue;let m=t.find(g=>g.index===d.index);if(!m)throw new Error(`No state found for result (index: ${d.index})`);yield*Xl({...r,result:d,skipEarlyFail:o,state:m,debug:n})}}}finally{l.releaseLock()}for(let c of t)yield*Zl({...r,state:c,debug:n});if(s){if(a.length){let c=Array.from(new Map(a.filter(p=>p.url).map(p=>[p.url,p])).values());s.citations=c}if(e.push(s),n&&r.logger){let c=structuredClone(s);delete c.citations,r.logger({name:"ChatResponseUsage",value:c}),s.citations&&s.citations.length>0&&r.logger({name:"ChatResponseCitations",value:s.citations})}}}async function*Xl({result:i,mem:e,sessionId:t,strictMode:n,skipEarlyFail:r,treatAllFieldsOptional:o,state:s,signature:a,streamingFieldProcessors:l,thoughtFieldName:c,streamingAsserts:p,asserts:u}){if(i.functionCalls&&i.functionCalls.length>0)Xn(s.functionCalls,i.functionCalls),e.updateResult({name:i.name,content:i.content,functionCalls:s.functionCalls,delta:i.functionCalls?.[0]?.function?.params,index:i.index},t);else if(i.content&&i.content.length>0){if(i.thought&&i.thought.length>0&&(yield{index:i.index,delta:{[c]:i.thought}}),s.content+=i.content,e.updateResult({name:i.name,content:s.content,delta:i.content,index:i.index},t),xs(a,s.values,s.xstate,s.content,{strictMode:n,skipEarlyFail:r,treatAllFieldsOptional:o}))return;p.length!==0&&await ls(p,s.xstate,s.content),l.length!==0&&await Ts(l,s.content,s.xstate,e,s.values,t),yield*bs(a,s.content,s.values,s.xstate,i.index),await _r(u,s.values)}else i.thought&&i.thought.length>0&&(s.values[c]=(s.values[c]??"")+i.thought,yield{index:i.index,delta:{[c]:i.thought}});if(i.finishReason==="length")throw new Error(`Max tokens reached before completion
195
- Content: ${s.content}`)}async function*Zl({state:i,signature:e,ai:t,model:n,functions:r,mem:o,sessionId:s,traceId:a,span:l,strictMode:c,excludeContentFromTrace:p,streamingAsserts:u,asserts:d,fieldProcessors:m,streamingFieldProcessors:g,functionResultFormatter:f,signatureToolCallingManager:h,logger:A,debug:x,stopFunctionNames:y}){let b=h?void 0:ms(t,i.functionCalls,i.values,n);if(b){if(!r)throw new Error("Functions are not defined");let S=await Ur({ai:t,functionList:r,functionCalls:b,mem:o,sessionId:s,traceId:a,span:l,index:i.index,excludeContentFromTrace:p,functionResultFormatter:f,logger:A,debug:x,stopFunctionNames:y});i.functionsExecuted=new Set([...i.functionsExecuted,...S]),i.functionCalls=[]}else{let S=h!==void 0;if(ys(e,i.values,i.xstate,i.content,{strictMode:c,treatAllFieldsOptional:S,deferRequiredCheckForStreaming:!0,forceFinalize:!0}),h){let I=await h.processResults(i.values);if(I&&I.length>0){if(!r)throw new Error("Functions are not defined");let T=await Ur({ai:t,functionList:r,functionCalls:I,mem:o,sessionId:s,traceId:a,span:l,index:i.index,excludeContentFromTrace:p,functionResultFormatter:f,logger:A,debug:x,stopFunctionNames:y});i.functionsExecuted=new Set([...i.functionsExecuted,...T]),o.updateResult({name:void 0,content:i.content,functionCalls:I.map(C=>({id:C.id,type:"function",function:{name:C.name,params:C.args}})),index:i.index},s);return}}await ls(u,i.xstate,i.content,!0),await _r(d,i.values),m.length&&await Is(m,i.values,o,s),g.length!==0&&await Ts(g,i.content,i.xstate,o,i.values,s,!0),yield*bs(e,i.content,i.values,i.xstate,i.index)}}async function*va({ai:i,res:e,mem:t,sessionId:n,traceId:r,functions:o,span:s,strictMode:a,states:l,usage:c,excludeContentFromTrace:p,asserts:u,fieldProcessors:d,thoughtFieldName:m,signature:g,functionResultFormatter:f,logger:h,debug:A,signatureToolCallingManager:x,stopFunctionNames:y,disableMemoryCleanup:b}){let S=e.results??[],I=x!==void 0;t.addResponse(S,n);let T=[];for(let P of S)if(Array.isArray(P?.citations))for(let w of P.citations)w?.url&&T.push({url:w.url,title:w.title,description:w.description,license:w.license,publicationDate:w.publicationDate,snippet:w.snippet});for(let P of S){let w=l[P.index];if(!w)throw new Error(`No state found for result (index: ${P.index})`);if(e.modelUsage){let O=Array.from(new Map(T.filter(v=>v.url).map(v=>[v.url,v])).values()),R={...e.modelUsage,...O.length?{citations:O}:{}};if(c.push(R),A&&h){let v=structuredClone(R);delete v.citations,h({name:"ChatResponseUsage",value:v}),R.citations&&R.citations.length>0&&h({name:"ChatResponseCitations",value:R.citations})}}if(x&&P.content){P.thought&&P.thought.length>0&&(w.values[m]=P.thought),As(g,w.values,P.content,{strictMode:a,treatAllFieldsOptional:I});let R=(await x.processResults(w.values))?.map(v=>({id:v.id,type:"function",function:{name:v.name,params:v.args}}));R&&R.length>0&&t.updateResult({name:P.name,content:P.content,functionCalls:R,index:P.index},n)}if(P.functionCalls?.length){let O=ms(i,P.functionCalls,w.values);if(O&&O.length>0){if(!o)throw new Error("Functions are not defined");let R;try{R=await Ur({ai:i,functionList:o,functionCalls:O,mem:t,sessionId:n,traceId:r,span:s,excludeContentFromTrace:p,index:P.index,functionResultFormatter:f,logger:h,debug:A,stopFunctionNames:y})}catch(v){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),v}w.functionsExecuted=new Set([...w.functionsExecuted,...R])}}else P.content&&(P.thought&&P.thought.length>0&&(w.values[m]=P.thought),As(g,w.values,P.content,{strictMode:a,treatAllFieldsOptional:I}));if(await _r(u,w.values),b||(t.removeByTag("correction",n),t.removeByTag("error",n)),d.length&&await Is(d,w.values,t,n),P.finishReason==="length")throw new Error(`Max tokens reached before completion
196
- Content: ${P.content}`)}let C=l.map(P=>P.values);for(let P of C)for(let w of g.getOutputFields())w.isInternal&&delete P[w.name];let N=g.getOutputFields(),U=C.map((P,w)=>{let O={};for(let R of N)R.isInternal||(O[R.name]=P[R.name]);return P[m]!==void 0&&(O[m]=P[m]),{index:w,delta:O}});for(let P of U)yield P}function Oa(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(u=>s.functionsExecuted.has(u)):!1;if(!r.chat[o])throw new Error(`No chat message found for result (index: ${o})`);let c=r.role==="function",p=r.tags?r.tags.some(u=>u==="processor"):!1;if(c&&e&&a||!(c||p))return!1}return!0}var Qt=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 L=class extends Error{constructor(t,n,r,o){super(t);this.position=n;this.context=r;this.suggestion=o;this.name="SignatureValidationError"}},Cs=class{input;position;currentFieldName=null;currentSection="description";constructor(e){if(this.input=e.trim(),this.position=0,!this.input)throw new L("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 L("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 L('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 L(`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 L)throw e;let t=e instanceof Error?e.message:"Unknown error";throw new L(t,this.position,this.getErrorContext())}}validateParsedSignature(e){let t=new Set;for(let r of e.inputs){if(t.has(r.name))throw new L(`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 L(`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 L(`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 L("Signature must have at least one input field",0,"",'Add an input field before "->". Example: "userInput:string -> ..."');if(e.outputs.length===0)throw new L("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(`
197
- `)}parseFieldList(e,t){let n=[];if(this.skipWhitespace(),this.position>=this.input.length)throw new L(`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 L?r:new L(`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 L(`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 L?r:new L(`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 L(`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 L(`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 L?o:new L(`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 L(`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(c=>c.trim()).filter(c=>c.length>0);if(l.length===0)throw new L(`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 L(`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 L(`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 L(`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 L(`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 L?s:new L(`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(se.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 L(`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 L(`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 L(`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 L(`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"],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 L(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 L("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 L(`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 L(`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 L(`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 L(`Expected "->" but found "${e}..."`,this.position,this.getErrorContext(),t)}}};function ka(i){return new Cs(i).parse()}var Fn=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},description:t.description,isOptional:t.isOptional||void 0,isInternal:t.isInternal||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},description:t.description,isOptional:t.isOptional||void 0,isInternal:t.isInternal||void 0};return n?this.outputFields.unshift(r):this.outputFields.push(r),this}description(e){return this.desc=e,this}build(){let e={description:this.desc,inputs:this.inputFields,outputs:this.outputFields};return new ce(e)}},Ae=class i{type;isArray;options;description;isOptional;isInternal;constructor(e){this.type=e.type,this.isArray=e.isArray,this.options=e.options,this.description=e.description,this.isOptional=e.isOptional,this.isInternal=e.isInternal}optional(){return new i({...this,isOptional:!0})}array(){return new i({...this,isArray:!0})}internal(){return new i({...this,isInternal:!0})}},ne=Object.assign(()=>new Fn,{string:i=>new Ae({type:"string",isArray:!1,description:i,isOptional:!1,isInternal:!1}),number:i=>new Ae({type:"number",isArray:!1,description:i,isOptional:!1,isInternal:!1}),boolean:i=>new Ae({type:"boolean",isArray:!1,description:i,isOptional:!1,isInternal:!1}),json:i=>new Ae({type:"json",isArray:!1,description:i,isOptional:!1,isInternal:!1}),datetime:i=>new Ae({type:"datetime",isArray:!1,description:i,isOptional:!1,isInternal:!1}),date:i=>new Ae({type:"date",isArray:!1,description:i,isOptional:!1,isInternal:!1}),class:(i,e)=>new Ae({type:"class",isArray:!1,options:i,description:e,isOptional:!1,isInternal:!1}),image:i=>new Ae({type:"image",isArray:!1,description:i,isOptional:!1,isInternal:!1}),audio:i=>new Ae({type:"audio",isArray:!1,description:i,isOptional:!1,isInternal:!1}),file:i=>new Ae({type:"file",isArray:!1,description:i,isOptional:!1,isInternal:!1}),url:i=>new Ae({type:"url",isArray:!1,description:i,isOptional:!1,isInternal:!1}),code:(i,e)=>new Ae({type:"code",isArray:!1,description:e||i,isOptional:!1,isInternal:!1})});function zr(i){return{type:{name:i.type,isArray:i.isArray,options:i.options?[...i.options]:void 0},description:i.description,isOptional:i.isOptional,isInternal:i.isInternal}}var $=class extends Error{constructor(t,n,r){super(t);this.fieldName=n;this.suggestion=r;this.name="AxSignatureValidationError"}},ce=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=ka(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 $(`Invalid Signature: ${n.message}`,void 0,r)}throw new $(`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);else if(typeof e=="object"&&e!==null){if(!("inputs"in e)||!("outputs"in e))throw new $("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 $("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 $?t:new $(`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 $("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 $("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 $("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 $("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);Ke(t,"input");for(let n of this.inputFields)if(n.name===t.name)throw new $(`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 $(`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 $?t:new $(`Failed to add input field "${e.name}": ${t instanceof Error?t.message:"Unknown error"}`,e.name)}};addOutputField=e=>{try{let t=this.parseField(e);Ke(t,"output");for(let n of this.outputFields)if(n.name===t.name)throw new $(`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 $(`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 $?t:new $(`Failed to add output field "${e.name}": ${t instanceof Error?t.message:"Unknown error"}`,e.name)}};setInputFields=e=>{if(!Array.isArray(e))throw new $("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 Ke(r,"input"),r});this.inputFields=t,this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof $?t:new $(`Failed to set input fields: ${t instanceof Error?t.message:"Unknown error"}`)}};setOutputFields=e=>{if(!Array.isArray(e))throw new $("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 Ke(r,"output"),r});this.outputFields=t,this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof $?t:new $(`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,...zr(t)}),n};prependInputField=(e,t)=>{let n=new i(this),r={name:e,...zr(t)},o=n.parseField(r);Ke(o,"input");for(let s of n.inputFields)if(s.name===o.name)throw new $(`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 $(`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,...zr(t)}),n};prependOutputField=(e,t)=>{let n=new i(this),r={name:e,...zr(t)},o=n.parseField(r);Ke(o,"output");for(let s of n.outputFields)if(s.name===o.name)throw new $(`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 $(`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};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)};toJSONSchema=()=>{let e={},t=[];for(let r of this.inputFields){let o=r.type?r.type.name:"string";r.type?.isArray?e[r.name]={description:r.description,type:"array",items:{type:o,description:r.description}}:e[r.name]={description:r.description,type:o},r.isOptional||t.push(r.name)}return{type:"object",properties:e,required:t}};updateHashLight=()=>{try{return this.getInputFields().forEach(e=>{Ke(e,"input")}),this.getOutputFields().forEach(e=>{Ke(e,"output")}),this.sigHash=Vn("sha256").update(JSON.stringify(this.inputFields)).update(JSON.stringify(this.outputFields)).digest("hex"),this.sigString=Ea(this.description,this.inputFields,this.outputFields),[this.sigHash,this.sigString]}catch(e){throw e instanceof $?e:new $(`Signature validation failed: ${e instanceof Error?e.message:"Unknown error"}`)}};updateHash=()=>{try{return this.getInputFields().forEach(e=>{Ke(e,"input")}),this.getOutputFields().forEach(e=>{Ke(e,"output")}),this.validateSignatureConsistency(),this.sigHash=Vn("sha256").update(this.description??"").update(JSON.stringify(this.inputFields)).update(JSON.stringify(this.outputFields)).digest("hex"),this.sigString=Ea(this.description,this.inputFields,this.outputFields),[this.sigHash,this.sigString]}catch(e){throw e instanceof $?e:new $(`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 $(`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 $(`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 $(`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 $("Signature must have at least one input field",void 0,'Add an input field. Example: "userInput:string -> ..."');if(this.outputFields.length===0)throw new $("Signature must have at least one output field",void 0,'Add an output field. Example: "... -> responseText:string"')}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})};function Ma(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 Ea(i,e,t){let n=i?`"${i}" `:"",r=e.map(Ma).join(", "),o=t.map(Ma).join(", ");return`${n}${r} -> ${o}`}function ec(i){let e=/^[a-z][a-zA-Z0-9]*$/,t=/^[a-z]+(_[a-z0-9]+)*$/;return e.test(i)||t.test(i)}function Ke(i,e){if(!i.name||i.name.length===0)throw new $("Field name cannot be blank",i.name,"Every field must have a descriptive name");if(!ec(i.name))throw new $(`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(se.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 $(`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 $(`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 $(`Field name '${i.name}' is too long (${i.name.length} characters)`,i.name,"Field names should be 50 characters or less");i.type&&tc(i,e)}function tc(i,e){if(!i.type)return;let{type:t}=i;if((t.name==="image"||t.name==="audio"||t.name==="file"||t.name==="url")&&e==="output")throw new $(`${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 $("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 $("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 $("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 $(`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 $("Duplicate class options found",i.name,"Each class option must be unique (case-insensitive)")}if(t.name==="code"&&t.isArray)throw new $("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 $("Internal marker (!) is not allowed on input fields",i.name,"Internal markers are only allowed on output fields")}var it=class{signature;sigHash;examples;examplesOptions;demos;trace;usage=[];traceLabel;key;children;constructor(e,t){this.signature=new ce(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 Qt,this.key={id:this.signature.hash()}}getSignature(){return new ce(this.signature)}setSignature(e){this.signature=new ce(e),e&&this.signature.validate(),this.updateSignatureHash()}setDescription(e){this.signature.setDescription(e),this.updateSignatureHash()}updateSignatureHash(){this.sigHash=this.signature.hash(),this.key={id:this.signature.hash()}}register(e){this.key&&e.setParentId(this.key.id),this.children.register(e)}setId(e){this.key={id:e,custom:!0};for(let t of Array.from(this.children))t?.setParentId(e)}setParentId(e){this.key.custom||(this.key.id=[e,this.key.id].join("/"))}setExamples(e,t){if(this._setExamples(e,t),"programId"in e)for(let n of Array.from(this.children))n?.setExamples(e,t)}_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 c=s[l.name];c!==void 0&&(Br(l,c),a[l.name]=c)}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 Pn(e)}resetUsage(){this.usage=[];for(let e of Array.from(this.children))e?.resetUsage()}setDemos(e){let t=Array.from(this.children).length>0,n=e.some(r=>r.programId===this.key.id);if(t&&!n)throw new Error(`Program with id '${this.key.id}' has children but no matching programId found in demos`);this.demos=e.filter(r=>r.programId===this.key.id).map(r=>r.traces).flat();for(let r of Array.from(this.children))r?.setDemos(e)}applyOptimization(e){e.applyTo(this);for(let t of Array.from(this.children))t&&"applyOptimization"in t&&typeof t.applyOptimization=="function"&&t.applyOptimization(e)}};var nc=`
201
+ Content: ${s.content}`)}async function*Zl({state:i,signature:e,ai:t,model:n,functions:r,mem:o,sessionId:s,traceId:a,span:l,strictMode:c,excludeContentFromTrace:p,streamingAsserts:u,asserts:d,fieldProcessors:m,streamingFieldProcessors:g,functionResultFormatter:f,signatureToolCallingManager:h,logger:A,debug:x,stopFunctionNames:y}){let b=h?void 0:ms(t,i.functionCalls,i.values,n);if(b){if(!r)throw new Error("Functions are not defined");let S=await Ur({ai:t,functionList:r,functionCalls:b,mem:o,sessionId:s,traceId:a,span:l,index:i.index,excludeContentFromTrace:p,functionResultFormatter:f,logger:A,debug:x,stopFunctionNames:y});i.functionsExecuted=new Set([...i.functionsExecuted,...S]),i.functionCalls=[]}else{let S=h!==void 0;if(ys(e,i.values,i.xstate,i.content,{strictMode:c,treatAllFieldsOptional:S,deferRequiredCheckForStreaming:!0,forceFinalize:!0}),h){let I=await h.processResults(i.values);if(I&&I.length>0){if(!r)throw new Error("Functions are not defined");let T=await Ur({ai:t,functionList:r,functionCalls:I,mem:o,sessionId:s,traceId:a,span:l,index:i.index,excludeContentFromTrace:p,functionResultFormatter:f,logger:A,debug:x,stopFunctionNames:y});i.functionsExecuted=new Set([...i.functionsExecuted,...T]),o.updateResult({name:void 0,content:i.content,functionCalls:I.map(C=>({id:C.id,type:"function",function:{name:C.name,params:C.args}})),index:i.index},s);return}}await ls(u,i.xstate,i.content,!0),await _r(d,i.values),m.length&&await Is(m,i.values,o,s),g.length!==0&&await Ts(g,i.content,i.xstate,o,i.values,s,!0),yield*bs(e,i.content,i.values,i.xstate,i.index)}}async function*va({ai:i,res:e,mem:t,sessionId:n,traceId:r,functions:o,span:s,strictMode:a,states:l,usage:c,excludeContentFromTrace:p,asserts:u,fieldProcessors:d,thoughtFieldName:m,signature:g,functionResultFormatter:f,logger:h,debug:A,signatureToolCallingManager:x,stopFunctionNames:y,disableMemoryCleanup:b}){let S=e.results??[],I=x!==void 0;t.addResponse(S,n);let T=[];for(let k of S)if(Array.isArray(k?.citations))for(let w of k.citations)w?.url&&T.push({url:w.url,title:w.title,description:w.description,license:w.license,publicationDate:w.publicationDate,snippet:w.snippet});for(let k of S){let w=l[k.index];if(!w)throw new Error(`No state found for result (index: ${k.index})`);if(e.modelUsage){let E=Array.from(new Map(T.filter(M=>M.url).map(M=>[M.url,M])).values()),R={...e.modelUsage,...E.length?{citations:E}:{}};if(c.push(R),A&&h){let M=structuredClone(R);delete M.citations,h({name:"ChatResponseUsage",value:M}),R.citations&&R.citations.length>0&&h({name:"ChatResponseCitations",value:R.citations})}}if(x&&k.content){k.thought&&k.thought.length>0&&(w.values[m]=k.thought),As(g,w.values,k.content,{strictMode:a,treatAllFieldsOptional:I});let R=(await x.processResults(w.values))?.map(M=>({id:M.id,type:"function",function:{name:M.name,params:M.args}}));R&&R.length>0&&t.updateResult({name:k.name,content:k.content,functionCalls:R,index:k.index},n)}if(k.functionCalls?.length){let E=ms(i,k.functionCalls,w.values);if(E&&E.length>0){if(!o)throw new Error("Functions are not defined");let R;try{R=await Ur({ai:i,functionList:o,functionCalls:E,mem:t,sessionId:n,traceId:r,span:s,excludeContentFromTrace:p,index:k.index,functionResultFormatter:f,logger:h,debug:A,stopFunctionNames:y})}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}w.functionsExecuted=new Set([...w.functionsExecuted,...R])}}else k.content&&(k.thought&&k.thought.length>0&&(w.values[m]=k.thought),As(g,w.values,k.content,{strictMode:a,treatAllFieldsOptional:I}));if(await _r(u,w.values),b||(t.removeByTag("correction",n),t.removeByTag("error",n)),d.length&&await Is(d,w.values,t,n),k.finishReason==="length")throw new Error(`Max tokens reached before completion
202
+ Content: ${k.content}`)}let C=l.map(k=>k.values);for(let k of C)for(let w of g.getOutputFields())w.isInternal&&delete k[w.name];let N=g.getOutputFields(),U=C.map((k,w)=>{let E={};for(let R of N)R.isInternal||(E[R.name]=k[R.name]);return k[m]!==void 0&&(E[m]=k[m]),{index:w,delta:E}});for(let k of U)yield k}function Oa(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(u=>s.functionsExecuted.has(u)):!1;if(!r.chat[o])throw new Error(`No chat message found for result (index: ${o})`);let c=r.role==="function",p=r.tags?r.tags.some(u=>u==="processor"):!1;if(c&&e&&a||!(c||p))return!1}return!0}var Qt=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 L=class extends Error{constructor(t,n,r,o){super(t);this.position=n;this.context=r;this.suggestion=o;this.name="SignatureValidationError"}},Cs=class{input;position;currentFieldName=null;currentSection="description";constructor(e){if(this.input=e.trim(),this.position=0,!this.input)throw new L("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 L("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 L('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 L(`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 L)throw e;let t=e instanceof Error?e.message:"Unknown error";throw new L(t,this.position,this.getErrorContext())}}validateParsedSignature(e){let t=new Set;for(let r of e.inputs){if(t.has(r.name))throw new L(`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 L(`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 L(`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 L("Signature must have at least one input field",0,"",'Add an input field before "->". Example: "userInput:string -> ..."');if(e.outputs.length===0)throw new L("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(`
203
+ `)}parseFieldList(e,t){let n=[];if(this.skipWhitespace(),this.position>=this.input.length)throw new L(`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 L?r:new L(`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 L(`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 L?r:new L(`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 L(`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 L(`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 L?o:new L(`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 L(`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(c=>c.trim()).filter(c=>c.length>0);if(l.length===0)throw new L(`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 L(`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 L(`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 L(`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 L(`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 L?s:new L(`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(se.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 L(`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 L(`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 L(`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 L(`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"],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 L(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 L("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 L(`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 L(`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 L(`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 L(`Expected "->" but found "${e}..."`,this.position,this.getErrorContext(),t)}}};function ka(i){return new Cs(i).parse()}var Fn=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},description:t.description,isOptional:t.isOptional||void 0,isInternal:t.isInternal||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},description:t.description,isOptional:t.isOptional||void 0,isInternal:t.isInternal||void 0};return n?this.outputFields.unshift(r):this.outputFields.push(r),this}description(e){return this.desc=e,this}build(){let e={description:this.desc,inputs:this.inputFields,outputs:this.outputFields};return new ce(e)}},Ae=class i{type;isArray;options;description;isOptional;isInternal;constructor(e){this.type=e.type,this.isArray=e.isArray,this.options=e.options,this.description=e.description,this.isOptional=e.isOptional,this.isInternal=e.isInternal}optional(){return new i({...this,isOptional:!0})}array(){return new i({...this,isArray:!0})}internal(){return new i({...this,isInternal:!0})}},ne=Object.assign(()=>new Fn,{string:i=>new Ae({type:"string",isArray:!1,description:i,isOptional:!1,isInternal:!1}),number:i=>new Ae({type:"number",isArray:!1,description:i,isOptional:!1,isInternal:!1}),boolean:i=>new Ae({type:"boolean",isArray:!1,description:i,isOptional:!1,isInternal:!1}),json:i=>new Ae({type:"json",isArray:!1,description:i,isOptional:!1,isInternal:!1}),datetime:i=>new Ae({type:"datetime",isArray:!1,description:i,isOptional:!1,isInternal:!1}),date:i=>new Ae({type:"date",isArray:!1,description:i,isOptional:!1,isInternal:!1}),class:(i,e)=>new Ae({type:"class",isArray:!1,options:i,description:e,isOptional:!1,isInternal:!1}),image:i=>new Ae({type:"image",isArray:!1,description:i,isOptional:!1,isInternal:!1}),audio:i=>new Ae({type:"audio",isArray:!1,description:i,isOptional:!1,isInternal:!1}),file:i=>new Ae({type:"file",isArray:!1,description:i,isOptional:!1,isInternal:!1}),url:i=>new Ae({type:"url",isArray:!1,description:i,isOptional:!1,isInternal:!1}),code:(i,e)=>new Ae({type:"code",isArray:!1,description:e||i,isOptional:!1,isInternal:!1})});function zr(i){return{type:{name:i.type,isArray:i.isArray,options:i.options?[...i.options]:void 0},description:i.description,isOptional:i.isOptional,isInternal:i.isInternal}}var G=class extends Error{constructor(t,n,r){super(t);this.fieldName=n;this.suggestion=r;this.name="AxSignatureValidationError"}},ce=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=ka(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 G(`Invalid Signature: ${n.message}`,void 0,r)}throw new G(`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);else if(typeof e=="object"&&e!==null){if(!("inputs"in e)||!("outputs"in e))throw new G("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 G("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 G?t:new G(`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 G("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 G("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 G("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 G("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);Ke(t,"input");for(let n of this.inputFields)if(n.name===t.name)throw new G(`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 G(`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 G?t:new G(`Failed to add input field "${e.name}": ${t instanceof Error?t.message:"Unknown error"}`,e.name)}};addOutputField=e=>{try{let t=this.parseField(e);Ke(t,"output");for(let n of this.outputFields)if(n.name===t.name)throw new G(`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 G(`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 G?t:new G(`Failed to add output field "${e.name}": ${t instanceof Error?t.message:"Unknown error"}`,e.name)}};setInputFields=e=>{if(!Array.isArray(e))throw new G("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 Ke(r,"input"),r});this.inputFields=t,this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof G?t:new G(`Failed to set input fields: ${t instanceof Error?t.message:"Unknown error"}`)}};setOutputFields=e=>{if(!Array.isArray(e))throw new G("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 Ke(r,"output"),r});this.outputFields=t,this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof G?t:new G(`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,...zr(t)}),n};prependInputField=(e,t)=>{let n=new i(this),r={name:e,...zr(t)},o=n.parseField(r);Ke(o,"input");for(let s of n.inputFields)if(s.name===o.name)throw new G(`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 G(`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,...zr(t)}),n};prependOutputField=(e,t)=>{let n=new i(this),r={name:e,...zr(t)},o=n.parseField(r);Ke(o,"output");for(let s of n.outputFields)if(s.name===o.name)throw new G(`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 G(`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};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)};toJSONSchema=()=>{let e={},t=[];for(let r of this.inputFields){let o=r.type?r.type.name:"string";r.type?.isArray?e[r.name]={description:r.description,type:"array",items:{type:o,description:r.description}}:e[r.name]={description:r.description,type:o},r.isOptional||t.push(r.name)}return{type:"object",properties:e,required:t}};updateHashLight=()=>{try{return this.getInputFields().forEach(e=>{Ke(e,"input")}),this.getOutputFields().forEach(e=>{Ke(e,"output")}),this.sigHash=Vn("sha256").update(JSON.stringify(this.inputFields)).update(JSON.stringify(this.outputFields)).digest("hex"),this.sigString=Ea(this.description,this.inputFields,this.outputFields),[this.sigHash,this.sigString]}catch(e){throw e instanceof G?e:new G(`Signature validation failed: ${e instanceof Error?e.message:"Unknown error"}`)}};updateHash=()=>{try{return this.getInputFields().forEach(e=>{Ke(e,"input")}),this.getOutputFields().forEach(e=>{Ke(e,"output")}),this.validateSignatureConsistency(),this.sigHash=Vn("sha256").update(this.description??"").update(JSON.stringify(this.inputFields)).update(JSON.stringify(this.outputFields)).digest("hex"),this.sigString=Ea(this.description,this.inputFields,this.outputFields),[this.sigHash,this.sigString]}catch(e){throw e instanceof G?e:new G(`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 G(`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 G(`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 G(`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 G("Signature must have at least one input field",void 0,'Add an input field. Example: "userInput:string -> ..."');if(this.outputFields.length===0)throw new G("Signature must have at least one output field",void 0,'Add an output field. Example: "... -> responseText:string"')}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})};function Ma(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 Ea(i,e,t){let n=i?`"${i}" `:"",r=e.map(Ma).join(", "),o=t.map(Ma).join(", ");return`${n}${r} -> ${o}`}function ec(i){let e=/^[a-z][a-zA-Z0-9]*$/,t=/^[a-z]+(_[a-z0-9]+)*$/;return e.test(i)||t.test(i)}function Ke(i,e){if(!i.name||i.name.length===0)throw new G("Field name cannot be blank",i.name,"Every field must have a descriptive name");if(!ec(i.name))throw new G(`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(se.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 G(`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 G(`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 G(`Field name '${i.name}' is too long (${i.name.length} characters)`,i.name,"Field names should be 50 characters or less");i.type&&tc(i,e)}function tc(i,e){if(!i.type)return;let{type:t}=i;if((t.name==="image"||t.name==="audio"||t.name==="file"||t.name==="url")&&e==="output")throw new G(`${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 G("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 G("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 G("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 G(`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 G("Duplicate class options found",i.name,"Each class option must be unique (case-insensitive)")}if(t.name==="code"&&t.isArray)throw new G("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 G("Internal marker (!) is not allowed on input fields",i.name,"Internal markers are only allowed on output fields")}var it=class{signature;sigHash;examples;examplesOptions;demos;trace;usage=[];traceLabel;key;children;constructor(e,t){this.signature=new ce(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 Qt,this.key={id:this.signature.hash()}}getSignature(){return new ce(this.signature)}setSignature(e){this.signature=new ce(e),e&&this.signature.validate(),this.updateSignatureHash()}setDescription(e){this.signature.setDescription(e),this.updateSignatureHash()}updateSignatureHash(){this.sigHash=this.signature.hash(),this.key={id:this.signature.hash()}}register(e){this.key&&e.setParentId(this.key.id),this.children.register(e)}setId(e){this.key={id:e,custom:!0};for(let t of Array.from(this.children))t?.setParentId(e)}setParentId(e){this.key.custom||(this.key.id=[e,this.key.id].join("/"))}setExamples(e,t){if(this._setExamples(e,t),"programId"in e)for(let n of Array.from(this.children))n?.setExamples(e,t)}_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 c=s[l.name];c!==void 0&&(Br(l,c),a[l.name]=c)}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 Pn(e)}resetUsage(){this.usage=[];for(let e of Array.from(this.children))e?.resetUsage()}setDemos(e){let t=Array.from(this.children).length>0,n=e.some(r=>r.programId===this.key.id);if(t&&!n)throw new Error(`Program with id '${this.key.id}' has children but no matching programId found in demos`);this.demos=e.filter(r=>r.programId===this.key.id).map(r=>r.traces).flat();for(let r of Array.from(this.children))r?.setDemos(e)}applyOptimization(e){e.applyTo(this);for(let t of Array.from(this.children))t&&"applyOptimization"in t&&typeof t.applyOptimization=="function"&&t.applyOptimization(e)}};var nc=`
198
204
  ## Function Call Instructions
199
205
  - Complete the task, using the functions defined earlier in this prompt.
200
206
  - Output fields should only be generated after all functions have been called.
@@ -226,7 +232,7 @@ ${p}`),l&&l.length>0&&r.push(nc.trim()),r.push(rc.trim());let u=this.sig.getDesc
226
232
  `),n.push(t.map(r=>`- ${r}`).join(`
227
233
  `))):n.push(t),[{type:"text",text:n.join("")}]}},Pa=i=>i.map(e=>`\`${e.title}\``).join(", "),oc=i=>i.map(t=>{let n=t.title,r=t.type?.name?_a(t.type):"string",o=t.isOptional?`This optional ${r} field may be omitted`:`A ${r} field`,s=t.description?` ${jr(t.description)}`:"";return`${n}: (${o})${s}`.trim()}).join(`
228
234
  `),sc=i=>i.map(t=>{let n=t.title,r=t.type?.name?_a(t.type):"string",o=t.isOptional?`Only include this ${r} field if its value is available`:`This ${r} field must be included`,s="";return t.description&&t.description.length>0&&(s=` ${t.type?.name==="class"?t.description:jr(t.description)}`),t.type?.options&&t.type.options.length>0&&(s.length>0&&(s+=". "),s+=`Allowed values: ${t.type.options.join(", ")}`),`${n}: (${o})${s}`.trim()}).join(`
229
- `),ic=(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?ya(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)},_a=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)";default:return"string"}})();return i?.isArray?`json array of ${e} items`:e};function Fa(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 ac=(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 jr(i){let e=i.trim();return e.length>0?`${e.charAt(0).toUpperCase()}${e.slice(1)}${e.endsWith(".")?"":"."}`:""}function lc(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 cc(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(c=>"functionId"in c&&c.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 Hr(i,e,t,n){if(!e?.resultPicker||i.length<=1)return 0;let r=e.resultPicker;if((t?lc(t,n):!1)&&t){let l=cc(t,n),c=await r({type:"function",results:l});if(c<0||c>=l.length)throw new Error(`Result picker returned invalid index: ${c}. Must be between 0 and ${l.length-1}`);return c}let s=i.map((l,c)=>({index:c,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 Na(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 Hr(r,t,i,e)}var Kr=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}=pc(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 c=this.tools.get(this.normalizeToolName(a));if(c){l!=null&&typeof l=="object"&&o.set(c.name,l);continue}r[a]=l}for(let[a,l]of Object.entries(e))for(let[c,p]of this.tools.entries()){let u=s.get(c);if(!u)continue;let d=u.get(a);if(!d)continue;let m=o.get(p.name)??{};this.setNested(m,d,l),o.set(p.name,m)}for(let[a,l]of this.tools.entries()){let c=o.get(l.name);if(!(!c||Object.keys(c).length===0)){if(l.parameters&&l.parameters.type==="object"){let u=(l.parameters.required||[]).filter(d=>c[d]===void 0);if(u.length>0)throw new Re(`Missing required arguments for tool '${l.name}': ${u.join(", ")}`)}n.push({id:l.name,name:l.name,args:JSON.stringify(c)})}}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 c=`${e.name}.${l.join(".")}`,p=this.sanitizeFieldName(c);t.set(p,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 pc(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[c,p]of Object.entries(s)){let u=a?`${a}.${c}`:c,d=`${i.name}.${u}`;if(p.type==="object"&&p.properties)o(p.properties,u,p.required||[]);else{let m=uc(p);e.push({name:mc(d),title:dc(i.name,u),type:m,description:p.description||`${c} parameter for ${i.name}`,isOptional:!0}),t.set(d,e[e.length-1])}}};return o(n,"",r),{fields:e,paramFieldMap:t}}function uc(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 dc(i,e){return`${i} ${e.replace(/\./g," ")}`}function mc(i){return i.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}function Ga(i,e,t){let n=new ce(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(c=>c.name===a.name)||n.addOutputField(a);else{let a=Rs(o.name),l=$a(o.parameters);n.getOutputFields().some(p=>p.name===a)||n.addOutputField({name:a,title:Da(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}=gc(o);r.set(o.name,a);for(let l of s)n.getOutputFields().some(p=>p.name===l.name)||n.addOutputField(l)}else{let s=Rs(o.name),a=$a(o.parameters);n.getOutputFields().some(c=>c.name===s)||n.addOutputField({name:s,title:Da(o.name),type:a,description:o.description||`Parameters for ${o.name}`,isOptional:!0})}return{signature:n,toolParamFieldMap:r}}}function gc(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[c,p]of Object.entries(s)){let u=a?`${a}.${c}`:c,d=`${i.name}.${u}`;if(p.type==="object"&&p.properties)o(p.properties,u,p.required||[]);else{let m=hc(p),g={name:Rs(d),title:fc(i.name,u),type:m,description:p.description||`${c} parameter for ${i.name}`,isOptional:!0};e.push(g),t.set(d,g)}}};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 fc(i,e){return`${i} ${e.replace(/\./g," ")}`}function Rs(i){return i.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}function Da(i){return i.replace(/([A-Z])/g," $1").replace(/^./,e=>e.toUpperCase()).trim()}function $a(i){return!i||!i.properties||Object.keys(i.properties).length===0?{name:"string",isArray:!1}:{name:"json",isArray:!1}}var _n=class{tools;router;injectedToolFieldNames=new Set;constructor(e){this.tools=e,this.router=new Kr(e)}processSignature(e){let{signature:t}=Ga(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 we=class extends it{promptTemplate;asserts;streamingAsserts;options;functions;fieldProcessors=[];streamingFieldProcessors=[];excludeContentFromTrace=!1;thoughtFieldName;signatureToolCallingManager;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,cacheSystemPrompt:t?.cacheSystemPrompt};this.promptTemplate=new(t?.promptTemplate??ft)(this.signature,n),this.asserts=this.options?.asserts??[],this.streamingAsserts=this.options?.streamingAsserts??[],this.excludeContentFromTrace=t?.excludeContentFromTrace??!1,this.functions=t?.functions?Lr(t.functions):[],this.usage=[]}getSignatureName(){return this.signature.getDescription()||"unknown_signature"}getMetricsInstruments(){return ps()}updateMeter(e){ps(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} is 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:c,rateLimiter:p,stream:u,thinkingTokenBudget:d,showThoughts:m}=n??{},g=await Na(t,l,{resultPicker:n?.resultPicker}),f=t?.history(g,l)??[];if(f.length===0)throw new Error("No chat prompt found");let h={...n?.modelConfig,...n?.sampleCount?{n:n.sampleCount}:{},...n?.sampleCount&&n?.modelConfig?.temperature===1?{temperature:.8}:{}},A=this.isDebug(e,n),x=a===0,y=this.getLogger(e,n);return o=this.signatureToolCallingManager?[]:o,await e.chat({chatPrompt:f,functions:o,functionCall:s,modelConfig:h,model:c},{sessionId:l,rateLimiter:p,stream:u,debug:A,debugHideSystemPrompt:!x,thinkingTokenBudget:d,showThoughts:m,traceContext:r,abortSignal:n?.abortSignal,stepIndex:a,logger:y,functionCallMode:n?.functionCallMode??this.options?.functionCallMode??"auto"})}async*forwardCore({ai:e,mem:t,options:n,stepIndex:r,span:o,traceContext:s,states:a,stopFunctionNames:l}){let{sessionId:c,functions:p}=n??{},u=n?.functionResultFormatter??this.options?.functionResultFormatter,d=n?.functionCall??this.options?.functionCall,m=this.signatureToolCallingManager,g=n?.strictMode??!1,f=n.model,h=this.usage,A=r===0,x=this.isDebug(e,n),y=this.getLogger(e,n),{functions:b,functionCall:S}=ma(p,d,A,n),I=await this.forwardSendRequest({ai:e,mem:t,options:n,traceContext:s,functions:b,functionCall:S,stepIndex:r});I instanceof ReadableStream?yield*Sa({ai:e,model:f,res:I,mem:t,sessionId:c,traceId:o?o.spanContext?.().traceId:void 0,functions:b,strictMode:g,span:o,states:a,usage:h,asserts:this.asserts,streamingAsserts:this.streamingAsserts,fieldProcessors:this.fieldProcessors,streamingFieldProcessors:this.streamingFieldProcessors,thoughtFieldName:this.thoughtFieldName,excludeContentFromTrace:this.excludeContentFromTrace,signature:this.signature,logger:y,debug:x,functionResultFormatter:u,signatureToolCallingManager:m,stopFunctionNames:l,disableMemoryCleanup:n.disableMemoryCleanup}):yield*va({ai:e,model:f,res:I,mem:t,sessionId:c,traceId:o?o.spanContext?.().traceId:void 0,functions:b,span:o,strictMode:g,states:a,usage:h,asserts:this.asserts,fieldProcessors:this.fieldProcessors,thoughtFieldName:this.thoughtFieldName,excludeContentFromTrace:this.excludeContentFromTrace,signature:this.signature,logger:y,debug:x,functionResultFormatter:u,signatureToolCallingManager:m,stopFunctionNames:l,disableMemoryCleanup:n.disableMemoryCleanup})}async*_forward2(e,t,n,r,o,s){let a=r?.stopFunction??this.options?.stopFunction,l=Array.isArray(a)?a.map(U=>U.toLowerCase()):a?[a.toLowerCase()]:void 0,c=r.maxRetries??this.options?.maxRetries??10,p=r.maxSteps??this.options?.maxSteps??10,u=r.mem??this.options?.mem??new Vt,d=[...this.functions,...r.functions?Lr(r.functions):[]],m=d&&d.length>0,g=r.functionCallMode??this.options?.functionCallMode??"auto",f=r.cacheSystemPrompt??this.options?.cacheSystemPrompt;m&&g==="prompt"&&(this.signatureToolCallingManager=new _n(d)),m&&g==="auto"&&!e.getFeatures(r.model).functions&&(this.signatureToolCallingManager=new _n(d));let h,A,x=this.options?.promptTemplate??ft;this.signatureToolCallingManager&&(this.signature=this.signatureToolCallingManager.processSignature(this.signature),this.setSignature(this.signature));let y={functions:this.signatureToolCallingManager?[]:d,thoughtFieldName:this.thoughtFieldName,cacheSystemPrompt:f};this.promptTemplate=new x(this.signature,y);let b,S=performance.now();Array.isArray(t)?(xi(t),b=this.promptTemplate.render(t,{examples:this.examples,demos:this.demos})):b=this.promptTemplate.render(t,{examples:this.examples,demos:this.demos});let I=performance.now()-S,T=this.getMetricsInstruments();T&&$r(T,"prompt_render",I,this.getSignatureName());let C=performance.now();u.addRequest(b,r.sessionId);let N=performance.now()-C;T&&$r(T,"memory_update",N,this.getSignatureName());e:for(let U=0;U<p;U++){for(let w=0;w<c;w++)try{let O=this.forwardCore({options:r,ai:e,mem:u,stepIndex:U,span:o,traceContext:s,states:n,stopFunctionNames:l});for await(let k of O)k!==void 0&&(yield{version:w,index:k.index,delta:k.delta});if(Oa(u,l,n,r?.sessionId)){let k=this.getMetricsInstruments();k&&Dr(k,U+1,p,this.getSignatureName());continue e}r?.disableMemoryCleanup||(u.removeByTag("invalid-assistant",r.sessionId),u.removeByTag("correction",r.sessionId),u.removeByTag("error",r.sessionId));let v=this.getMetricsInstruments();if(v){Dr(v,U+1,p,this.getSignatureName());let k=new Set;n.forEach(G=>{G.functionsExecuted.forEach(X=>k.add(X))}),k.size>0&&Yi(v,!0,k.size,!0,!1,this.getSignatureName()),Xi(v,this.fieldProcessors.length,this.streamingFieldProcessors.length,this.getSignatureName())}return}catch(O){A=O;let R,v=this.isDebug(e,r),k=this.getLogger(e,r),G=this.getMetricsInstruments(),X=this.getSignatureName(),J={error:O,errCount:w,logger:k,metricsInstruments:G,signatureName:X,span:o,debug:v};if(o?.recordException(O),O instanceof Re)R=ca(J),h=O;else if(O instanceof He)R=pa(J),h=O;else if(O instanceof ae)ua(J);else{if(O instanceof ht)throw O;if(!(O instanceof Ge))throw ws(O,e,this.signature)}R&&(u.addTag("error",r.sessionId),u.addRequest([{role:"user",content:this.promptTemplate.renderExtraFields(R)}],r.sessionId),u.addTag("correction",r.sessionId))}let P=this.getMetricsInstruments();throw P&&ds(P,c,!1,c,this.getSignatureName()),ws(new Error(`Unable to fix validation error: ${(h??A)?.message??(h??A)?.toString()??"unknown error"}`),e,this.signature)}throw T&&Dr(T,p,p,this.getSignatureName()),ws(new Error(`Max steps reached: ${p}`),e,this.signature)}async*_forward1(e,t,n){let r=performance.now(),o=this.createStates(n.sampleCount??1),s=performance.now()-r,a=this.getMetricsInstruments();a&&$r(a,"state_creation",s,this.getSignatureName());let l=n?.tracer??this.options?.tracer??e.getOptions().tracer,c=this.functions;if(n?.functions&&(c=Lr(n.functions,this.functions)),!l){yield*this._forward2(e,t,o,{...n,functions:c});return}let p=c?.map(A=>A.name).join(","),u={signature:JSON.stringify(this.signature.toJSON(),null,2),...this.examples?{examples:JSON.stringify(this.examples,null,2)}:{},...p?{provided_functions:p}:{},...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}:{}},d=this.traceLabel&&n.traceLabel?`${this.traceLabel} > ${n.traceLabel}`:n.traceLabel??this.traceLabel,m=d?`AxGen > ${d}`:"AxGen",g=l.startSpan(m,{kind:Yt.SpanKind.SERVER,attributes:u}),f=Yt.context.active(),h=Yt.trace.setSpan(f,g);try{if(this.excludeContentFromTrace||g.addEvent("input",{content:JSON.stringify(t,null,2)}),yield*this._forward2(e,t,o,{...n,functions:c},g,h),!this.excludeContentFromTrace){let A=o.map(y=>y.values),x=A.length===1?A[0]:A;g.addEvent("output",{content:JSON.stringify(x,null,2)})}}finally{g.end()}}async forward(e,t,n){let r=performance.now(),o=this.getSignatureName(),s=n?.stream??!1,a=!1,l=0,c=!1;try{let p=this.getMetricsInstruments();p&&ta(p,this.signature.getInputFields().length,this.signature.getOutputFields().length,this.examples?.length??0,this.demos?.length??0,o);let u=this._forward1(e,t,n??{}),d=[],m=0,g=0;for await(let S of u)S.version!==m&&(d=[]),m=S.version,d=hs(d,S),g++;l=m;let f=performance.now();c=!!n?.resultPicker;let h=await Hr(d,{resultPicker:n?.resultPicker},n?.mem,n?.sessionId),A=performance.now()-f,y=d[h]?.delta??{},b=Array.isArray(t)?{}:t??{};if(this.trace={...b,...y},c&&this.isDebug(e,n)){let S=this.getLogger(e,n);ei(d.length,h,A,S)}return a=!0,p&&(ea(p,d.length,c,c?A:void 0,o),Zi(p,s,g,void 0,o)),y}catch(p){throw a=!1,p}finally{let p=performance.now()-r,u=this.getMetricsInstruments();u&&(Ji(u,p,a,o,e.getName(),n?.model?String(n.model):void 0),l>0&&ds(u,l,a,n?.maxRetries??10,o))}}async*streamingForward(e,t,n){if(!n?.resultPicker){yield*this._forward1(e,t,{...n,stream:!0});return}let r=this._forward1(e,t,{...n,stream:!0}),o=[],s=0;for await(let c of r)c.version!==s&&(o=[]),s=c.version,o=hs(o,c);let a=await Hr(o,{resultPicker:n?.resultPicker},n?.mem,n?.sessionId),l=o[a];l&&(yield{version:s,index:a,delta:l.delta})}setExamples(e,t){super.setExamples(e,t)}isDebug(e,t){return t?.debug??this.options?.debug??e.getOptions().debug??!1}getLogger(e,t){return t?.logger??this.options?.logger??se.logger??e.getLogger()}},Nn=class extends Error{details;constructor(e,t,n){super(e),this.name="AxGenerateError",this.details=t,n?.cause&&(this.cause=n.cause)}};function ws(i,e,t){let n=i instanceof Error?i:new Error(String(i)),r=e.getLastUsedChatModel(),o=e.getLastUsedModelConfig(),s={model:r,maxTokens:o?.maxTokens,streaming:o?.stream??!1,signature:{input:t.getInputFields(),output:t.getOutputFields(),description:t.getDescription()}};return new Nn("Generate failed",s,{cause:n})}var Ac=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]},yc=i=>{let e=new Set,t=[];for(let n of i)e.has(n)||(e.add(n),t.push(n));return t},bc=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}},Ic=i=>{let e=i.match(/^(\d+)[.,\s]+(.*)$/);return e&&e[2]!==void 0?e[2].trim():i},Tc=(i,e)=>{let t=[];for(let n=0;n<i.length;n+=e)t.push(i.slice(n,n+e));return t},Wr={trimNonAlphaNum:Ac,splitIntoTwo:xc,dedup:yc,extractIdAndText:bc,extractIndexPrefixedText:Ic,batchArray:Tc};var Vr=class extends we{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."
235
+ `),ic=(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?ya(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)},_a=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)";default:return"string"}})();return i?.isArray?`json array of ${e} items`:e};function Fa(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 ac=(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 jr(i){let e=i.trim();return e.length>0?`${e.charAt(0).toUpperCase()}${e.slice(1)}${e.endsWith(".")?"":"."}`:""}function lc(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 cc(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(c=>"functionId"in c&&c.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 Hr(i,e,t,n){if(!e?.resultPicker||i.length<=1)return 0;let r=e.resultPicker;if((t?lc(t,n):!1)&&t){let l=cc(t,n),c=await r({type:"function",results:l});if(c<0||c>=l.length)throw new Error(`Result picker returned invalid index: ${c}. Must be between 0 and ${l.length-1}`);return c}let s=i.map((l,c)=>({index:c,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 Na(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 Hr(r,t,i,e)}var Kr=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}=pc(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 c=this.tools.get(this.normalizeToolName(a));if(c){l!=null&&typeof l=="object"&&o.set(c.name,l);continue}r[a]=l}for(let[a,l]of Object.entries(e))for(let[c,p]of this.tools.entries()){let u=s.get(c);if(!u)continue;let d=u.get(a);if(!d)continue;let m=o.get(p.name)??{};this.setNested(m,d,l),o.set(p.name,m)}for(let[a,l]of this.tools.entries()){let c=o.get(l.name);if(!(!c||Object.keys(c).length===0)){if(l.parameters&&l.parameters.type==="object"){let u=(l.parameters.required||[]).filter(d=>c[d]===void 0);if(u.length>0)throw new Re(`Missing required arguments for tool '${l.name}': ${u.join(", ")}`)}n.push({id:l.name,name:l.name,args:JSON.stringify(c)})}}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 c=`${e.name}.${l.join(".")}`,p=this.sanitizeFieldName(c);t.set(p,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 pc(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[c,p]of Object.entries(s)){let u=a?`${a}.${c}`:c,d=`${i.name}.${u}`;if(p.type==="object"&&p.properties)o(p.properties,u,p.required||[]);else{let m=uc(p);e.push({name:mc(d),title:dc(i.name,u),type:m,description:p.description||`${c} parameter for ${i.name}`,isOptional:!0}),t.set(d,e[e.length-1])}}};return o(n,"",r),{fields:e,paramFieldMap:t}}function uc(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 dc(i,e){return`${i} ${e.replace(/\./g," ")}`}function mc(i){return i.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}function Ga(i,e,t){let n=new ce(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(c=>c.name===a.name)||n.addOutputField(a);else{let a=Rs(o.name),l=$a(o.parameters);n.getOutputFields().some(p=>p.name===a)||n.addOutputField({name:a,title:Da(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}=gc(o);r.set(o.name,a);for(let l of s)n.getOutputFields().some(p=>p.name===l.name)||n.addOutputField(l)}else{let s=Rs(o.name),a=$a(o.parameters);n.getOutputFields().some(c=>c.name===s)||n.addOutputField({name:s,title:Da(o.name),type:a,description:o.description||`Parameters for ${o.name}`,isOptional:!0})}return{signature:n,toolParamFieldMap:r}}}function gc(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[c,p]of Object.entries(s)){let u=a?`${a}.${c}`:c,d=`${i.name}.${u}`;if(p.type==="object"&&p.properties)o(p.properties,u,p.required||[]);else{let m=hc(p),g={name:Rs(d),title:fc(i.name,u),type:m,description:p.description||`${c} parameter for ${i.name}`,isOptional:!0};e.push(g),t.set(d,g)}}};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 fc(i,e){return`${i} ${e.replace(/\./g," ")}`}function Rs(i){return i.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}function Da(i){return i.replace(/([A-Z])/g," $1").replace(/^./,e=>e.toUpperCase()).trim()}function $a(i){return!i||!i.properties||Object.keys(i.properties).length===0?{name:"string",isArray:!1}:{name:"json",isArray:!1}}var _n=class{tools;router;injectedToolFieldNames=new Set;constructor(e){this.tools=e,this.router=new Kr(e)}processSignature(e){let{signature:t}=Ga(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 we=class extends it{promptTemplate;asserts;streamingAsserts;options;functions;fieldProcessors=[];streamingFieldProcessors=[];excludeContentFromTrace=!1;thoughtFieldName;signatureToolCallingManager;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,cacheSystemPrompt:t?.cacheSystemPrompt};this.promptTemplate=new(t?.promptTemplate??ft)(this.signature,n),this.asserts=this.options?.asserts??[],this.streamingAsserts=this.options?.streamingAsserts??[],this.excludeContentFromTrace=t?.excludeContentFromTrace??!1,this.functions=t?.functions?Lr(t.functions):[],this.usage=[]}getSignatureName(){return this.signature.getDescription()||"unknown_signature"}getMetricsInstruments(){return ps()}updateMeter(e){ps(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} is 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:c,rateLimiter:p,stream:u,thinkingTokenBudget:d,showThoughts:m}=n??{},g=await Na(t,l,{resultPicker:n?.resultPicker}),f=t?.history(g,l)??[];if(f.length===0)throw new Error("No chat prompt found");let h={...n?.modelConfig,...n?.sampleCount?{n:n.sampleCount}:{},...n?.sampleCount&&n?.modelConfig?.temperature===1?{temperature:.8}:{}},A=this.isDebug(e,n),x=a===0,y=this.getLogger(e,n);return o=this.signatureToolCallingManager?[]:o,await e.chat({chatPrompt:f,functions:o,functionCall:s,modelConfig:h,model:c},{sessionId:l,rateLimiter:p,stream:u,debug:A,debugHideSystemPrompt:!x,thinkingTokenBudget:d,showThoughts:m,traceContext:r,abortSignal:n?.abortSignal,stepIndex:a,logger:y,functionCallMode:n?.functionCallMode??this.options?.functionCallMode??"auto"})}async*forwardCore({ai:e,mem:t,options:n,stepIndex:r,span:o,traceContext:s,states:a,stopFunctionNames:l}){let{sessionId:c,functions:p}=n??{},u=n?.functionResultFormatter??this.options?.functionResultFormatter,d=n?.functionCall??this.options?.functionCall,m=this.signatureToolCallingManager,g=n?.strictMode??!1,f=n.model,h=this.usage,A=r===0,x=this.isDebug(e,n),y=this.getLogger(e,n),{functions:b,functionCall:S}=ma(p,d,A,n),I=await this.forwardSendRequest({ai:e,mem:t,options:n,traceContext:s,functions:b,functionCall:S,stepIndex:r});I instanceof ReadableStream?yield*Sa({ai:e,model:f,res:I,mem:t,sessionId:c,traceId:o?o.spanContext?.().traceId:void 0,functions:b,strictMode:g,span:o,states:a,usage:h,asserts:this.asserts,streamingAsserts:this.streamingAsserts,fieldProcessors:this.fieldProcessors,streamingFieldProcessors:this.streamingFieldProcessors,thoughtFieldName:this.thoughtFieldName,excludeContentFromTrace:this.excludeContentFromTrace,signature:this.signature,logger:y,debug:x,functionResultFormatter:u,signatureToolCallingManager:m,stopFunctionNames:l,disableMemoryCleanup:n.disableMemoryCleanup}):yield*va({ai:e,model:f,res:I,mem:t,sessionId:c,traceId:o?o.spanContext?.().traceId:void 0,functions:b,span:o,strictMode:g,states:a,usage:h,asserts:this.asserts,fieldProcessors:this.fieldProcessors,thoughtFieldName:this.thoughtFieldName,excludeContentFromTrace:this.excludeContentFromTrace,signature:this.signature,logger:y,debug:x,functionResultFormatter:u,signatureToolCallingManager:m,stopFunctionNames:l,disableMemoryCleanup:n.disableMemoryCleanup})}async*_forward2(e,t,n,r,o,s){let a=r?.stopFunction??this.options?.stopFunction,l=Array.isArray(a)?a.map(U=>U.toLowerCase()):a?[a.toLowerCase()]:void 0,c=r.maxRetries??this.options?.maxRetries??10,p=r.maxSteps??this.options?.maxSteps??10,u=r.mem??this.options?.mem??new Vt,d=[...this.functions,...r.functions?Lr(r.functions):[]],m=d&&d.length>0,g=r.functionCallMode??this.options?.functionCallMode??"auto",f=r.cacheSystemPrompt??this.options?.cacheSystemPrompt;m&&g==="prompt"&&(this.signatureToolCallingManager=new _n(d)),m&&g==="auto"&&!e.getFeatures(r.model).functions&&(this.signatureToolCallingManager=new _n(d));let h,A,x=this.options?.promptTemplate??ft;this.signatureToolCallingManager&&(this.signature=this.signatureToolCallingManager.processSignature(this.signature),this.setSignature(this.signature));let y={functions:this.signatureToolCallingManager?[]:d,thoughtFieldName:this.thoughtFieldName,cacheSystemPrompt:f};this.promptTemplate=new x(this.signature,y);let b,S=performance.now();Array.isArray(t)?(xi(t),b=this.promptTemplate.render(t,{examples:this.examples,demos:this.demos})):b=this.promptTemplate.render(t,{examples:this.examples,demos:this.demos});let I=performance.now()-S,T=this.getMetricsInstruments();T&&$r(T,"prompt_render",I,this.getSignatureName());let C=performance.now();u.addRequest(b,r.sessionId);let N=performance.now()-C;T&&$r(T,"memory_update",N,this.getSignatureName());e:for(let U=0;U<p;U++){for(let w=0;w<c;w++)try{let E=this.forwardCore({options:r,ai:e,mem:u,stepIndex:U,span:o,traceContext:s,states:n,stopFunctionNames:l}),R=!1;try{for await(let D of E)D!==void 0&&(yield{version:w,index:D.index,delta:D.delta})}catch(D){if(D instanceof ht)R=!0;else throw D}if(R?!1:Oa(u,l,n,r?.sessionId)){let D=this.getMetricsInstruments();D&&Dr(D,U+1,p,this.getSignatureName());continue e}r?.disableMemoryCleanup||(u.removeByTag("invalid-assistant",r.sessionId),u.removeByTag("correction",r.sessionId),u.removeByTag("error",r.sessionId));let F=this.getMetricsInstruments();if(F){Dr(F,U+1,p,this.getSignatureName());let D=new Set;n.forEach(X=>{X.functionsExecuted.forEach(W=>D.add(W))}),D.size>0&&Yi(F,!0,D.size,!0,!1,this.getSignatureName()),Xi(F,this.fieldProcessors.length,this.streamingFieldProcessors.length,this.getSignatureName())}return}catch(E){A=E;let R,M=this.isDebug(e,r),F=this.getLogger(e,r),D=this.getMetricsInstruments(),X=this.getSignatureName(),W={error:E,errCount:w,logger:F,metricsInstruments:D,signatureName:X,span:o,debug:M};if(o?.recordException(E),E instanceof Re)R=ca(W),h=E;else if(E instanceof He)R=pa(W),h=E;else if(E instanceof ae)ua(W);else if(!(E instanceof Ge))throw ws(E,e,this.signature);R&&(u.addTag("error",r.sessionId),u.addRequest([{role:"user",content:this.promptTemplate.renderExtraFields(R)}],r.sessionId),u.addTag("correction",r.sessionId))}let k=this.getMetricsInstruments();throw k&&ds(k,c,!1,c,this.getSignatureName()),ws(new Error(`Unable to fix validation error: ${(h??A)?.message??(h??A)?.toString()??"unknown error"}`),e,this.signature)}throw T&&Dr(T,p,p,this.getSignatureName()),ws(new Error(`Max steps reached: ${p}`),e,this.signature)}async*_forward1(e,t,n){let r=performance.now(),o=this.createStates(n.sampleCount??1),s=performance.now()-r,a=this.getMetricsInstruments();a&&$r(a,"state_creation",s,this.getSignatureName());let l=n?.tracer??this.options?.tracer??e.getOptions().tracer,c=this.functions;if(n?.functions&&(c=Lr(n.functions,this.functions)),!l){yield*this._forward2(e,t,o,{...n,functions:c});return}let p=c?.map(A=>A.name).join(","),u={signature:JSON.stringify(this.signature.toJSON(),null,2),...this.examples?{examples:JSON.stringify(this.examples,null,2)}:{},...p?{provided_functions:p}:{},...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}:{}},d=this.traceLabel&&n.traceLabel?`${this.traceLabel} > ${n.traceLabel}`:n.traceLabel??this.traceLabel,m=d?`AxGen > ${d}`:"AxGen",g=l.startSpan(m,{kind:Yt.SpanKind.SERVER,attributes:u}),f=Yt.context.active(),h=Yt.trace.setSpan(f,g);try{if(this.excludeContentFromTrace||g.addEvent("input",{content:JSON.stringify(t,null,2)}),yield*this._forward2(e,t,o,{...n,functions:c},g,h),!this.excludeContentFromTrace){let A=o.map(y=>y.values),x=A.length===1?A[0]:A;g.addEvent("output",{content:JSON.stringify(x,null,2)})}}finally{g.end()}}async forward(e,t,n){let r=performance.now(),o=this.getSignatureName(),s=n?.stream??!1,a=!1,l=0,c=!1;try{let p=this.getMetricsInstruments();p&&ta(p,this.signature.getInputFields().length,this.signature.getOutputFields().length,this.examples?.length??0,this.demos?.length??0,o);let u=this._forward1(e,t,n??{}),d=[],m=0,g=0;for await(let S of u)S.version!==m&&(d=[]),m=S.version,d=hs(d,S),g++;l=m;let f=performance.now();c=!!n?.resultPicker;let h=await Hr(d,{resultPicker:n?.resultPicker},n?.mem,n?.sessionId),A=performance.now()-f,y=d[h]?.delta??{},b=Array.isArray(t)?{}:t??{};if(this.trace={...b,...y},c&&this.isDebug(e,n)){let S=this.getLogger(e,n);ei(d.length,h,A,S)}return a=!0,p&&(ea(p,d.length,c,c?A:void 0,o),Zi(p,s,g,void 0,o)),y}catch(p){throw a=!1,p}finally{let p=performance.now()-r,u=this.getMetricsInstruments();u&&(Ji(u,p,a,o,e.getName(),n?.model?String(n.model):void 0),l>0&&ds(u,l,a,n?.maxRetries??10,o))}}async*streamingForward(e,t,n){if(!n?.resultPicker){yield*this._forward1(e,t,{...n,stream:!0});return}let r=this._forward1(e,t,{...n,stream:!0}),o=[],s=0;for await(let c of r)c.version!==s&&(o=[]),s=c.version,o=hs(o,c);let a=await Hr(o,{resultPicker:n?.resultPicker},n?.mem,n?.sessionId),l=o[a];l&&(yield{version:s,index:a,delta:l.delta})}setExamples(e,t){super.setExamples(e,t)}isDebug(e,t){return t?.debug??this.options?.debug??e.getOptions().debug??!1}getLogger(e,t){return t?.logger??this.options?.logger??se.logger??e.getLogger()}},Nn=class extends Error{details;constructor(e,t,n){super(e),this.name="AxGenerateError",this.details=t,n?.cause&&(this.cause=n.cause)}};function ws(i,e,t){let n=i instanceof Error?i:new Error(String(i)),r=e.getLastUsedChatModel(),o=e.getLastUsedModelConfig(),s={model:r,maxTokens:o?.maxTokens,streaming:o?.stream??!1,signature:{input:t.getInputFields(),output:t.getOutputFields(),description:t.getDescription()}};return new Nn("Generate failed",s,{cause:n})}var Ac=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]},yc=i=>{let e=new Set,t=[];for(let n of i)e.has(n)||(e.add(n),t.push(n));return t},bc=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}},Ic=i=>{let e=i.match(/^(\d+)[.,\s]+(.*)$/);return e&&e[2]!==void 0?e[2].trim():i},Tc=(i,e)=>{let t=[];for(let n=0;n<i.length;n+=e)t.push(i.slice(n,n+e));return t},Wr={trimNonAlphaNum:Ac,splitIntoTwo:xc,dedup:yc,extractIdAndText:bc,extractIndexPrefixedText:Ic,batchArray:Tc};var Vr=class extends we{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."
230
236
  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}=Wr.extractIdAndText(a);return l});return{rankedItems:t.items.map((a,l)=>{let c=o[l];return c!==void 0?t.items[c]:void 0}).filter(a=>a!==void 0)}}};var Jr=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(c=>this._convert(c,{format:t?.format})),l=await Promise.all(a);n.push(...l)}return n}};var Zg=new Te,Qr=class{name;context;constructor(e,t){this.name=e,this.context=t}getName(){return this.name}getContext(){return this.context}},Yr=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 La=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 Ua(i,e){return i.filter(t=>!e.has(t))}function Ba(i){let e={};for(let t of i)e[t]=(e[t]||0)+1;return e}function At(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 Cc(i,e){return At(i)===At(e)?1:0}function Rc(i,e){let t=At(i).split(" "),n=At(e).split(" "),r=Ba(t),o=Ba(n),s=0;for(let c in r){let p=r[c]??0,u=o[c]??0;s+=Math.min(p,u)}if(s===0)return 0;let a=s/t.length,l=s/n.length;return 2*a*l/(a+l)}function wc(i,e,t,n=!1){let r=At(i).split(" "),o=At(e).split(" "),s=At(t).split(" "),a=new Set([...La,...r]);o=Ua(o,a),s=Ua(s,a);let l=0,c=l/o.length,p=l/s.length,u=2*c*p/(c+p);return n?p:u}var qa={emScore:Cc,f1Score:Rc,novelF1ScoreOptimized:wc};var Xr=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}),c=await e({prediction:l,example:a});r+=c}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(`
231
237
  Performance: `,r,"/",n,"Average Score: ",o,`
232
238
  `)}};var Zr=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("."),c=o.row;for(let u of l)Object.hasOwn(c,u)&&(c=c[u]);if(!c)return;let p=n&&a in n?n[a]:a;if(!p)throw new Error(`Invalid field name: ${a}`);s[p]=c}),s}).filter(o=>Object.keys(o).length!==0)}};var za=i=>{console.log(i)},Ss=(i=za)=>{let e=new Te,t=e.gray("\u2500".repeat(50)),n=e.gray("\u2501".repeat(50));return r=>{let o="";switch(r.name){case"OptimizationStart":o=`
@@ -309,9 +315,9 @@ _(empty)_`}).join(`
309
315
  `);return`${e}
310
316
  ${t}`.trim()}function Uc(i){let e=i.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"").slice(0,6),t=Va.default.randomBytes(4).toString("hex");return`${e||"ctx"}-${t}`}function Bc(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,c=a-l*2,p=Date.parse(s.updatedAt??s.createdAt),u=[c,a,Number.isFinite(p)?p:Number.POSITIVE_INFINITY];if(!n){t=o,n=u;continue}let d=i[t],m=d.helpfulCount??0,g=d.harmfulCount??0,f=m-g*2,h=Date.parse(d.updatedAt??d.createdAt),A=[f,m,Number.isFinite(h)?h:Number.POSITIVE_INFINITY];(u[0]<A[0]||u[0]===A[0]&&u[1]<A[1]||u[0]===A[0]&&u[1]===A[1]&&u[2]<A[2])&&(t=o,n=u)}if(t===-1)return;let[r]=i.splice(t,1);return r}function Es(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}Ps(i)}function Ps(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+=Lc(s.content);i.stats={bulletCount:e,helpfulCount:t,harmfulCount:n,tokenEstimate:r}}var qc={maxEpochs:1,maxReflectorRounds:2,maxSectionSize:25,similarityThreshold:.95,allowDynamicSections:!0},Ln=class extends We{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=xt(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(),"",Gn(this.playbook)].filter(o=>o&&o.trim().length>0).join(`
311
317
 
312
- `);e.setDescription(r)}},no=class extends Se{aceConfig;playbook;generatorHistory=[];deltaHistory=[];reflectorProgram;curatorProgram;constructor(e,t){super(e),this.aceConfig={...qc,...t},this.playbook=t?.initialPlaybook!==void 0?xt(t.initialPlaybook):Os()}reset(){super.reset(),this.playbook=this.aceConfig.initialPlaybook!==void 0?xt(this.aceConfig.initialPlaybook):Os(),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=xt(o.initialPlaybook)));let s=Date.now();this.validateExamples(t);let a=await this.extractProgramInstruction(e),l=e.getSignature().getDescription()??"";this.generatorHistory=[],this.deltaHistory=[];let c=Number.NEGATIVE_INFINITY,p=0,u=Math.max(this.aceConfig.maxEpochs,1),d=u*t.length;try{for(let A=0;A<u;A++)for(let x=0;x<t.length;x++){let y=t[x],b=this.composeInstruction(a??l,this.playbook);e.setDescription?.(b);let S=await e.forward(this.studentAI,y);this.stats.totalCalls+=1;let I=await n({prediction:S,example:y});typeof I=="number"&&(this.stats.bestScore=Math.max(this.stats.bestScore,I),c=Math.max(c,I));let T=S?.severity,C=y?.severity,N=this.createGeneratorOutput(S,y),U=C&&T&&C!==T,P=await this.runReflectionRounds({example:y,generatorOutput:N,feedback:C&&T&&C!==T?`Expected severity "${C}" but model predicted "${T}".`:void 0}),w=await this.runCurator({example:y,reflection:P,playbook:this.playbook}),O=this.normalizeCuratorOperations(w?.operations);O.length===0&&U&&(O=this.inferOperationsFromReflection(P)),O=this.resolveCuratorOperationTargets(O,this.playbook,P,N);let R=w||O.length>0?{...w??{},operations:O}:void 0,v=[];if(O.length>0){let xe=this.collectProtectedBulletIds(O),pe=ks(this.playbook,O,{maxSectionSize:this.aceConfig.maxSectionSize,allowDynamicSections:this.aceConfig.allowDynamicSections,enableAutoPrune:!0,protectedBulletIds:xe});v=pe.updatedBulletIds,pe.autoRemoved.length>0&&(O.push(...pe.autoRemoved),R&&(R.operations=O))}if(P?.bulletTags)for(let xe of P.bulletTags)Ms(this.playbook,xe.id,xe.tag);O.length>0&&v.length>0&&Es(this.playbook,this.aceConfig.similarityThreshold);let k={example:y,prediction:S,score:typeof I=="number"?I:0,generatorOutput:N,reflection:P,curator:R,timestamp:new Date().toISOString()};this.generatorHistory.push(k),v.length>0&&R?.operations?.length&&this.deltaHistory.push({epoch:A,exampleIndex:x,operations:R.operations}),p+=1,this.currentRound=p;let G=typeof I=="number"&&Number.isFinite(I)?I:0,X=Number.isFinite(c)?c:G,J={...r??{},maxIterations:d};await this.updateOptimizationProgress(p,G,{epoch:A,exampleIndex:x,playbookBullets:this.playbook.stats.bulletCount},"ACE",{epochs:u,totalRounds:d},X,{playbookBullets:this.playbook.stats.bulletCount},void 0,J),this.stats.convergenceInfo.finalImprovement=Math.max(this.stats.convergenceInfo.finalImprovement,G)}}finally{e.setDescription?.(l)}let m=Date.now()-s;this.stats.resourceUsage.totalTime=m,this.stats.convergenceInfo.converged=!0,this.stats.bestScore=Number.isFinite(c)?c:0;let g={playbook:xt(this.playbook),feedback:[...this.generatorHistory],history:[...this.deltaHistory]},f=new Ln({baseInstruction:a??l,playbook:this.playbook,artifact:g,bestScore:Number.isFinite(c)?c:0,stats:this.stats,optimizerType:"ACE",optimizationTime:m,totalRounds:p,converged:this.stats.convergenceInfo.converged});return{stats:this.stats,bestScore:Number.isFinite(c)?c:0,finalConfiguration:{strategy:"ace",epochs:u},optimizedProgram:f,playbook:xt(this.playbook),artifact:g}}async applyOnlineUpdate(e){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({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 c=s||a.length>0?{...s??{},operations:a}:void 0;if(o?.bulletTags)for(let u of o.bulletTags)Ms(this.playbook,u.id,u.tag);if(a.length>0){let u=this.collectProtectedBulletIds(a),d=ks(this.playbook,a,{maxSectionSize:this.aceConfig.maxSectionSize,allowDynamicSections:this.aceConfig.allowDynamicSections,enableAutoPrune:!0,protectedBulletIds:u});d.autoRemoved.length>0&&(a.push(...d.autoRemoved),c&&(c.operations=a)),Es(this.playbook,this.aceConfig.similarityThreshold)}let p={example:e.example,prediction:e.prediction,score:0,generatorOutput:t,reflection:o,curator:c,timestamp:new Date().toISOString()};return this.generatorHistory.push(p),c}composeInstruction(e,t){return[e.trim(),"",Gn(t)].filter(r=>r.trim().length>0).join(`
318
+ `);e.setDescription(r)}},no=class extends Se{aceConfig;playbook;generatorHistory=[];deltaHistory=[];reflectorProgram;curatorProgram;constructor(e,t){super(e),this.aceConfig={...qc,...t},this.playbook=t?.initialPlaybook!==void 0?xt(t.initialPlaybook):Os()}reset(){super.reset(),this.playbook=this.aceConfig.initialPlaybook!==void 0?xt(this.aceConfig.initialPlaybook):Os(),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=xt(o.initialPlaybook)));let s=Date.now();this.validateExamples(t);let a=await this.extractProgramInstruction(e),l=e.getSignature().getDescription()??"";this.generatorHistory=[],this.deltaHistory=[];let c=Number.NEGATIVE_INFINITY,p=0,u=Math.max(this.aceConfig.maxEpochs,1),d=u*t.length;try{for(let A=0;A<u;A++)for(let x=0;x<t.length;x++){let y=t[x],b=this.composeInstruction(a??l,this.playbook);e.setDescription?.(b);let S=await e.forward(this.studentAI,y);this.stats.totalCalls+=1;let I=await n({prediction:S,example:y});typeof I=="number"&&(this.stats.bestScore=Math.max(this.stats.bestScore,I),c=Math.max(c,I));let T=S?.severity,C=y?.severity,N=this.createGeneratorOutput(S,y),U=C&&T&&C!==T,k=await this.runReflectionRounds({example:y,generatorOutput:N,feedback:C&&T&&C!==T?`Expected severity "${C}" but model predicted "${T}".`:void 0}),w=await this.runCurator({example:y,reflection:k,playbook:this.playbook}),E=this.normalizeCuratorOperations(w?.operations);E.length===0&&U&&(E=this.inferOperationsFromReflection(k)),E=this.resolveCuratorOperationTargets(E,this.playbook,k,N);let R=w||E.length>0?{...w??{},operations:E}:void 0,M=[];if(E.length>0){let xe=this.collectProtectedBulletIds(E),pe=ks(this.playbook,E,{maxSectionSize:this.aceConfig.maxSectionSize,allowDynamicSections:this.aceConfig.allowDynamicSections,enableAutoPrune:!0,protectedBulletIds:xe});M=pe.updatedBulletIds,pe.autoRemoved.length>0&&(E.push(...pe.autoRemoved),R&&(R.operations=E))}if(k?.bulletTags)for(let xe of k.bulletTags)Ms(this.playbook,xe.id,xe.tag);E.length>0&&M.length>0&&Es(this.playbook,this.aceConfig.similarityThreshold);let F={example:y,prediction:S,score:typeof I=="number"?I:0,generatorOutput:N,reflection:k,curator:R,timestamp:new Date().toISOString()};this.generatorHistory.push(F),M.length>0&&R?.operations?.length&&this.deltaHistory.push({epoch:A,exampleIndex:x,operations:R.operations}),p+=1,this.currentRound=p;let D=typeof I=="number"&&Number.isFinite(I)?I:0,X=Number.isFinite(c)?c:D,W={...r??{},maxIterations:d};await this.updateOptimizationProgress(p,D,{epoch:A,exampleIndex:x,playbookBullets:this.playbook.stats.bulletCount},"ACE",{epochs:u,totalRounds:d},X,{playbookBullets:this.playbook.stats.bulletCount},void 0,W),this.stats.convergenceInfo.finalImprovement=Math.max(this.stats.convergenceInfo.finalImprovement,D)}}finally{e.setDescription?.(l)}let m=Date.now()-s;this.stats.resourceUsage.totalTime=m,this.stats.convergenceInfo.converged=!0,this.stats.bestScore=Number.isFinite(c)?c:0;let g={playbook:xt(this.playbook),feedback:[...this.generatorHistory],history:[...this.deltaHistory]},f=new Ln({baseInstruction:a??l,playbook:this.playbook,artifact:g,bestScore:Number.isFinite(c)?c:0,stats:this.stats,optimizerType:"ACE",optimizationTime:m,totalRounds:p,converged:this.stats.convergenceInfo.converged});return{stats:this.stats,bestScore:Number.isFinite(c)?c:0,finalConfiguration:{strategy:"ace",epochs:u},optimizedProgram:f,playbook:xt(this.playbook),artifact:g}}async applyOnlineUpdate(e){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({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 c=s||a.length>0?{...s??{},operations:a}:void 0;if(o?.bulletTags)for(let u of o.bulletTags)Ms(this.playbook,u.id,u.tag);if(a.length>0){let u=this.collectProtectedBulletIds(a),d=ks(this.playbook,a,{maxSectionSize:this.aceConfig.maxSectionSize,allowDynamicSections:this.aceConfig.allowDynamicSections,enableAutoPrune:!0,protectedBulletIds:u});d.autoRemoved.length>0&&(a.push(...d.autoRemoved),c&&(c.operations=a)),Es(this.playbook,this.aceConfig.similarityThreshold)}let p={example:e.example,prediction:e.prediction,score:0,generatorOutput:t,reflection:o,curator:c,timestamp:new Date().toISOString()};return this.generatorHistory.push(p),c}composeInstruction(e,t){return[e.trim(),"",Gn(t)].filter(r=>r.trim().length>0).join(`
313
319
 
314
- `)}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(p=>p.bulletId).filter(p=>typeof p=="string")),a=new Map,l=(p,u)=>{if(s.has(p))return;let d=this.locateBullet(t,p);if(!d)return;let m=a.get(d.section)??{harmful:[],primary:[],generator:[]};m[u].push(d.id),a.set(d.section,m)};for(let p of n?.bulletTags??[]){let u=p.tag==="harmful"?"harmful":"primary";l(p.id,u)}if(r?.bulletIds)for(let p of r.bulletIds)l(p,"generator");let c=p=>{let u=a.get(p);if(!u)return this.locateFallbackBullet(t,p,s);let d=g=>{for(;g.length>0;){let f=g.shift();if(!s.has(f))return f}},m=d(u.harmful)??d(u.primary)??d(u.generator);return m||this.locateFallbackBullet(t,p,s)};for(let p of e){if((p.type==="UPDATE"||p.type==="REMOVE")&&!p.bulletId){let u=c(p.section);u&&(p.bulletId=u,s.add(u))}(p.type==="UPDATE"||p.type==="REMOVE")&&!p.bulletId||o.push(p)}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",c=typeof l=="string"&&l.trim().length>0?l.trim():"Guidelines",p=r.content??"",u=typeof p=="string"?p.trim():"";if(a!=="REMOVE"&&u.length===0)continue;let d=r.bulletId??r.id,m=typeof d=="string"&&d.trim().length>0?d.trim():void 0,f=[a,c,u,m??""].join(":");if(n.has(f))continue;n.add(f);let h={type:a,section:c};a!=="REMOVE"&&(h.content=u),m&&(h.bulletId=m);let A=r.metadata;A&&typeof A=="object"&&(h.metadata={...A}),t.push(h)}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 c=`${o}:${a}`;n.has(c)||(n.add(c),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};return await o.forward(s,{question:JSON.stringify(e),generator_answer:JSON.stringify(t.answer),generator_reasoning:t.reasoning,playbook:JSON.stringify({markdown:Gn(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({example:e,reflection:t,playbook:n}){if(!t)return;let r=this.getOrCreateCuratorProgram(),o=this.teacherAI??this.studentAI;try{return await r.forward(o,{playbook:JSON.stringify({markdown:Gn(n),structured:n}),reflection:JSON.stringify(t),question_context:JSON.stringify(e),token_budget:1024})}catch(s){this.verbose&&console.warn("[AxACE] Curator error:",s instanceof Error?s.message:s);return}}getOrCreateReflectorProgram(){if(!this.reflectorProgram){let e=ne().input("question",ne.string("Original task input serialized as JSON")).input("generator_answer",ne.string("Generator output serialized as JSON")).input("generator_reasoning",ne.string("Generator reasoning trace").optional()).input("playbook",ne.string("Current context playbook rendered as markdown")).input("expected_answer",ne.string("Expected output when ground truth is available").optional()).input("feedback",ne.string("External feedback or reward signal").optional()).input("previous_reflection",ne.string("Most recent reflection JSON when running multi-round refinement").optional()).output("reasoning",ne.string("Step-by-step analysis of generator performance")).output("errorIdentification",ne.string("Specific mistakes detected")).output("rootCauseAnalysis",ne.string("Underlying cause of the error")).output("correctApproach",ne.string("What the generator should do differently")).output("keyInsight",ne.string("Reusable insight to remember")).output("bulletTags",ne.json("Array of {id, tag} entries referencing playbook bullets")).build();this.reflectorProgram=ge(e)}return this.reflectorProgram}getOrCreateCuratorProgram(){if(!this.curatorProgram){let e=ne().input("playbook",ne.string("Current playbook serialized as JSON")).input("reflection",ne.string("Latest reflection output serialized as JSON")).input("question_context",ne.string("Original task input serialized as JSON")).input("token_budget",ne.number("Approximate token budget for curator response").optional()).output("reasoning",ne.string("Justification for the proposed updates")).output("operations",ne.json("List of operations with type/section/content fields")).build();this.curatorProgram=ge(e)}return this.curatorProgram}};var Xt=class extends Se{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 c=jc([...t],this.maxExamples),p=this.traces.length;for(let u=0;u<c.length;u+=this.batchSize){u>0&&(l.modelConfig.temperature=.7+.001*u);let d=c.slice(u,u+this.batchSize);for(let m of d){if(!m||typeof m!="object")continue;let g=t.filter(A=>A!==m);e.setExamples(g);let f=this.getTeacherOrStudentAI();this.stats.totalCalls++;let h;try{let A={...l,maxRetries:1};h=await e.forward(f,m,A),this.costMonitoring&&(this.stats.estimatedTokenUsage+=JSON.stringify(m).length/4+JSON.stringify(h).length/4),await r({prediction:h,example:m})>=.5&&(this.traces=[...this.traces,...e.getTraces()],this.stats.successfulDemos++)}catch(A){(this.verboseMode||this.debugMode)&&console.warn(`Student model failed during bootstrap: ${A instanceof Error?A.message:"Unknown error"}`),h={}}if(this.traces.length>=a)return}}if(this.earlyStoppingPatience>0){let d=this.traces.length-p;if(!this.stats.earlyStopping)this.stats.earlyStopping={bestScoreRound:d>0?n:0,patienceExhausted:!1,reason:"No improvement detected"};else if(d>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=zc(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 zc(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 jc=(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)};function Ja(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 Ve(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(Ja(i[s].scores,i[n].scores,e)){o=!0;break}Ja(i[n].scores,i[s].scores,e)&&r++}o||t.push({idx:i[n].idx,scores:i[n].scores,dominated:r})}return t}function Fe(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,c=a[n]??0,p=Math.max(c-s,0);o+=l*p,s=Math.max(s,c)}return o}function yt(i){if(i.length===0)return 0;let e=0;for(let t of i)e+=t;return e/i.length}function ro(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 Un(i,e){let t=new Set;for(let p of i)for(let u of p)t.add(u);let r=[...Array.from(t)].sort((p,u)=>(e[p]??0)-(e[u]??0)),o=new Set,s=(p,u)=>{for(let d of i){if(!d.has(p))continue;let m=!1;for(let g of u)if(d.has(g)){m=!0;break}if(!m)return!1}return!0},a=!0;for(;a;){a=!1;for(let p of r){if(o.has(p))continue;let u=new Set(r.filter(d=>d!==p&&!o.has(d)));if(s(p,u)){o.add(p),a=!0;break}}}let l=r.filter(p=>!o.has(p)),c=new Set(l);return i.map(p=>{let u=new Set;for(let d of p)c.has(d)&&u.add(d);return u})}function Bn(i,e,t){let n=Un(i,e),r={};for(let l of n)for(let c of l)r[c]=(r[c]||0)+1;let o=[];for(let[l,c]of Object.entries(r)){let p=Number(l);for(let u=0;u<c;u++)o.push(p)}if(o.length===0)return 0;let s=typeof t=="function"?t():Math.random(),a=Math.floor(s*o.length);return o[a]}var oo=class extends Se{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;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??0)),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),c=a&&a.length>0?a:t,p=async(R,v)=>{try{e.setInstruction?.(R);let k=await e.forward(this.studentAI,v,{sampleCount:this.sampleCount});return this.stats.totalCalls+=1,await n({prediction:k,example:v})||{}}catch{return{}}},u=async(R,v)=>{let k=[];for(let G of v)k.push(await p(R,G));return ro(k)},d=await this.getBaseInstruction(e),m=[{instruction:d,parent:void 0,scores:await u(d,l)}],g=R=>{let v=r?.paretoMetricKey,k=r?.paretoScalarize;if(typeof k=="function")return k(R);if(v)return Number.isFinite(R[v])?R[v]:0;let G=Object.values(R);return G.length?G.reduce((X,J)=>X+J,0)/G.length:0},f=[],h=async(R,v)=>{let k=[];for(let G of v){let X=await p(R,G);k.push(g(X))}return k};f.push(await h(d,l));let A=()=>{let R=f[0]?.length??0,v=[];for(let G=0;G<R;G++){let X=Number.NEGATIVE_INFINITY,J=new Set;for(let xe=0;xe<f.length;xe++){let pe=f[xe][G];pe>X+this.tieEpsilon?(X=pe,J.clear(),J.add(xe)):Math.abs(pe-X)<=this.tieEpsilon&&J.add(xe)}v.push(J)}let k=f.map(G=>yt(G));return Bn(v,k)};this.getOptimizerLogger(r)?.({name:"OptimizationStart",value:{optimizerType:"GEPA",exampleCount:t.length,validationCount:l.length,config:{numTrials:this.numTrials,minibatch:this.minibatch}}});let y=0,b=Ve(m.map((R,v)=>({idx:v,scores:R.scores})),this.tieEpsilon).map(R=>R.idx),S,I=r?.maxMetricCalls;if(!Number.isFinite(I)||I<=0)throw new Error("AxGEPA: options.maxMetricCalls must be set to a positive integer");let T=Math.floor(I);for(let R=0;R<this.numTrials&&!(T!==void 0&&this.stats.totalCalls>=Math.max(1,Math.floor(T)));R++){let v=f[0]?.length??0,k=[];for(let M=0;M<v;M++){let D=Number.NEGATIVE_INFINITY,z=new Set;for(let j=0;j<f.length;j++){let H=f[j][M];H>D+this.tieEpsilon?(D=H,z.clear(),z.add(j)):Math.abs(H-D)<=this.tieEpsilon&&z.add(j)}k.push(z)}let G=f.map(M=>yt(M));if(this.mergeMax>0&&this.mergesDue>0&&this.lastIterFoundNewProgram){let M=F=>{let E=[],_=F;for(;_!==void 0;)E.push(_),_=m[_]?.parent;return E},D=F=>F.length?F[Math.floor(this.rand()*F.length)]:void 0,z=Un(k,G),j=new Set;for(let F of z)for(let E of F)j.add(E);let H=Array.from(j),ee;for(let F=0;F<10&&!ee&&!(H.length<2);F++){let E=D(H),_=D(H);if(E===_)continue;_<E&&([E,_]=[_,E]);let Y=new Set(M(E)),le=new Set(M(_));if(Y.has(_)||le.has(E))continue;let Z=[...Y].filter(ie=>le.has(ie));if(Z.length===0)continue;let K=Z.map(ie=>Math.max(1e-9,G[ie])),W=this.rand()*K.reduce((ie,Ne)=>ie+Ne,0),V=Z[Z.length-1];for(let ie=0;ie<Z.length;ie++){if(W<K[ie]){V=Z[ie];break}W-=K[ie]}ee={i:E,j:_,a:V}}if(this.lastIterFoundNewProgram=!1,ee){let{i:F,j:E,a:_}=ee,Y=G[_],le=G[F],Z=G[E],K=m[_].instruction,W=m[F].instruction,V=m[E].instruction,ie=W===K&&V!==W||V===K&&W!==V,Ne=Y<=Math.min(le,Z)&&ie,ve="",Oe="i",et=!1;if(Ne){let Je=`${F}|${E}|${_}`;if(this.mergeAttemptKeys.has(Je))Ne=!1;else{W===K&&V!==W?(ve=V,Oe="j"):V===K&&W!==V?(ve=W,Oe="i"):W!==K&&V!==K&&W!==V?le>Z||le===Z&&this.rand()<.5?(ve=W,Oe="i"):(ve=V,Oe="j"):(ve=W,Oe="i");let oe=`${Math.min(F,E)}|${Math.max(F,E)}|${Oe}`;if(this.mergeCompositionKeys.has(oe))Ne=!1;else{this.mergeAttemptKeys.add(Je),this.mergeCompositionKeys.add(oe);let De=f[F],Qe=f[E],ke=Array.from({length:De.length},(te,be)=>be),tt=ke.filter(te=>(De[te]??0)>(Qe[te]??0)),bt=ke.filter(te=>(Qe[te]??0)>(De[te]??0)),lt=ke.filter(te=>!(tt.includes(te)||bt.includes(te))),Ye=5,nt=Math.ceil(Ye/3),B=(te,be)=>{if(be<=0||te.length===0)return[];if(te.length<=be)return[...te];let ln=[],Kn=new Set;for(;ln.length<be;){let $e=Math.floor(this.rand()*te.length);Kn.has($e)||(Kn.add($e),ln.push(te[$e]))}return ln},re=[];re.push(...B(tt,Math.min(nt,tt.length))),re.push(...B(bt,Math.min(nt,bt.length)));let ct=Ye-re.length;re.push(...B(lt,Math.max(0,ct)));let pt=Ye-re.length;if(pt>0){let te=ke.filter(be=>!re.includes(be));re.push(...B(te,Math.min(pt,te.length)))}let ye=re.slice(0,Math.min(Ye,ke.length)),Ro=ye.map(te=>l[te]);et=!0;let ml=(await h(ve,Ro)).reduce((te,be)=>te+be,0),gl=ye.reduce((te,be)=>te+(De[be]??0),0),hl=ye.reduce((te,be)=>te+(Qe[be]??0),0);if(ml>=Math.max(gl,hl)+this.tieEpsilon){let te=await u(ve,l);m.push({instruction:ve,parent:_,scores:te}),f.push(await h(ve,l));let be=b.length,ln=Fe(b.map($e=>m[$e].scores))??0;b=Ve(m.map(($e,fl)=>({idx:fl,scores:$e.scores})),this.tieEpsilon).map($e=>$e.idx);let Kn=Fe(b.map($e=>m[$e].scores))??0;(b.length>be||Kn>ln+1e-6)&&(y=0),this.mergesDue-=1,this.totalMergesTested+=1}}}}if(et)continue}}let X=Bn(k,G,()=>this.rand()),J=this.minibatch?this.nextMinibatchIndices(c.length,R).map(M=>c[M]):c;if(r?.skipPerfectScore??!0){let M=Number(r?.perfectScore??1),D=await h(m[X].instruction,J);if(D.length>0&&D.every(z=>z>=M))continue}let xe=!1,pe=m[X].instruction,he="reflective_mutation",_e,Ze;if(xe){let M=(X+1)%m.length;pe=await this.mergeInstructions(m[X].instruction,m[M].instruction,r),he="merge",this.mergesUsed+=1}else{let M=r?.gepaAdapter;if(M){try{let D={instruction:m[X].instruction},z=await M.evaluate(J,D,!0);_e=Array.isArray(z?.scores)?z.scores.reduce((F,E)=>F+(Number(E)||0),0):void 0;let j=M.make_reflective_dataset(D,z,["instruction"]),H=await M.propose_new_texts?.(D,j,["instruction"]),ee=H?.instruction??(H?Object.values(H)[0]:void 0);typeof ee=="string"&&ee.length>0?pe=ee:pe=await this.reflectInstruction(m[X].instruction,e,J,async({prediction:F,example:E})=>{let _=await n({prediction:F,example:E}),Y=Object.values(_||{});return Y.length?Y.reduce((le,Z)=>le+Z,0)/Y.length:0},r)}catch{pe=await this.reflectInstruction(m[X].instruction,e,J,async({prediction:D,example:z})=>{let j=await n({prediction:D,example:z}),H=Object.values(j||{});return H.length?H.reduce((ee,F)=>ee+F,0)/H.length:0},r)}if(_e!==void 0)try{let D=await M.evaluate(J,{instruction:pe},!1);Ze=Array.isArray(D?.scores)?D.scores.reduce((z,j)=>z+(Number(j)||0),0):void 0}catch{}}else pe=await this.reflectInstruction(m[X].instruction,e,J,async({prediction:D,example:z})=>{let j=await n({prediction:D,example:z}),H=Object.values(j||{});return H.length?H.reduce((ee,F)=>ee+F,0)/H.length:0},r)}let Ao=await h(m[X].instruction,J),xo=await h(pe,J),yo=Ao.reduce((M,D)=>M+D,0),at=xo.reduce((M,D)=>M+D,0);if(this.currentRound=R+1,await this.updateOptimizationProgress(this.currentRound,at,{instructionLen:pe.length,parent:X,totalRounds:this.numTrials},"GEPA",{strategy:he,paretoSetSize:l.length},at,{instructionLen:m[X].instruction.length,idx:X},{...r??{},maxIterations:this.numTrials}),!(at>yo+this.tieEpsilon&&(_e===void 0||Ze===void 0||Ze>_e+this.tieEpsilon))){if(++y>=this.earlyStoppingTrials)break;continue}let bo=await u(pe,l);m.push({instruction:pe,parent:X,scores:bo}),f.push(await h(pe,l));let Io=b.length,To=Fe(b.map(M=>m[M].scores))??0;b=Ve(m.map((M,D)=>({idx:D,scores:M.scores})),this.tieEpsilon).map(M=>M.idx);let Co=Fe(b.map(M=>m[M].scores))??0;if(b.length>Io||Co>To+1e-6)y=0;else if(y++,y>=this.earlyStoppingTrials)break;this.lastIterFoundNewProgram=!0,this.mergeMax>0&&this.totalMergesTested<this.mergeMax&&(this.mergesDue+=1)}let C=Ve(m.map((R,v)=>({idx:v,scores:R.scores})),this.tieEpsilon),N=C.length>0?Math.max(...C.map(R=>g(R.scores))):0,U;if(C.length>0){let R=Number.NEGATIVE_INFINITY;for(let v of C){let k=g(v.scores);k>R&&(R=k,U=v.idx)}}let P=Fe(C.map(R=>R.scores));this.stats.convergenceInfo.converged=!0,this.recordParetoMetrics(C.length,m.length,"GEPA",P);let w=Date.now()-o,O=typeof U=="number"?new We({bestScore:N,stats:this.stats,instruction:m[U].instruction,demos:[],examples:t,modelConfig:void 0,optimizerType:"GEPA",optimizationTime:w,totalRounds:this.numTrials,converged:this.stats.convergenceInfo.converged}):void 0;return this.generateOptimizationReport(C,P,N),{demos:[],stats:this.stats,bestScore:N,paretoFront:C.map(R=>({demos:[],scores:R.scores,configuration:{candidate:R.idx},dominatedSolutions:R.dominated})),paretoFrontSize:C.length,hypervolume:P,finalConfiguration:{strategy:"gepa",candidates:m.length},optimizedProgram:O}}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){try{let t=e.getSignature?.();if(t&&typeof t.instruction=="string"&&t.instruction.length>0)return t.instruction}catch{}return"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?yt(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){let s=[];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 f=await r({prediction:g,example:m});s.push({input:m,prediction:g,score:typeof f=="number"?f:0})}catch{s.push({input:m,prediction:{},score:0})}let a=o?.overrideTeacherAI??this.teacherAI??this.studentAI,l=ge('minibatch:json "Array of {input,prediction,score}", evalFeedback?:string[] "Evaluator feedback per case if available" -> feedbackSummary:string "Concise feedback: common errors, missing constraints, desired changes"'),c=[],p=o?.feedbackFn;if(typeof p=="function")for(let m=0;m<s.length;m++)try{let g=p({prediction:s[m].prediction,example:s[m].input});g&&(Array.isArray(g)?c.push(...g):c.push(g))}catch{}let u="";try{u=(await l.forward(a,{minibatch:s,evalFeedback:c}))?.feedbackSummary?.trim()||"",u&&(this.feedbackMemory.unshift(u),this.feedbackMemory.length>this.feedbackMemorySize&&this.feedbackMemory.pop())}catch{}let d=ge('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(a,{currentInstruction:e,feedbackSummary:u,recentFeedback:this.feedbackMemory,minibatch:s}))?.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)}updateSamplerShuffled(e){let t=Array.from({length:e},(l,c)=>c);for(let l=t.length-1;l>0;l--){let c=Math.floor(this.rand()*(l+1));[t[l],t[c]]=[t[c],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,c)=>c).sort((l,c)=>(this.samplerState.freq.get(l)??0)-(this.samplerState.freq.get(c)??0)),a=[...t];for(let l=0;l<o;l++){let c=s[l%s.length];a.push(c),this.samplerState.freq.set(c,(this.samplerState.freq.get(c)??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){console.log(`
320
+ `)}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(p=>p.bulletId).filter(p=>typeof p=="string")),a=new Map,l=(p,u)=>{if(s.has(p))return;let d=this.locateBullet(t,p);if(!d)return;let m=a.get(d.section)??{harmful:[],primary:[],generator:[]};m[u].push(d.id),a.set(d.section,m)};for(let p of n?.bulletTags??[]){let u=p.tag==="harmful"?"harmful":"primary";l(p.id,u)}if(r?.bulletIds)for(let p of r.bulletIds)l(p,"generator");let c=p=>{let u=a.get(p);if(!u)return this.locateFallbackBullet(t,p,s);let d=g=>{for(;g.length>0;){let f=g.shift();if(!s.has(f))return f}},m=d(u.harmful)??d(u.primary)??d(u.generator);return m||this.locateFallbackBullet(t,p,s)};for(let p of e){if((p.type==="UPDATE"||p.type==="REMOVE")&&!p.bulletId){let u=c(p.section);u&&(p.bulletId=u,s.add(u))}(p.type==="UPDATE"||p.type==="REMOVE")&&!p.bulletId||o.push(p)}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",c=typeof l=="string"&&l.trim().length>0?l.trim():"Guidelines",p=r.content??"",u=typeof p=="string"?p.trim():"";if(a!=="REMOVE"&&u.length===0)continue;let d=r.bulletId??r.id,m=typeof d=="string"&&d.trim().length>0?d.trim():void 0,f=[a,c,u,m??""].join(":");if(n.has(f))continue;n.add(f);let h={type:a,section:c};a!=="REMOVE"&&(h.content=u),m&&(h.bulletId=m);let A=r.metadata;A&&typeof A=="object"&&(h.metadata={...A}),t.push(h)}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 c=`${o}:${a}`;n.has(c)||(n.add(c),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};return await o.forward(s,{question:JSON.stringify(e),generator_answer:JSON.stringify(t.answer),generator_reasoning:t.reasoning,playbook:JSON.stringify({markdown:Gn(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({example:e,reflection:t,playbook:n}){if(!t)return;let r=this.getOrCreateCuratorProgram(),o=this.teacherAI??this.studentAI;try{return await r.forward(o,{playbook:JSON.stringify({markdown:Gn(n),structured:n}),reflection:JSON.stringify(t),question_context:JSON.stringify(e),token_budget:1024})}catch(s){this.verbose&&console.warn("[AxACE] Curator error:",s instanceof Error?s.message:s);return}}getOrCreateReflectorProgram(){if(!this.reflectorProgram){let e=ne().input("question",ne.string("Original task input serialized as JSON")).input("generator_answer",ne.string("Generator output serialized as JSON")).input("generator_reasoning",ne.string("Generator reasoning trace").optional()).input("playbook",ne.string("Current context playbook rendered as markdown")).input("expected_answer",ne.string("Expected output when ground truth is available").optional()).input("feedback",ne.string("External feedback or reward signal").optional()).input("previous_reflection",ne.string("Most recent reflection JSON when running multi-round refinement").optional()).output("reasoning",ne.string("Step-by-step analysis of generator performance")).output("errorIdentification",ne.string("Specific mistakes detected")).output("rootCauseAnalysis",ne.string("Underlying cause of the error")).output("correctApproach",ne.string("What the generator should do differently")).output("keyInsight",ne.string("Reusable insight to remember")).output("bulletTags",ne.json("Array of {id, tag} entries referencing playbook bullets")).build();this.reflectorProgram=ge(e)}return this.reflectorProgram}getOrCreateCuratorProgram(){if(!this.curatorProgram){let e=ne().input("playbook",ne.string("Current playbook serialized as JSON")).input("reflection",ne.string("Latest reflection output serialized as JSON")).input("question_context",ne.string("Original task input serialized as JSON")).input("token_budget",ne.number("Approximate token budget for curator response").optional()).output("reasoning",ne.string("Justification for the proposed updates")).output("operations",ne.json("List of operations with type/section/content fields")).build();this.curatorProgram=ge(e)}return this.curatorProgram}};var Xt=class extends Se{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 c=jc([...t],this.maxExamples),p=this.traces.length;for(let u=0;u<c.length;u+=this.batchSize){u>0&&(l.modelConfig.temperature=.7+.001*u);let d=c.slice(u,u+this.batchSize);for(let m of d){if(!m||typeof m!="object")continue;let g=t.filter(A=>A!==m);e.setExamples(g);let f=this.getTeacherOrStudentAI();this.stats.totalCalls++;let h;try{let A={...l,maxRetries:1};h=await e.forward(f,m,A),this.costMonitoring&&(this.stats.estimatedTokenUsage+=JSON.stringify(m).length/4+JSON.stringify(h).length/4),await r({prediction:h,example:m})>=.5&&(this.traces=[...this.traces,...e.getTraces()],this.stats.successfulDemos++)}catch(A){(this.verboseMode||this.debugMode)&&console.warn(`Student model failed during bootstrap: ${A instanceof Error?A.message:"Unknown error"}`),h={}}if(this.traces.length>=a)return}}if(this.earlyStoppingPatience>0){let d=this.traces.length-p;if(!this.stats.earlyStopping)this.stats.earlyStopping={bestScoreRound:d>0?n:0,patienceExhausted:!1,reason:"No improvement detected"};else if(d>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=zc(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 zc(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 jc=(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)};function Ja(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 Ve(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(Ja(i[s].scores,i[n].scores,e)){o=!0;break}Ja(i[n].scores,i[s].scores,e)&&r++}o||t.push({idx:i[n].idx,scores:i[n].scores,dominated:r})}return t}function Fe(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,c=a[n]??0,p=Math.max(c-s,0);o+=l*p,s=Math.max(s,c)}return o}function yt(i){if(i.length===0)return 0;let e=0;for(let t of i)e+=t;return e/i.length}function ro(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 Un(i,e){let t=new Set;for(let p of i)for(let u of p)t.add(u);let r=[...Array.from(t)].sort((p,u)=>(e[p]??0)-(e[u]??0)),o=new Set,s=(p,u)=>{for(let d of i){if(!d.has(p))continue;let m=!1;for(let g of u)if(d.has(g)){m=!0;break}if(!m)return!1}return!0},a=!0;for(;a;){a=!1;for(let p of r){if(o.has(p))continue;let u=new Set(r.filter(d=>d!==p&&!o.has(d)));if(s(p,u)){o.add(p),a=!0;break}}}let l=r.filter(p=>!o.has(p)),c=new Set(l);return i.map(p=>{let u=new Set;for(let d of p)c.has(d)&&u.add(d);return u})}function Bn(i,e,t){let n=Un(i,e),r={};for(let l of n)for(let c of l)r[c]=(r[c]||0)+1;let o=[];for(let[l,c]of Object.entries(r)){let p=Number(l);for(let u=0;u<c;u++)o.push(p)}if(o.length===0)return 0;let s=typeof t=="function"?t():Math.random(),a=Math.floor(s*o.length);return o[a]}var oo=class extends Se{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;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??0)),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),c=a&&a.length>0?a:t,p=async(R,M)=>{try{e.setInstruction?.(R);let F=await e.forward(this.studentAI,M,{sampleCount:this.sampleCount});return this.stats.totalCalls+=1,await n({prediction:F,example:M})||{}}catch{return{}}},u=async(R,M)=>{let F=[];for(let D of M)F.push(await p(R,D));return ro(F)},d=await this.getBaseInstruction(e),m=[{instruction:d,parent:void 0,scores:await u(d,l)}],g=R=>{let M=r?.paretoMetricKey,F=r?.paretoScalarize;if(typeof F=="function")return F(R);if(M)return Number.isFinite(R[M])?R[M]:0;let D=Object.values(R);return D.length?D.reduce((X,W)=>X+W,0)/D.length:0},f=[],h=async(R,M)=>{let F=[];for(let D of M){let X=await p(R,D);F.push(g(X))}return F};f.push(await h(d,l));let A=()=>{let R=f[0]?.length??0,M=[];for(let D=0;D<R;D++){let X=Number.NEGATIVE_INFINITY,W=new Set;for(let xe=0;xe<f.length;xe++){let pe=f[xe][D];pe>X+this.tieEpsilon?(X=pe,W.clear(),W.add(xe)):Math.abs(pe-X)<=this.tieEpsilon&&W.add(xe)}M.push(W)}let F=f.map(D=>yt(D));return Bn(M,F)};this.getOptimizerLogger(r)?.({name:"OptimizationStart",value:{optimizerType:"GEPA",exampleCount:t.length,validationCount:l.length,config:{numTrials:this.numTrials,minibatch:this.minibatch}}});let y=0,b=Ve(m.map((R,M)=>({idx:M,scores:R.scores})),this.tieEpsilon).map(R=>R.idx),S,I=r?.maxMetricCalls;if(!Number.isFinite(I)||I<=0)throw new Error("AxGEPA: options.maxMetricCalls must be set to a positive integer");let T=Math.floor(I);for(let R=0;R<this.numTrials&&!(T!==void 0&&this.stats.totalCalls>=Math.max(1,Math.floor(T)));R++){let M=f[0]?.length??0,F=[];for(let v=0;v<M;v++){let $=Number.NEGATIVE_INFINITY,z=new Set;for(let j=0;j<f.length;j++){let H=f[j][v];H>$+this.tieEpsilon?($=H,z.clear(),z.add(j)):Math.abs(H-$)<=this.tieEpsilon&&z.add(j)}F.push(z)}let D=f.map(v=>yt(v));if(this.mergeMax>0&&this.mergesDue>0&&this.lastIterFoundNewProgram){let v=P=>{let O=[],_=P;for(;_!==void 0;)O.push(_),_=m[_]?.parent;return O},$=P=>P.length?P[Math.floor(this.rand()*P.length)]:void 0,z=Un(F,D),j=new Set;for(let P of z)for(let O of P)j.add(O);let H=Array.from(j),ee;for(let P=0;P<10&&!ee&&!(H.length<2);P++){let O=$(H),_=$(H);if(O===_)continue;_<O&&([O,_]=[_,O]);let Y=new Set(v(O)),le=new Set(v(_));if(Y.has(_)||le.has(O))continue;let Z=[...Y].filter(ie=>le.has(ie));if(Z.length===0)continue;let K=Z.map(ie=>Math.max(1e-9,D[ie])),V=this.rand()*K.reduce((ie,Ne)=>ie+Ne,0),J=Z[Z.length-1];for(let ie=0;ie<Z.length;ie++){if(V<K[ie]){J=Z[ie];break}V-=K[ie]}ee={i:O,j:_,a:J}}if(this.lastIterFoundNewProgram=!1,ee){let{i:P,j:O,a:_}=ee,Y=D[_],le=D[P],Z=D[O],K=m[_].instruction,V=m[P].instruction,J=m[O].instruction,ie=V===K&&J!==V||J===K&&V!==J,Ne=Y<=Math.min(le,Z)&&ie,ve="",Oe="i",et=!1;if(Ne){let Je=`${P}|${O}|${_}`;if(this.mergeAttemptKeys.has(Je))Ne=!1;else{V===K&&J!==V?(ve=J,Oe="j"):J===K&&V!==J?(ve=V,Oe="i"):V!==K&&J!==K&&V!==J?le>Z||le===Z&&this.rand()<.5?(ve=V,Oe="i"):(ve=J,Oe="j"):(ve=V,Oe="i");let oe=`${Math.min(P,O)}|${Math.max(P,O)}|${Oe}`;if(this.mergeCompositionKeys.has(oe))Ne=!1;else{this.mergeAttemptKeys.add(Je),this.mergeCompositionKeys.add(oe);let De=f[P],Qe=f[O],ke=Array.from({length:De.length},(te,be)=>be),tt=ke.filter(te=>(De[te]??0)>(Qe[te]??0)),bt=ke.filter(te=>(Qe[te]??0)>(De[te]??0)),lt=ke.filter(te=>!(tt.includes(te)||bt.includes(te))),Ye=5,nt=Math.ceil(Ye/3),B=(te,be)=>{if(be<=0||te.length===0)return[];if(te.length<=be)return[...te];let ln=[],Kn=new Set;for(;ln.length<be;){let $e=Math.floor(this.rand()*te.length);Kn.has($e)||(Kn.add($e),ln.push(te[$e]))}return ln},re=[];re.push(...B(tt,Math.min(nt,tt.length))),re.push(...B(bt,Math.min(nt,bt.length)));let ct=Ye-re.length;re.push(...B(lt,Math.max(0,ct)));let pt=Ye-re.length;if(pt>0){let te=ke.filter(be=>!re.includes(be));re.push(...B(te,Math.min(pt,te.length)))}let ye=re.slice(0,Math.min(Ye,ke.length)),Ro=ye.map(te=>l[te]);et=!0;let ml=(await h(ve,Ro)).reduce((te,be)=>te+be,0),gl=ye.reduce((te,be)=>te+(De[be]??0),0),hl=ye.reduce((te,be)=>te+(Qe[be]??0),0);if(ml>=Math.max(gl,hl)+this.tieEpsilon){let te=await u(ve,l);m.push({instruction:ve,parent:_,scores:te}),f.push(await h(ve,l));let be=b.length,ln=Fe(b.map($e=>m[$e].scores))??0;b=Ve(m.map(($e,fl)=>({idx:fl,scores:$e.scores})),this.tieEpsilon).map($e=>$e.idx);let Kn=Fe(b.map($e=>m[$e].scores))??0;(b.length>be||Kn>ln+1e-6)&&(y=0),this.mergesDue-=1,this.totalMergesTested+=1}}}}if(et)continue}}let X=Bn(F,D,()=>this.rand()),W=this.minibatch?this.nextMinibatchIndices(c.length,R).map(v=>c[v]):c;if(r?.skipPerfectScore??!0){let v=Number(r?.perfectScore??1),$=await h(m[X].instruction,W);if($.length>0&&$.every(z=>z>=v))continue}let xe=!1,pe=m[X].instruction,he="reflective_mutation",_e,Ze;if(xe){let v=(X+1)%m.length;pe=await this.mergeInstructions(m[X].instruction,m[v].instruction,r),he="merge",this.mergesUsed+=1}else{let v=r?.gepaAdapter;if(v){try{let $={instruction:m[X].instruction},z=await v.evaluate(W,$,!0);_e=Array.isArray(z?.scores)?z.scores.reduce((P,O)=>P+(Number(O)||0),0):void 0;let j=v.make_reflective_dataset($,z,["instruction"]),H=await v.propose_new_texts?.($,j,["instruction"]),ee=H?.instruction??(H?Object.values(H)[0]:void 0);typeof ee=="string"&&ee.length>0?pe=ee:pe=await this.reflectInstruction(m[X].instruction,e,W,async({prediction:P,example:O})=>{let _=await n({prediction:P,example:O}),Y=Object.values(_||{});return Y.length?Y.reduce((le,Z)=>le+Z,0)/Y.length:0},r)}catch{pe=await this.reflectInstruction(m[X].instruction,e,W,async({prediction:$,example:z})=>{let j=await n({prediction:$,example:z}),H=Object.values(j||{});return H.length?H.reduce((ee,P)=>ee+P,0)/H.length:0},r)}if(_e!==void 0)try{let $=await v.evaluate(W,{instruction:pe},!1);Ze=Array.isArray($?.scores)?$.scores.reduce((z,j)=>z+(Number(j)||0),0):void 0}catch{}}else pe=await this.reflectInstruction(m[X].instruction,e,W,async({prediction:$,example:z})=>{let j=await n({prediction:$,example:z}),H=Object.values(j||{});return H.length?H.reduce((ee,P)=>ee+P,0)/H.length:0},r)}let Ao=await h(m[X].instruction,W),xo=await h(pe,W),yo=Ao.reduce((v,$)=>v+$,0),at=xo.reduce((v,$)=>v+$,0);if(this.currentRound=R+1,await this.updateOptimizationProgress(this.currentRound,at,{instructionLen:pe.length,parent:X,totalRounds:this.numTrials},"GEPA",{strategy:he,paretoSetSize:l.length},at,{instructionLen:m[X].instruction.length,idx:X},{...r??{},maxIterations:this.numTrials}),!(at>yo+this.tieEpsilon&&(_e===void 0||Ze===void 0||Ze>_e+this.tieEpsilon))){if(++y>=this.earlyStoppingTrials)break;continue}let bo=await u(pe,l);m.push({instruction:pe,parent:X,scores:bo}),f.push(await h(pe,l));let Io=b.length,To=Fe(b.map(v=>m[v].scores))??0;b=Ve(m.map((v,$)=>({idx:$,scores:v.scores})),this.tieEpsilon).map(v=>v.idx);let Co=Fe(b.map(v=>m[v].scores))??0;if(b.length>Io||Co>To+1e-6)y=0;else if(y++,y>=this.earlyStoppingTrials)break;this.lastIterFoundNewProgram=!0,this.mergeMax>0&&this.totalMergesTested<this.mergeMax&&(this.mergesDue+=1)}let C=Ve(m.map((R,M)=>({idx:M,scores:R.scores})),this.tieEpsilon),N=C.length>0?Math.max(...C.map(R=>g(R.scores))):0,U;if(C.length>0){let R=Number.NEGATIVE_INFINITY;for(let M of C){let F=g(M.scores);F>R&&(R=F,U=M.idx)}}let k=Fe(C.map(R=>R.scores));this.stats.convergenceInfo.converged=!0,this.recordParetoMetrics(C.length,m.length,"GEPA",k);let w=Date.now()-o,E=typeof U=="number"?new We({bestScore:N,stats:this.stats,instruction:m[U].instruction,demos:[],examples:t,modelConfig:void 0,optimizerType:"GEPA",optimizationTime:w,totalRounds:this.numTrials,converged:this.stats.convergenceInfo.converged}):void 0;return this.generateOptimizationReport(C,k,N),{demos:[],stats:this.stats,bestScore:N,paretoFront:C.map(R=>({demos:[],scores:R.scores,configuration:{candidate:R.idx},dominatedSolutions:R.dominated})),paretoFrontSize:C.length,hypervolume:k,finalConfiguration:{strategy:"gepa",candidates:m.length},optimizedProgram:E}}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){try{let t=e.getSignature?.();if(t&&typeof t.instruction=="string"&&t.instruction.length>0)return t.instruction}catch{}return"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?yt(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){let s=[];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 f=await r({prediction:g,example:m});s.push({input:m,prediction:g,score:typeof f=="number"?f:0})}catch{s.push({input:m,prediction:{},score:0})}let a=o?.overrideTeacherAI??this.teacherAI??this.studentAI,l=ge('minibatch:json "Array of {input,prediction,score}", evalFeedback?:string[] "Evaluator feedback per case if available" -> feedbackSummary:string "Concise feedback: common errors, missing constraints, desired changes"'),c=[],p=o?.feedbackFn;if(typeof p=="function")for(let m=0;m<s.length;m++)try{let g=p({prediction:s[m].prediction,example:s[m].input});g&&(Array.isArray(g)?c.push(...g):c.push(g))}catch{}let u="";try{u=(await l.forward(a,{minibatch:s,evalFeedback:c}))?.feedbackSummary?.trim()||"",u&&(this.feedbackMemory.unshift(u),this.feedbackMemory.length>this.feedbackMemorySize&&this.feedbackMemory.pop())}catch{}let d=ge('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(a,{currentInstruction:e,feedbackSummary:u,recentFeedback:this.feedbackMemory,minibatch:s}))?.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)}updateSamplerShuffled(e){let t=Array.from({length:e},(l,c)=>c);for(let l=t.length-1;l>0;l--){let c=Math.floor(this.rand()*(l+1));[t[l],t[c]]=[t[c],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,c)=>c).sort((l,c)=>(this.samplerState.freq.get(l)??0)-(this.samplerState.freq.get(c)??0)),a=[...t];for(let l=0;l<o;l++){let c=s[l%s.length];a.push(c),this.samplerState.freq.set(c,(this.samplerState.freq.get(c)??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){console.log(`
315
321
  \u{1F389} GEPA Multi-Objective Optimization Complete!
316
322
  `),console.log("\u2705 Improvements:"),e.length>1?console.log("\u2022 Successfully found multiple Pareto-optimal solutions"):console.log("\u2022 Found at least one optimal solution"),t!==void 0&&t>0&&console.log(`\u2022 Hypervolume improvement: ${(t*100).toFixed(1)}%`),n!==void 0&&console.log(`\u2022 Best score achieved: ${n.toFixed(3)}`),console.log(`\u2022 Multi-objective approach balances competing goals
317
323
  `),console.log("\u26A0\uFE0F Limitations:"),e.length===1&&console.log("\u2022 Limited diversity in Pareto frontier"),this.stats.totalCalls<100&&console.log("\u2022 Relatively few optimization trials performed"),console.log("\u2022 Results depend on training data quality and size"),console.log(`\u2022 Optimization time scales with problem complexity
@@ -319,7 +325,7 @@ ${t}`.trim()}function Uc(i){let e=i.toLowerCase().replace(/[^a-z0-9]+/g,"-").rep
319
325
  `),console.log("\u{1F4A1} What This Means:"),console.log("\u2022 GEPA framework successfully demonstrates multi-objective optimization"),console.log("\u2022 Pareto frontier reveals real trade-offs between objectives"),console.log("\u2022 Users can select solutions based on their specific priorities"),console.log("\u2022 More training data and trials would likely improve results")}async mergeInstructions(e,t,n){let r=n?.overrideTeacherAI??this.teacherAI??this.studentAI,o=ge(`instructionA:string "Parent A instruction",
320
326
  instructionB:string "Parent B instruction",
321
327
  recentFeedback?:string[] "Past feedback memory"
322
- -> 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)}};var so=class extends Se{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,c=r?.feedbackExamples,p=(l&&l.length>0?l:t).slice(0,this.paretoSetSize),u=c&&c.length>0?c:t;this.getOptimizerLogger(r)?.({name:"OptimizationStart",value:{optimizerType:"GEPA-Flow",exampleCount:t.length,validationCount:p.length,config:{numTrials:this.numTrials,minibatch:this.minibatch}}});let m=async(w,O)=>{try{s.setAllNodeInstructions?.(w);let R=await s.forward(this.studentAI,O,{sampleCount:this.sampleCount});return this.stats.totalCalls+=1,await n({prediction:R,example:O})||{}}catch{return{}}},g=async(w,O)=>{let R=[];for(let v of O)R.push(await m(w,v));return ro(R)},f={};for(let w of a)f[w.name]=await this.getBaseInstruction(w.program);let h=[{cfg:{...f},parent:void 0,scores:await g(f,p)}],A=w=>{let O=r?.paretoMetricKey,R=r?.paretoScalarize;if(typeof R=="function")return R(w);if(O)return Number.isFinite(w[O])?w[O]:0;let v=Object.values(w);return v.length?v.reduce((k,G)=>k+G,0)/v.length:0},x=[],y=async(w,O)=>{let R=[];for(let v of O){let k=await m(w,v);R.push(A(k))}return R};x.push(await y(f,p));let b=Ve(h.map((w,O)=>({idx:O,scores:w.scores})),this.tieEpsilon).map(w=>w.idx),S=0,I=new Set,T=r?.maxMetricCalls;if(!Number.isFinite(T)||T<=0)throw new Error("AxGEPA-Flow: options.maxMetricCalls must be set to a positive integer");let C=Math.floor(T);for(let w=0;w<this.numTrials&&!(C!==void 0&&this.stats.totalCalls>=Math.max(1,Math.floor(C)));w++){let O=x[0]?.length??0,R=[];for(let M=0;M<O;M++){let D=Number.NEGATIVE_INFINITY,z=new Set;for(let j=0;j<x.length;j++){let H=x[j][M];H>D+this.tieEpsilon?(D=H,z.clear(),z.add(j)):Math.abs(H-D)<=this.tieEpsilon&&z.add(j)}R.push(z)}let v=x.map(M=>yt(M));if(this.mergeMax>0&&this.mergesDue>0&&this.lastIterFoundNewProgram){let M=Un(R,v),D=new Set;for(let F of M)for(let E of F)D.add(E);let z=Array.from(D),j=F=>{let E=[],_=F;for(;_!==void 0;)E.push(_),_=h[_]?.parent;return E},H=F=>F.length?F[Math.floor(this.rand()*F.length)]:void 0,ee;for(let F=0;F<10&&!ee&&!(z.length<2);F++){let E=H(z),_=H(z);if(E===_)continue;_<E&&([E,_]=[_,E]);let Y=new Set(j(E)),le=new Set(j(_));if(Y.has(_)||le.has(E))continue;let Z=[...Y].filter(oe=>le.has(oe));if(Z.length===0)continue;let K=[];for(let oe of Z){let De=h[oe].cfg,Qe=h[E].cfg,ke=h[_].cfg,tt=!1,bt=new Set([...Object.keys(De),...Object.keys(Qe),...Object.keys(ke)]);for(let lt of bt){let Ye=De[lt],nt=Qe[lt],B=ke[lt];if(nt===Ye&&B!==nt||B===Ye&&nt!==B){tt=!0;break}}tt&&K.push(oe)}if(K.length===0)continue;let W=K.map(oe=>Math.max(1e-9,v[oe])),V=this.rand()*W.reduce((oe,De)=>oe+De,0),ie=K[K.length-1];for(let oe=0;oe<K.length;oe++){if(V<W[oe]){ie=K[oe];break}V-=W[oe]}let Ne=v[ie],ve=v[E],Oe=v[_];if(Ne>Math.min(ve,Oe))continue;let et=`${E}|${_}|${ie}`;if(this.mergeAttemptKeys.has(et))continue;this.mergeAttemptKeys.add(et);let Je=`${E}|${_}|${ie}`;I.has(Je)||(ee={i:E,j:_,a:ie})}if(this.lastIterFoundNewProgram=!1,ee){let{i:F,j:E,a:_}=ee,{cfg:Y,descSig:le}=this.systemAwareMergeWithSig(h,F,E,(B,re)=>v[B]>=v[re]?B:re),Z=`${Math.min(F,E)}|${Math.max(F,E)}|${le}`;if(this.mergeCompositionKeys.has(Z))continue;this.mergeCompositionKeys.add(Z);let K=x[F],W=x[E],V=Array.from({length:K.length},(B,re)=>re),ie=V.filter(B=>(K[B]??0)>(W[B]??0)),Ne=V.filter(B=>(W[B]??0)>(K[B]??0)),ve=V.filter(B=>!(ie.includes(B)||Ne.includes(B))),Oe=5,et=Math.ceil(Oe/3),Je=(B,re)=>{if(re<=0||B.length===0)return[];if(B.length<=re)return[...B];let ct=[],pt=new Set;for(;ct.length<re;){let ye=Math.floor(this.rand()*B.length);pt.has(ye)||(pt.add(ye),ct.push(B[ye]))}return ct},oe=[];oe.push(...Je(ie,Math.min(et,ie.length))),oe.push(...Je(Ne,Math.min(et,Ne.length)));let De=Oe-oe.length;oe.push(...Je(ve,Math.max(0,De)));let Qe=Oe-oe.length;if(Qe>0){let B=V.filter(re=>!oe.includes(re));oe.push(...Je(B,Math.min(Qe,B.length)))}let ke=oe.slice(0,Math.min(Oe,V.length)),tt=ke.map(B=>p[B]),lt=(await y(Y,tt)).reduce((B,re)=>B+re,0),Ye=ke.reduce((B,re)=>B+(K[re]??0),0),nt=ke.reduce((B,re)=>B+(W[re]??0),0);if(lt>=Math.max(Ye,nt)+this.tieEpsilon){let B=await g(Y,p);h.push({cfg:Y,parent:_,scores:B}),x.push(await y(Y,p));let re=b.length,ct=Fe(b.map(ye=>h[ye].scores))??0;b=Ve(h.map((ye,Ro)=>({idx:Ro,scores:ye.scores})),this.tieEpsilon).map(ye=>ye.idx);let pt=Fe(b.map(ye=>h[ye].scores))??0;(b.length>re||pt>ct+1e-6)&&(S=0),this.mergesDue-=1,this.totalMergesTested+=1,I.add(`${Math.min(F,E)}|${Math.max(F,E)}|${_}`)}continue}}let k=Bn(R,v,()=>this.rand());this.lastIterFoundNewProgram=!1;let G=this.minibatch?this.nextMinibatchIndices(u.length,w).map(M=>u[M]):u;if(r?.skipPerfectScore??!0){let M=Number(r?.perfectScore??1),D=await y(h[k].cfg,G);if(D.length>0&&D.every(z=>z>=M))continue}let X=!1,J={...h[k].cfg},xe="reflective_mutation",pe=w%a.length,he=a[pe],_e,Ze;if(X&&this.mergesUsed<this.mergeMax){let M=(k+1)%h.length,D=F=>{let E=[],_=F;for(;_!==void 0;)E.push(_),_=h[_]?.parent;return E},z=D(k),j=D(M),H=z.find(F=>j.includes(F)),ee=!0;if(H||(ee=!1),(j.includes(k)||z.includes(M))&&(ee=!1),ee){let F=h[H].cfg,E=h[k].cfg,_=h[M].cfg,Y=!1,le=new Set([...Object.keys(F),...Object.keys(E),...Object.keys(_)]);for(let Z of le){let K=F[Z],W=E[Z],V=_[Z];if(W===K&&V!==W||V===K&&W!==V){Y=!0;break}}Y||(ee=!1)}if(ee){let F=Math.min(k,M),E=Math.max(k,M),_=`${F}|${E}|${H}`;if(!I.has(_)){let Y=A(h[H].scores),le=A(h[k].scores),Z=A(h[M].scores);Y<=Math.min(le,Z)&&(J=this.systemAwareMerge(h,k,M,(K,W)=>{let V=A(h[K].scores),ie=A(h[W].scores);return V>=ie?K:W}),xe="system_merge",this.mergesUsed+=1,I.add(_))}}else{let F=h[k].cfg[he.name],E=r?.gepaAdapter,_;if(E)try{let Y=await E.evaluate(G,{...h[k].cfg},!0);_e=Array.isArray(Y?.scores)?Y.scores.reduce((W,V)=>W+(Number(V)||0),0):void 0;let le=E.make_reflective_dataset({...h[k].cfg},Y,[he.name]),K=(await E.propose_new_texts?.({...h[k].cfg},le,[he.name]))?.[he.name];typeof K=="string"&&K.length>0&&(_=K)}catch{}if(_||(_=await this.reflectModuleInstruction(he.name,F,s,a,{...h[k].cfg},G,async({prediction:Y,example:le})=>{let Z=await n({prediction:Y,example:le}),K=Object.values(Z||{});return K.length?K.reduce((W,V)=>W+V,0)/K.length:0},r)),J[he.name]=_,E&&_e!==void 0)try{let Y=await E.evaluate(G,J,!1);Ze=Array.isArray(Y?.scores)?Y.scores.reduce((le,Z)=>le+(Number(Z)||0),0):void 0}catch{}}}else{let M=h[k].cfg[he.name],D=r?.gepaAdapter,z;if(D)try{let j=await D.evaluate(G,{...h[k].cfg},!0);_e=Array.isArray(j?.scores)?j.scores.reduce((E,_)=>E+(Number(_)||0),0):void 0;let H=D.make_reflective_dataset({...h[k].cfg},j,[he.name]),F=(await D.propose_new_texts?.({...h[k].cfg},H,[he.name]))?.[he.name];typeof F=="string"&&F.length>0&&(z=F)}catch{}if(z||(z=await this.reflectModuleInstruction(he.name,M,s,a,{...h[k].cfg},G,async({prediction:j,example:H})=>{let ee=await n({prediction:j,example:H}),F=Object.values(ee||{});return F.length?F.reduce((E,_)=>E+_,0)/F.length:0},r)),J[he.name]=z,D&&_e!==void 0)try{let j=await D.evaluate(G,J,!1);Ze=Array.isArray(j?.scores)?j.scores.reduce((H,ee)=>H+(Number(ee)||0),0):void 0}catch{}}let Ao=await y(h[k].cfg,G),xo=await y(J,G),yo=Ao.reduce((M,D)=>M+D,0),at=xo.reduce((M,D)=>M+D,0);if(this.currentRound=w+1,await this.updateOptimizationProgress(this.currentRound,at,{modules:a.length,mutatedModule:he.name,totalRounds:this.numTrials},"GEPA-Flow",{strategy:xe,paretoSetSize:p.length},at,{idx:k},{...r??{},maxIterations:this.numTrials}),!(at>yo+this.tieEpsilon&&(_e===void 0||Ze===void 0||Ze>_e+this.tieEpsilon))){if(++S>=this.earlyStoppingTrials)break;continue}let bo=await g(J,p);h.push({cfg:J,parent:k,scores:bo}),x.push(await y(J,p));let Io=b.length,To=Fe(b.map(M=>h[M].scores))??0;b=Ve(h.map((M,D)=>({idx:D,scores:M.scores})),this.tieEpsilon).map(M=>M.idx);let Co=Fe(b.map(M=>h[M].scores))??0;if(b.length>Io||Co>To+1e-6)S=0;else if(S++,S>=this.earlyStoppingTrials)break;this.lastIterFoundNewProgram=!0,this.mergeMax>0&&this.totalMergesTested<this.mergeMax&&(this.mergesDue+=1)}let N=Ve(h.map((w,O)=>({idx:O,scores:w.scores})),this.tieEpsilon),U=N.length>0?Math.max(...N.map(w=>A(w.scores))):0,P=Fe(N.map(w=>w.scores));return this.stats.convergenceInfo.converged=!0,this.recordParetoMetrics(N.length,h.length,"GEPA-Flow",P),{demos:[],stats:this.stats,bestScore:U,paretoFront:N.map(w=>({demos:[],scores:w.scores,configuration:{candidate:w.idx},dominatedSolutions:w.dominated})),paretoFrontSize:N.length,hypervolume:P,finalConfiguration:{strategy:"gepa_flow_pareto",candidates:h.length}}}async getBaseInstruction(e){try{let t=e?.getSignature?.();if(t&&typeof t.instruction=="string"&&t.instruction.length>0)return t.instruction}catch{}return"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?yt(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 c=[],p=[];for(let A of s)try{o[e]=t,n.setAllNodeInstructions?.(o);let x=await n.forward(this.studentAI,A,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let y=await a({prediction:x,example:A});p.push({input:A,prediction:x,score:typeof y=="number"?y:0})}catch{p.push({input:A,prediction:{},score:0})}let u=l?.overrideTeacherAI??this.teacherAI??this.studentAI,d=ge('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 A of p){let x=g({prediction:A.prediction,example:A.input});x&&(Array.isArray(x)?m.push(...x):m.push(x))}let f="";try{f=(await d.forward(u,{moduleName:e,minibatch:p,evalFeedback:m}))?.feedbackSummary?.trim()||""}catch{}let h=ge('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 h.forward(u,{moduleName:e,currentInstruction:t,feedbackSummary:f,minibatch:p}))?.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,c)=>c);for(let l=t.length-1;l>0;l--){let c=Math.floor(this.rand()*(l+1));[t[l],t[c]]=[t[c],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,c)=>c).sort((l,c)=>(this.samplerState.freq.get(l)??0)-(this.samplerState.freq.get(c)??0)),a=[...t];for(let l=0;l<o;l++){let c=s[l%s.length];a.push(c),this.samplerState.freq.set(c,(this.samplerState.freq.get(c)??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=h=>{let A=[],x=h;for(;x!==void 0;)A.push(x),x=e[x]?.parent;return A},s=o(t),a=o(n),c=s.find(h=>a.includes(h))??t,p=e[c].cfg,u=e[t].cfg,d=e[n].cfg,m={},g=[],f=Array.from(new Set([...Object.keys(p),...Object.keys(u),...Object.keys(d)])).sort();for(let h of f){let A=p[h],x=u[h],y=d[h];if(x===A&&y!==x)m[h]=y,g.push("j");else if(y===A&&x!==y)m[h]=x,g.push("i");else if(x!==y&&x!==A&&y!==A){let b=r(t,n);m[h]=b===t?x:y,g.push(b===t?"i":"j")}else m[h]=x??y??A,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=f=>{let h=[],A=f;for(;A!==void 0;)h.push(A),A=e[A]?.parent;return h},s=o(t),a=o(n),c=s.find(f=>a.includes(f))??t,p=e[c].cfg,u=e[t].cfg,d=e[n].cfg,m={},g=new Set([...Object.keys(p),...Object.keys(u),...Object.keys(d)]);for(let f of g){let h=p[f],A=u[f],x=d[f];if(A===h&&x!==A)m[f]=x;else if(x===h&&A!==x)m[f]=A;else if(A!==x&&A!==h&&x!==h){let y=r(t,n);m[f]=y===t?A:x}else m[f]=A??x??h}return m}};var io=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 ao=class extends Se{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 io(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=`
328
+ -> 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)}};var so=class extends Se{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,c=r?.feedbackExamples,p=(l&&l.length>0?l:t).slice(0,this.paretoSetSize),u=c&&c.length>0?c:t;this.getOptimizerLogger(r)?.({name:"OptimizationStart",value:{optimizerType:"GEPA-Flow",exampleCount:t.length,validationCount:p.length,config:{numTrials:this.numTrials,minibatch:this.minibatch}}});let m=async(w,E)=>{try{s.setAllNodeInstructions?.(w);let R=await s.forward(this.studentAI,E,{sampleCount:this.sampleCount});return this.stats.totalCalls+=1,await n({prediction:R,example:E})||{}}catch{return{}}},g=async(w,E)=>{let R=[];for(let M of E)R.push(await m(w,M));return ro(R)},f={};for(let w of a)f[w.name]=await this.getBaseInstruction(w.program);let h=[{cfg:{...f},parent:void 0,scores:await g(f,p)}],A=w=>{let E=r?.paretoMetricKey,R=r?.paretoScalarize;if(typeof R=="function")return R(w);if(E)return Number.isFinite(w[E])?w[E]:0;let M=Object.values(w);return M.length?M.reduce((F,D)=>F+D,0)/M.length:0},x=[],y=async(w,E)=>{let R=[];for(let M of E){let F=await m(w,M);R.push(A(F))}return R};x.push(await y(f,p));let b=Ve(h.map((w,E)=>({idx:E,scores:w.scores})),this.tieEpsilon).map(w=>w.idx),S=0,I=new Set,T=r?.maxMetricCalls;if(!Number.isFinite(T)||T<=0)throw new Error("AxGEPA-Flow: options.maxMetricCalls must be set to a positive integer");let C=Math.floor(T);for(let w=0;w<this.numTrials&&!(C!==void 0&&this.stats.totalCalls>=Math.max(1,Math.floor(C)));w++){let E=x[0]?.length??0,R=[];for(let v=0;v<E;v++){let $=Number.NEGATIVE_INFINITY,z=new Set;for(let j=0;j<x.length;j++){let H=x[j][v];H>$+this.tieEpsilon?($=H,z.clear(),z.add(j)):Math.abs(H-$)<=this.tieEpsilon&&z.add(j)}R.push(z)}let M=x.map(v=>yt(v));if(this.mergeMax>0&&this.mergesDue>0&&this.lastIterFoundNewProgram){let v=Un(R,M),$=new Set;for(let P of v)for(let O of P)$.add(O);let z=Array.from($),j=P=>{let O=[],_=P;for(;_!==void 0;)O.push(_),_=h[_]?.parent;return O},H=P=>P.length?P[Math.floor(this.rand()*P.length)]:void 0,ee;for(let P=0;P<10&&!ee&&!(z.length<2);P++){let O=H(z),_=H(z);if(O===_)continue;_<O&&([O,_]=[_,O]);let Y=new Set(j(O)),le=new Set(j(_));if(Y.has(_)||le.has(O))continue;let Z=[...Y].filter(oe=>le.has(oe));if(Z.length===0)continue;let K=[];for(let oe of Z){let De=h[oe].cfg,Qe=h[O].cfg,ke=h[_].cfg,tt=!1,bt=new Set([...Object.keys(De),...Object.keys(Qe),...Object.keys(ke)]);for(let lt of bt){let Ye=De[lt],nt=Qe[lt],B=ke[lt];if(nt===Ye&&B!==nt||B===Ye&&nt!==B){tt=!0;break}}tt&&K.push(oe)}if(K.length===0)continue;let V=K.map(oe=>Math.max(1e-9,M[oe])),J=this.rand()*V.reduce((oe,De)=>oe+De,0),ie=K[K.length-1];for(let oe=0;oe<K.length;oe++){if(J<V[oe]){ie=K[oe];break}J-=V[oe]}let Ne=M[ie],ve=M[O],Oe=M[_];if(Ne>Math.min(ve,Oe))continue;let et=`${O}|${_}|${ie}`;if(this.mergeAttemptKeys.has(et))continue;this.mergeAttemptKeys.add(et);let Je=`${O}|${_}|${ie}`;I.has(Je)||(ee={i:O,j:_,a:ie})}if(this.lastIterFoundNewProgram=!1,ee){let{i:P,j:O,a:_}=ee,{cfg:Y,descSig:le}=this.systemAwareMergeWithSig(h,P,O,(B,re)=>M[B]>=M[re]?B:re),Z=`${Math.min(P,O)}|${Math.max(P,O)}|${le}`;if(this.mergeCompositionKeys.has(Z))continue;this.mergeCompositionKeys.add(Z);let K=x[P],V=x[O],J=Array.from({length:K.length},(B,re)=>re),ie=J.filter(B=>(K[B]??0)>(V[B]??0)),Ne=J.filter(B=>(V[B]??0)>(K[B]??0)),ve=J.filter(B=>!(ie.includes(B)||Ne.includes(B))),Oe=5,et=Math.ceil(Oe/3),Je=(B,re)=>{if(re<=0||B.length===0)return[];if(B.length<=re)return[...B];let ct=[],pt=new Set;for(;ct.length<re;){let ye=Math.floor(this.rand()*B.length);pt.has(ye)||(pt.add(ye),ct.push(B[ye]))}return ct},oe=[];oe.push(...Je(ie,Math.min(et,ie.length))),oe.push(...Je(Ne,Math.min(et,Ne.length)));let De=Oe-oe.length;oe.push(...Je(ve,Math.max(0,De)));let Qe=Oe-oe.length;if(Qe>0){let B=J.filter(re=>!oe.includes(re));oe.push(...Je(B,Math.min(Qe,B.length)))}let ke=oe.slice(0,Math.min(Oe,J.length)),tt=ke.map(B=>p[B]),lt=(await y(Y,tt)).reduce((B,re)=>B+re,0),Ye=ke.reduce((B,re)=>B+(K[re]??0),0),nt=ke.reduce((B,re)=>B+(V[re]??0),0);if(lt>=Math.max(Ye,nt)+this.tieEpsilon){let B=await g(Y,p);h.push({cfg:Y,parent:_,scores:B}),x.push(await y(Y,p));let re=b.length,ct=Fe(b.map(ye=>h[ye].scores))??0;b=Ve(h.map((ye,Ro)=>({idx:Ro,scores:ye.scores})),this.tieEpsilon).map(ye=>ye.idx);let pt=Fe(b.map(ye=>h[ye].scores))??0;(b.length>re||pt>ct+1e-6)&&(S=0),this.mergesDue-=1,this.totalMergesTested+=1,I.add(`${Math.min(P,O)}|${Math.max(P,O)}|${_}`)}continue}}let F=Bn(R,M,()=>this.rand());this.lastIterFoundNewProgram=!1;let D=this.minibatch?this.nextMinibatchIndices(u.length,w).map(v=>u[v]):u;if(r?.skipPerfectScore??!0){let v=Number(r?.perfectScore??1),$=await y(h[F].cfg,D);if($.length>0&&$.every(z=>z>=v))continue}let X=!1,W={...h[F].cfg},xe="reflective_mutation",pe=w%a.length,he=a[pe],_e,Ze;if(X&&this.mergesUsed<this.mergeMax){let v=(F+1)%h.length,$=P=>{let O=[],_=P;for(;_!==void 0;)O.push(_),_=h[_]?.parent;return O},z=$(F),j=$(v),H=z.find(P=>j.includes(P)),ee=!0;if(H||(ee=!1),(j.includes(F)||z.includes(v))&&(ee=!1),ee){let P=h[H].cfg,O=h[F].cfg,_=h[v].cfg,Y=!1,le=new Set([...Object.keys(P),...Object.keys(O),...Object.keys(_)]);for(let Z of le){let K=P[Z],V=O[Z],J=_[Z];if(V===K&&J!==V||J===K&&V!==J){Y=!0;break}}Y||(ee=!1)}if(ee){let P=Math.min(F,v),O=Math.max(F,v),_=`${P}|${O}|${H}`;if(!I.has(_)){let Y=A(h[H].scores),le=A(h[F].scores),Z=A(h[v].scores);Y<=Math.min(le,Z)&&(W=this.systemAwareMerge(h,F,v,(K,V)=>{let J=A(h[K].scores),ie=A(h[V].scores);return J>=ie?K:V}),xe="system_merge",this.mergesUsed+=1,I.add(_))}}else{let P=h[F].cfg[he.name],O=r?.gepaAdapter,_;if(O)try{let Y=await O.evaluate(D,{...h[F].cfg},!0);_e=Array.isArray(Y?.scores)?Y.scores.reduce((V,J)=>V+(Number(J)||0),0):void 0;let le=O.make_reflective_dataset({...h[F].cfg},Y,[he.name]),K=(await O.propose_new_texts?.({...h[F].cfg},le,[he.name]))?.[he.name];typeof K=="string"&&K.length>0&&(_=K)}catch{}if(_||(_=await this.reflectModuleInstruction(he.name,P,s,a,{...h[F].cfg},D,async({prediction:Y,example:le})=>{let Z=await n({prediction:Y,example:le}),K=Object.values(Z||{});return K.length?K.reduce((V,J)=>V+J,0)/K.length:0},r)),W[he.name]=_,O&&_e!==void 0)try{let Y=await O.evaluate(D,W,!1);Ze=Array.isArray(Y?.scores)?Y.scores.reduce((le,Z)=>le+(Number(Z)||0),0):void 0}catch{}}}else{let v=h[F].cfg[he.name],$=r?.gepaAdapter,z;if($)try{let j=await $.evaluate(D,{...h[F].cfg},!0);_e=Array.isArray(j?.scores)?j.scores.reduce((O,_)=>O+(Number(_)||0),0):void 0;let H=$.make_reflective_dataset({...h[F].cfg},j,[he.name]),P=(await $.propose_new_texts?.({...h[F].cfg},H,[he.name]))?.[he.name];typeof P=="string"&&P.length>0&&(z=P)}catch{}if(z||(z=await this.reflectModuleInstruction(he.name,v,s,a,{...h[F].cfg},D,async({prediction:j,example:H})=>{let ee=await n({prediction:j,example:H}),P=Object.values(ee||{});return P.length?P.reduce((O,_)=>O+_,0)/P.length:0},r)),W[he.name]=z,$&&_e!==void 0)try{let j=await $.evaluate(D,W,!1);Ze=Array.isArray(j?.scores)?j.scores.reduce((H,ee)=>H+(Number(ee)||0),0):void 0}catch{}}let Ao=await y(h[F].cfg,D),xo=await y(W,D),yo=Ao.reduce((v,$)=>v+$,0),at=xo.reduce((v,$)=>v+$,0);if(this.currentRound=w+1,await this.updateOptimizationProgress(this.currentRound,at,{modules:a.length,mutatedModule:he.name,totalRounds:this.numTrials},"GEPA-Flow",{strategy:xe,paretoSetSize:p.length},at,{idx:F},{...r??{},maxIterations:this.numTrials}),!(at>yo+this.tieEpsilon&&(_e===void 0||Ze===void 0||Ze>_e+this.tieEpsilon))){if(++S>=this.earlyStoppingTrials)break;continue}let bo=await g(W,p);h.push({cfg:W,parent:F,scores:bo}),x.push(await y(W,p));let Io=b.length,To=Fe(b.map(v=>h[v].scores))??0;b=Ve(h.map((v,$)=>({idx:$,scores:v.scores})),this.tieEpsilon).map(v=>v.idx);let Co=Fe(b.map(v=>h[v].scores))??0;if(b.length>Io||Co>To+1e-6)S=0;else if(S++,S>=this.earlyStoppingTrials)break;this.lastIterFoundNewProgram=!0,this.mergeMax>0&&this.totalMergesTested<this.mergeMax&&(this.mergesDue+=1)}let N=Ve(h.map((w,E)=>({idx:E,scores:w.scores})),this.tieEpsilon),U=N.length>0?Math.max(...N.map(w=>A(w.scores))):0,k=Fe(N.map(w=>w.scores));return this.stats.convergenceInfo.converged=!0,this.recordParetoMetrics(N.length,h.length,"GEPA-Flow",k),{demos:[],stats:this.stats,bestScore:U,paretoFront:N.map(w=>({demos:[],scores:w.scores,configuration:{candidate:w.idx},dominatedSolutions:w.dominated})),paretoFrontSize:N.length,hypervolume:k,finalConfiguration:{strategy:"gepa_flow_pareto",candidates:h.length}}}async getBaseInstruction(e){try{let t=e?.getSignature?.();if(t&&typeof t.instruction=="string"&&t.instruction.length>0)return t.instruction}catch{}return"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?yt(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 c=[],p=[];for(let A of s)try{o[e]=t,n.setAllNodeInstructions?.(o);let x=await n.forward(this.studentAI,A,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let y=await a({prediction:x,example:A});p.push({input:A,prediction:x,score:typeof y=="number"?y:0})}catch{p.push({input:A,prediction:{},score:0})}let u=l?.overrideTeacherAI??this.teacherAI??this.studentAI,d=ge('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 A of p){let x=g({prediction:A.prediction,example:A.input});x&&(Array.isArray(x)?m.push(...x):m.push(x))}let f="";try{f=(await d.forward(u,{moduleName:e,minibatch:p,evalFeedback:m}))?.feedbackSummary?.trim()||""}catch{}let h=ge('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 h.forward(u,{moduleName:e,currentInstruction:t,feedbackSummary:f,minibatch:p}))?.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,c)=>c);for(let l=t.length-1;l>0;l--){let c=Math.floor(this.rand()*(l+1));[t[l],t[c]]=[t[c],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,c)=>c).sort((l,c)=>(this.samplerState.freq.get(l)??0)-(this.samplerState.freq.get(c)??0)),a=[...t];for(let l=0;l<o;l++){let c=s[l%s.length];a.push(c),this.samplerState.freq.set(c,(this.samplerState.freq.get(c)??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=h=>{let A=[],x=h;for(;x!==void 0;)A.push(x),x=e[x]?.parent;return A},s=o(t),a=o(n),c=s.find(h=>a.includes(h))??t,p=e[c].cfg,u=e[t].cfg,d=e[n].cfg,m={},g=[],f=Array.from(new Set([...Object.keys(p),...Object.keys(u),...Object.keys(d)])).sort();for(let h of f){let A=p[h],x=u[h],y=d[h];if(x===A&&y!==x)m[h]=y,g.push("j");else if(y===A&&x!==y)m[h]=x,g.push("i");else if(x!==y&&x!==A&&y!==A){let b=r(t,n);m[h]=b===t?x:y,g.push(b===t?"i":"j")}else m[h]=x??y??A,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=f=>{let h=[],A=f;for(;A!==void 0;)h.push(A),A=e[A]?.parent;return h},s=o(t),a=o(n),c=s.find(f=>a.includes(f))??t,p=e[c].cfg,u=e[t].cfg,d=e[n].cfg,m={},g=new Set([...Object.keys(p),...Object.keys(u),...Object.keys(d)]);for(let f of g){let h=p[f],A=u[f],x=d[f];if(A===h&&x!==A)m[f]=x;else if(x===h&&A!==x)m[f]=A;else if(A!==x&&A!==h&&x!==h){let y=r(t,n);m[f]=y===t?A:x}else m[f]=A??x??h}return m}};var io=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 ao=class extends Se{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 io(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=`
323
329
  Analyze this language model program and provide a concise summary of its purpose and structure.
324
330
 
325
331
  Program Signature: ${e.getSignature()}
@@ -359,7 +365,7 @@ Requirements:
359
365
  5. Keep it concise but comprehensive
360
366
 
361
367
  Generate a single, well-crafted instruction:
362
- Instruction:`;try{let m=(await ge('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 c=["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."],p=c[t%c.length]||c[0];return e&&(p=`${p} ${e}`),p}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 c=0;c<this.numCandidates;c++){let p=l.length>0?c%l.length:-1,u=p>=0?l[p]:void 0,d=await this.generateInstruction({tip:u,candidateIndex:c,ai:o,programSummary:s,datasetSummary:a,previousInstructions:r});r.push(d)}return r}async bootstrapFewShotExamples(e,t,n){return(await new Xt({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){e.setInstruction&&e.setInstruction(t.instruction),t.bootstrappedDemos>0&&e.setDemos&&e.setDemos(n.slice(0,t.bootstrappedDemos)),t.labeledExamples>0&&e.setExamples&&e.setExamples(r.slice(0,t.labeledExamples))}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),t.bootstrappedDemos>0&&e.setDemos(n.slice(0,t.bootstrappedDemos)),t.labeledExamples>0&&e.setExamples(r.slice(0,t.labeledExamples))}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=`mipro_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,a={study_name:s,parameters:[{name:"temperature",type:"float",low:.1,high:2},{name:"bootstrappedDemos",type:"int",low:0,high:this.maxBootstrappedDemos},...this.optimizeTopP?[{name:"topP",type:"float",low:.7,high:1}]:[]],objective:{name:"score",direction:"maximize"},n_trials:this.numTrials,sampler:"TPESampler",pruner:this.minibatch?"MedianPruner":void 0},l=await this.pythonClient.createOptimizationJob(a);this.getOptimizerLogger()?.({name:"OptimizationStart",value:{optimizerType:"MiPRO (Python)",exampleCount:t.length,validationCount:0,config:{jobId:l.job_id,numTrials:this.numTrials}}});let p=Number.NEGATIVE_INFINITY,u,d=0,m=0;for(let b=0;b<this.numTrials;b++)try{let S=await this.pythonClient.suggestParameters(s),I=S.params.temperature,T=S.params.bootstrappedDemos,C=this.optimizeTopP?S.params.topP:void 0;if(I===void 0)throw new Error(`Missing temperature parameter in suggestion: ${JSON.stringify(S)}`);if(T===void 0)throw new Error(`Missing bootstrappedDemos parameter in suggestion: ${JSON.stringify(S)}`);let U=!this.minibatch||this.minibatchFullEvalSteps>0&&b%this.minibatchFullEvalSteps===this.minibatchFullEvalSteps-1?[...t]:(()=>{let O=Math.min(this.minibatchSize,t.length),R=new Set;for(;R.size<O;)R.add(Math.floor(Math.random()*t.length));return Array.from(R).map(v=>t[v])})(),P=await this.evaluateConfiguration(e,n,{temperature:I,bootstrappedDemos:T,topP:C},U);d++,await this.pythonClient.evaluateTrial({study_name:s,trial_number:S.trial_number,value:P}),P>p+this.minImprovementThreshold?(p=P,u={temperature:I,bootstrappedDemos:T,...C!==void 0?{topP:C}:{},trialNumber:S.trial_number},m=0):m+=1,this.currentRound=b+1;let w={temperature:I,bootstrappedDemos:T,...C!==void 0?{topP:C}:{},trialNumber:S.trial_number};if(this.localScoreHistory.push(P),this.localConfigurationHistory.push(w),await this.updateOptimizationProgress(this.currentRound,P,w,"MiPRO (Python)",{sampler:"TPESampler"},p,u),this.onProgress?.({round:b+1,totalRounds:this.numTrials,currentScore:P,bestScore:p,tokensUsed:this.stats.estimatedTokenUsage,timeElapsed:Date.now()-o,successfulExamples:d,totalExamples:t.length}),this.earlyStoppingTrials>0&&m>=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 g=p,f={},h=[];try{let b=await this.pythonClient.getStudyResults(s);if(g=b.best_value||p,f=b.best_params||{},f&&Object.keys(f).length>0){let S=f.bootstrappedDemos||0;S>0&&(h=await this.bootstrapFewShotExamples(e,n,t.slice(0,Math.floor(t.length*.8))),h=h.slice(0,S))}}catch{}let A;try{let S=await ge('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:g,totalCalls:this.stats.totalCalls,successfulDemos:this.stats.successfulDemos,bestConfig:f||{}});A={humanExplanation:S.humanExplanation??"",recommendations:S.recommendations??[],performanceAssessment:S.performanceAssessment??""}}catch{}await this.logOptimizationComplete("MiPRO (Python)",g,f,r,A);try{await this.pythonClient.deleteStudy(s)}catch{}this.stats.bestScore=g;let x=new we(e.getSignature());h.length>0&&x.setDemos(h),f.temperature&&(x._optimizedModelConfig={temperature:f.temperature});let y=new We({bestScore:g,stats:this.stats,instruction:void 0,demos:h,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(g,h.length),{bestScore:g,demos:h,stats:this.stats,optimizedGen:x,optimizedProgram:y,finalConfiguration:{temperature:f.temperature,bootstrappedDemos:f.bootstrappedDemos||0,...f}}}generateOptimizationReport(e,t){console.log(`
368
+ Instruction:`;try{let m=(await ge('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 c=["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."],p=c[t%c.length]||c[0];return e&&(p=`${p} ${e}`),p}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 c=0;c<this.numCandidates;c++){let p=l.length>0?c%l.length:-1,u=p>=0?l[p]:void 0,d=await this.generateInstruction({tip:u,candidateIndex:c,ai:o,programSummary:s,datasetSummary:a,previousInstructions:r});r.push(d)}return r}async bootstrapFewShotExamples(e,t,n){return(await new Xt({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){e.setInstruction&&e.setInstruction(t.instruction),t.bootstrappedDemos>0&&e.setDemos&&e.setDemos(n.slice(0,t.bootstrappedDemos)),t.labeledExamples>0&&e.setExamples&&e.setExamples(r.slice(0,t.labeledExamples))}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),t.bootstrappedDemos>0&&e.setDemos(n.slice(0,t.bootstrappedDemos)),t.labeledExamples>0&&e.setExamples(r.slice(0,t.labeledExamples))}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=`mipro_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,a={study_name:s,parameters:[{name:"temperature",type:"float",low:.1,high:2},{name:"bootstrappedDemos",type:"int",low:0,high:this.maxBootstrappedDemos},...this.optimizeTopP?[{name:"topP",type:"float",low:.7,high:1}]:[]],objective:{name:"score",direction:"maximize"},n_trials:this.numTrials,sampler:"TPESampler",pruner:this.minibatch?"MedianPruner":void 0},l=await this.pythonClient.createOptimizationJob(a);this.getOptimizerLogger()?.({name:"OptimizationStart",value:{optimizerType:"MiPRO (Python)",exampleCount:t.length,validationCount:0,config:{jobId:l.job_id,numTrials:this.numTrials}}});let p=Number.NEGATIVE_INFINITY,u,d=0,m=0;for(let b=0;b<this.numTrials;b++)try{let S=await this.pythonClient.suggestParameters(s),I=S.params.temperature,T=S.params.bootstrappedDemos,C=this.optimizeTopP?S.params.topP:void 0;if(I===void 0)throw new Error(`Missing temperature parameter in suggestion: ${JSON.stringify(S)}`);if(T===void 0)throw new Error(`Missing bootstrappedDemos parameter in suggestion: ${JSON.stringify(S)}`);let U=!this.minibatch||this.minibatchFullEvalSteps>0&&b%this.minibatchFullEvalSteps===this.minibatchFullEvalSteps-1?[...t]:(()=>{let E=Math.min(this.minibatchSize,t.length),R=new Set;for(;R.size<E;)R.add(Math.floor(Math.random()*t.length));return Array.from(R).map(M=>t[M])})(),k=await this.evaluateConfiguration(e,n,{temperature:I,bootstrappedDemos:T,topP:C},U);d++,await this.pythonClient.evaluateTrial({study_name:s,trial_number:S.trial_number,value:k}),k>p+this.minImprovementThreshold?(p=k,u={temperature:I,bootstrappedDemos:T,...C!==void 0?{topP:C}:{},trialNumber:S.trial_number},m=0):m+=1,this.currentRound=b+1;let w={temperature:I,bootstrappedDemos:T,...C!==void 0?{topP:C}:{},trialNumber:S.trial_number};if(this.localScoreHistory.push(k),this.localConfigurationHistory.push(w),await this.updateOptimizationProgress(this.currentRound,k,w,"MiPRO (Python)",{sampler:"TPESampler"},p,u),this.onProgress?.({round:b+1,totalRounds:this.numTrials,currentScore:k,bestScore:p,tokensUsed:this.stats.estimatedTokenUsage,timeElapsed:Date.now()-o,successfulExamples:d,totalExamples:t.length}),this.earlyStoppingTrials>0&&m>=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 g=p,f={},h=[];try{let b=await this.pythonClient.getStudyResults(s);if(g=b.best_value||p,f=b.best_params||{},f&&Object.keys(f).length>0){let S=f.bootstrappedDemos||0;S>0&&(h=await this.bootstrapFewShotExamples(e,n,t.slice(0,Math.floor(t.length*.8))),h=h.slice(0,S))}}catch{}let A;try{let S=await ge('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:g,totalCalls:this.stats.totalCalls,successfulDemos:this.stats.successfulDemos,bestConfig:f||{}});A={humanExplanation:S.humanExplanation??"",recommendations:S.recommendations??[],performanceAssessment:S.performanceAssessment??""}}catch{}await this.logOptimizationComplete("MiPRO (Python)",g,f,r,A);try{await this.pythonClient.deleteStudy(s)}catch{}this.stats.bestScore=g;let x=new we(e.getSignature());h.length>0&&x.setDemos(h),f.temperature&&(x._optimizedModelConfig={temperature:f.temperature});let y=new We({bestScore:g,stats:this.stats,instruction:void 0,demos:h,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(g,h.length),{bestScore:g,demos:h,stats:this.stats,optimizedGen:x,optimizedProgram:y,finalConfiguration:{temperature:f.temperature,bootstrappedDemos:f.bootstrappedDemos||0,...f}}}generateOptimizationReport(e,t){console.log(`
363
369
  \u{1F389} MiPRO Optimization Complete!
364
370
  `),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
365
371
  `),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