@ax-llm/ax 24.0.16 → 24.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1256,7 +1256,7 @@ ${e}
1256
1256
  `);throw new He(n,void 0,e)}return r.filter(n=>n.type==="output_text").map(n=>n.text).join(`
1257
1257
  `)};function hc(r){let e=[];for(let t of r??[])if(t?.type==="output_text"&&Array.isArray(t.annotations))for(let n of t.annotations)n&&n.type==="url_citation"&&typeof n.url=="string"&&e.push({url:n.url,title:n.title,description:n.description});return e.length?e:void 0}var yc=()=>({model:"gpt-4o",embedModel:"text-embedding-ada-002",temperature:.7,topP:1,stream:!0}),$P=()=>({...yc(),model:"gpt-4o",temperature:.5}),UP=()=>({...yc(),model:"gpt-4o",temperature:.9}),Ao=class extends It{constructor({apiKey:e,credentialProvider:t,credentialProfile:n,config:o,options:i,apiURL:s,modelInfo:a=[],models:c,responsesReqUpdater:l,supportFor:u={functions:!0,streaming:!0,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}){if((!e||e==="")&&!t)throw new Error("OpenAI API key not set");let d=new xc(o,i?.streamingUsage??!0,i,l,u),p=c?.map(m=>{let g=m,h=g?.config;if(!h)return m;let x={};h.maxTokens!==void 0&&(x.maxTokens=h.maxTokens),h.temperature!==void 0&&(x.temperature=h.temperature),h.topP!==void 0&&(x.topP=h.topP),h.presencePenalty!==void 0&&(x.presencePenalty=h.presencePenalty),h.frequencyPenalty!==void 0&&(x.frequencyPenalty=h.frequencyPenalty);let f=h.stopSequences??h.stop;f!==void 0&&(x.stopSequences=f),h.n!==void 0&&(x.n=h.n),h.stream!==void 0&&(x.stream=h.stream);let y={...g};Object.keys(x).length>0&&(y.modelConfig={...g.modelConfig??{},...x});let A=h?.thinking?.thinkingTokenBudget;if(typeof A=="number"){let b=[["minimal",200],["low",800],["medium",5e3],["high",1e4],["highest",24500]],v="minimal",w=Number.POSITIVE_INFINITY;for(let[N,E]of b){let I=Math.abs(A-E);I<w&&(w=I,v=N)}y.thinkingTokenBudget=v}return h?.thinking?.includeThoughts!==void 0&&(y.showThoughts=!!h.thinking.includeThoughts),h.serviceTier!==void 0&&(y.serviceTier=Bt(h.serviceTier)),y});super(d,{name:"OpenAI",apiURL:s||"https://api.openai.com/v1",headers:async()=>e?{Authorization:`Bearer ${e}`}:{},profile:n,credentialProvider:t,modelInfo:a,defaults:{model:o.model??"gpt-4o",embedModel:o.embedModel??"text-embedding-ada-002"},options:i,supportFor:u,models:p??c}),this.setChatCredentialOperation("responses")}},$i=class extends Ao{constructor({apiKey:e,credentialProvider:t,config:n,options:o,models:i,modelInfo:s}){if((!e||e==="")&&!t)throw new Error("OpenAI API key not set");s=[...Oi,...s??[]];let a=c=>{let l=jt({model:c,modelInfo:s,models:i}),u=l?.supported?.structuredOutputs??!0,d=l?.supported?.structuredOutputModes??(u?["native","function","json_object"]:["function","json_object"]);return{functions:!0,streaming:!0,hasThinkingBudget:l?.supported?.thinkingBudget??!1,hasShowThoughts:l?.supported?.showThoughts??!1,structuredOutputs:d.includes("native"),structuredOutputModes:d,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,serviceTiers:l?.supported?.serviceTiers??["standard","flex","priority"]}};super({apiKey:e,credentialProvider:t,credentialProfile:"openai-responses",config:{...yc(),...n},options:o,modelInfo:s,models:i,supportFor:a})}};var op={openai:{id:"openai",name:"OpenAI",aliases:["openai"],transport:"openai-chat",baseURL:"https://api.openai.com/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:"gpt-5-mini",embedModel:"text-embedding-3-small"},capabilities:{functions:!0,streaming:!0,structuredOutputs:!0,thinking:!0,multiTurn:!0,images:!0,audio:!0,audioOutput:!0,structuredOutputModes:["native","function","json_object"],serviceTiers:["standard","flex","priority"]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"},embed:{path:"/embeddings",dialect:"openai-embeddings"},transcribe:{path:"/audio/transcriptions",dialect:"openai-transcription"},speak:{path:"/audio/speech",dialect:"openai-speech"},realtime:{path:"/realtime",dialect:"openai-realtime",modelMatch:{prefix:["gpt-realtime"]},url:"wss://api.openai.com/v1/realtime",grammar:"openai_realtime_compatible",audio:{input:{formats:["pcm16","pcm"],sampleRate:24e3},output:{formats:["pcm16","pcm"],sampleRate:24e3,voices:["alloy","ash","ballad","coral","echo","sage","shimmer","verse"],defaultVoice:"alloy"}},validation:{structuredOutputWithAudio:!1}}},modelRules:[],sources:["https://platform.openai.com/docs/api-reference/chat"],reviewedAt:"2026-08-17",request:{serviceTierMap:{auto:"auto",standard:"default",flex:"flex",priority:"priority"}}},"openai-compatible":{id:"openai-compatible",name:"OpenAI Compatible",aliases:["openai-compatible","openai_compatible","compatible"],transport:"openai-chat",baseURL:null,requiresApiURL:!0,auth:{type:"bearer",required:!1},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"},embed:{path:"/embeddings",dialect:"openai-embeddings"}},modelRules:[],sources:["https://platform.openai.com/docs/api-reference/chat"],reviewedAt:"2026-08-17"},"openai-responses":{id:"openai-responses",name:"OpenAI Responses",aliases:["openai-responses","openai_responses","responses"],transport:"openai-responses",baseURL:"https://api.openai.com/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:"gpt-5-mini",embedModel:"text-embedding-3-small"},capabilities:{functions:!0,streaming:!0,structuredOutputs:!0,thinking:!0,multiTurn:!0,images:!0,audio:!0,audioOutput:!0,structuredOutputModes:["native","function","json_object"],serviceTiers:["standard","flex","priority"]},operations:{chat:{path:"/responses",dialect:"openai-responses"},embed:{path:"/embeddings",dialect:"openai-embeddings"},transcribe:{path:"/audio/transcriptions",dialect:"openai-transcription"},speak:{path:"/audio/speech",dialect:"openai-speech"},realtime:{path:"/realtime",dialect:"openai-realtime",modelMatch:{prefix:["gpt-realtime"]},url:"wss://api.openai.com/v1/realtime",grammar:"openai_realtime_compatible",audio:{input:{formats:["pcm16","pcm"],sampleRate:24e3},output:{formats:["pcm16","pcm"],sampleRate:24e3,voices:["alloy","ash","ballad","coral","echo","sage","shimmer","verse"],defaultVoice:"alloy"}},validation:{structuredOutputWithAudio:!1}}},modelRules:[],sources:["https://platform.openai.com/docs/api-reference/responses"],reviewedAt:"2026-08-17",request:{serviceTierMap:{auto:"auto",standard:"default",flex:"flex",priority:"priority"}}},anthropic:{id:"anthropic",name:"Anthropic",aliases:["anthropic","claude"],transport:"anthropic-messages",baseURL:"https://api.anthropic.com",requiresApiURL:!1,auth:{type:"x-api-key",required:!0},headers:{"anthropic-version":"2023-06-01","anthropic-beta":"structured-outputs-2025-11-13, web-search-2025-03-05"},defaults:{model:"claude-sonnet-4-5"},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!0,multiTurn:!0,images:!0,caching:{types:["ephemeral"],cacheBreakpoints:!0},structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/v1/messages",dialect:"anthropic-messages"}},modelRules:[],sources:["https://docs.anthropic.com/en/api/messages"],reviewedAt:"2026-08-17"},"google-gemini":{id:"google-gemini",name:"Google Gemini",aliases:["google-gemini","google_gemini","gemini"],transport:"gemini-generate-content",baseURL:"https://generativelanguage.googleapis.com/v1beta",requiresApiURL:!1,auth:{type:"api-key-header",header:"x-goog-api-key",required:!0},defaults:{model:"gemini-3.5-flash",embedModel:"gemini-embedding-2"},capabilities:{functions:!0,streaming:!0,structuredOutputs:!0,thinking:!0,multiTurn:!0,images:!0,audio:!0,audioOutput:!0,files:{uploadMethod:"cloud"},caching:{types:["persistent"]},structuredOutputModes:["native","function"],serviceTiers:["standard","flex","priority"]},operations:{chat:{path:"/models/{model}:generateContent",dialect:"gemini-generate-content"},stream_chat:{path:"/models/{model}:streamGenerateContent?alt=sse",dialect:"gemini-generate-content"},embed:{path:"/models/{model}:batchEmbedContents",dialect:"gemini-generate-content"},transcribe:{path:"/models/{model}:generateContent",dialect:"gemini-generate-content"},speak:{path:"/models/{model}:generateContent",dialect:"gemini-generate-content"},realtime:{path:"/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",dialect:"gemini-live-bidi",modelMatch:{prefix:["gemini-live"],contains:["native-audio","-live-"]},url:"wss://generativelanguage.googleapis.com/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",grammar:"gemini_live_bidi",defaultModel:"gemini-2.5-flash-native-audio-preview-12-2025",audio:{input:{formats:["pcm16","pcm"],sampleRate:16e3},output:{formats:["pcm16","pcm"],sampleRate:24e3,voices:["Kore","Puck","Charon","Fenrir","Aoede"],defaultVoice:"Kore"}},validation:{pcmInputOnly:!0,rejectStructuredOutputWithAudio:!0}}},modelRules:[],sources:["https://ai.google.dev/api/generate-content","https://ai.google.dev/gemini-api/docs/optimization"],reviewedAt:"2026-08-17",request:{serviceTierMap:{auto:null,standard:"standard",flex:"flex",priority:"priority"}}},webllm:{id:"webllm",name:"WebLLM",aliases:["webllm"],transport:"webllm",baseURL:null,requiresApiURL:!1,auth:{type:"none",required:!1},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"",dialect:"webllm"}},modelRules:[],sources:["https://webllm.mlc.ai/docs/"],reviewedAt:"2026-08-17"},"azure-openai":{id:"azure-openai",name:"Azure OpenAI",aliases:["azure-openai","azure_openai","azure"],transport:"openai-chat",baseURL:null,requiresApiURL:!1,auth:{type:"api-key-header",header:"api-key",required:!0},defaults:{model:"gpt-5-mini",embedModel:"text-embedding-3-small"},capabilities:{functions:!0,streaming:!0,structuredOutputs:!0,thinking:!0,multiTurn:!0,images:!0,structuredOutputModes:["native","function"],serviceTiers:["standard","priority"]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"},embed:{path:"/embeddings",dialect:"openai-embeddings"}},endpoint:{scheme:"https",hostField:"resourceName",hostSuffix:".openai.azure.com",path:"/openai/deployments/{deploymentName}",fields:{resourceName:["resource_name","resourceName"],deploymentName:["deployment_name","deploymentName"],version:["api_version","apiVersion","version"]},required:["resourceName","deploymentName"],defaults:{version:"2024-02-15-preview"},normalizers:{version:"api-version"},apiVersionField:"version"},capabilityGates:{structuredOutputs:{option:"version",min:"2024-08-01"}},modelRules:[],sources:["https://learn.microsoft.com/en-us/azure/ai-services/openai/reference","https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/priority-processing"],reviewedAt:"2026-08-17",request:{serviceTierMap:{auto:"auto",standard:"default",flex:"flex",priority:"priority"}}},deepseek:{id:"deepseek",name:"DeepSeek",aliases:["deepseek"],transport:"openai-chat",baseURL:"https://api.deepseek.com",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:"deepseek-v4-flash"},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function","json_object"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[{match:{exact:["deepseek-v4-flash","deepseek-v4-pro"]},capabilities:{thinking:!0,thinkingBudget:!0,showThoughts:!0,structuredOutputs:!1,structuredOutputModes:["function"]},request:{reasoning:"thinking-object",toolChoice:"unforced",effortMap:{none:null,minimal:"low",low:"low",medium:"medium",high:"high",highest:"max",xhigh:"max",max:"max"},dropWhenThinking:["temperature","top_p","presence_penalty","frequency_penalty"],defaultThinkingLevel:"max"},response:{reasoningFields:["reasoning_content","reasoning"]},replay:{assistantReasoningField:"reasoning_content"}},{match:{exact:["deepseek-reasoner"]},capabilities:{thinking:!0,thinkingBudget:!1,showThoughts:!0,structuredOutputs:!1,structuredOutputModes:["function"]},request:{toolChoice:"unforced"},response:{reasoningFields:["reasoning_content","reasoning"]},replay:{assistantReasoningField:"reasoning_content"}}],sources:["https://api-docs.deepseek.com/guides/thinking_mode/"],reviewedAt:"2026-08-18"},"deepseek-responses":{id:"deepseek-responses",name:"DeepSeek Responses",aliases:["deepseek-responses","deepseek_responses"],transport:"openai-responses",baseURL:"https://api.deepseek.com",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:"deepseek-v4-flash"},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!0,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/responses",dialect:"openai-responses"}},request:{dropFields:["include","previous_response_id","store","parallel_tool_calls"],reasoningObjectFields:["effort"]},modelRules:[],sources:["https://api-docs.deepseek.com/api/create-chat-completion"],reviewedAt:"2026-08-17"},mistral:{id:"mistral",name:"Mistral AI",aliases:["mistral"],transport:"openai-chat",baseURL:"https://api.mistral.ai/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:"mistral-small-latest",embedModel:"mistral-embed"},capabilities:{functions:!0,streaming:!0,structuredOutputs:!0,thinking:!1,multiTurn:!0,images:!0,audio:!0,audioOutput:!0,structuredOutputModes:["native","function"],serviceTiers:["standard","priority"]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"},embed:{path:"/embeddings",dialect:"openai-embeddings"},transcribe:{path:"/audio/transcriptions",dialect:"openai-transcription"},speak:{path:"/audio/speech",dialect:"mistral-speech"}},request:{renameFields:{max_completion_tokens:"max_tokens"},imageURLShape:"object",serviceTierMap:{auto:"auto",standard:"standard_only",priority:"auto"}},modelRules:[],sources:["https://docs.mistral.ai/api/","https://docs.mistral.ai/inference/priority-tier"],reviewedAt:"2026-08-17"},cohere:{id:"cohere",name:"Cohere",aliases:["cohere"],transport:"openai-chat",baseURL:"https://api.cohere.ai/compatibility/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:"command-r-plus",embedModel:"embed-english-v3.0"},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"},embed:{path:"/embeddings",dialect:"openai-embeddings"}},modelRules:[],sources:["https://docs.cohere.com/reference/compatibility-api"],reviewedAt:"2026-08-17"},grok:{id:"grok",name:"xAI Grok",aliases:["grok","xai","x-grok","x_grok"],transport:"openai-chat",baseURL:"https://api.x.ai/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:"grok-4.6"},capabilities:{functions:!0,streaming:!0,structuredOutputs:!0,thinking:!1,multiTurn:!0,images:!0,audio:!0,audioOutput:!0,webSearch:!0,structuredOutputModes:["native","function"],serviceTiers:["standard","priority"]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"},transcribe:{path:"/stt",dialect:"xai-transcription"},speak:{path:"/tts",dialect:"xai-speech"},realtime:{path:"/realtime",dialect:"xai-realtime",modelMatch:{prefix:["grok-voice"]},url:"wss://api.x.ai/v1/realtime",grammar:"openai_realtime_compatible",defaultModel:"grok-voice-think-fast-1.0",audio:{input:{formats:["pcm16","pcm"],sampleRate:24e3},output:{formats:["pcm16","pcm"],sampleRate:24e3,voices:["eve","ara","rex","sal","leo"],defaultVoice:"eve"}},validation:{structuredOutputWithAudio:!1}}},request:{optionDialect:"search-parameters",serviceTierMap:{auto:null,standard:"default",priority:"priority"}},modelRules:[{match:{exact:["grok-4.6"]},capabilities:{thinking:!0,thinkingBudget:!0,structuredOutputs:!0,structuredOutputModes:["native","function"]},request:{reasoning:"effort",defaultThinkingLevel:"max",effortMap:{none:null,minimal:"low",low:"low",medium:"medium",high:"high",highest:"xhigh",xhigh:"xhigh",max:"xhigh"},unsupportedThinkingLevels:{none:"xAI Grok 4.6 reasoning cannot be disabled"},dropFields:["presence_penalty","frequency_penalty","stop"]}},{match:{exact:["grok-4.5","grok-4.5-latest","grok-build-latest"]},capabilities:{thinking:!0,thinkingBudget:!0,structuredOutputs:!0,structuredOutputModes:["native","function"]},request:{reasoning:"effort",defaultThinkingLevel:"max",effortMap:{none:null,minimal:"low",low:"low",medium:"medium",high:"high",highest:"high",xhigh:"high",max:"high"},unsupportedThinkingLevels:{none:"xAI Grok 4.5 reasoning cannot be disabled"},dropFields:["presence_penalty","frequency_penalty","stop"]}},{match:{exact:["grok-4.3","grok-4.3-latest","grok-latest"]},capabilities:{thinking:!0,thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0,structuredOutputModes:["native","function"]},request:{reasoning:"effort",defaultThinkingLevel:"max",effortMap:{none:"none",minimal:"low",low:"low",medium:"medium",high:"high",highest:"high",xhigh:"high",max:"high"},dropFields:["presence_penalty","frequency_penalty","stop"]}},{match:{exact:["grok-3-mini","grok-3-mini-latest","grok-3-mini-beta","grok-3-mini-fast","grok-3-mini-fast-latest","grok-3-mini-fast-beta"]},capabilities:{thinking:!0,thinkingBudget:!0},request:{reasoning:"effort",defaultThinkingLevel:"low",effortMap:{none:null,minimal:"low",low:"low",medium:"high",high:"high",highest:"high",xhigh:"high",max:"high"},unsupportedThinkingLevels:{none:"xAI Grok 3 Mini reasoning cannot be disabled"}}}],sources:["https://docs.x.ai/developers/model-capabilities/text/reasoning","https://docs.x.ai/developers/rest-api-reference/management/auth","https://docs.x.ai/developers/models/grok-4.5","https://docs.x.ai/developers/advanced-api-usage/priority-processing"],reviewedAt:"2026-08-30"},reka:{id:"reka",name:"Reka",aliases:["reka"],transport:"openai-chat",baseURL:"https://api.reka.ai/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:"reka-core"},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.reka.ai/"],reviewedAt:"2026-08-17"},together:{id:"together",name:"Together AI",aliases:["together","together-ai","together_ai"],transport:"openai-chat",baseURL:"https://api.together.xyz/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!0,thinking:!1,multiTurn:!0,structuredOutputModes:["native","function","json_object"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"},embed:{path:"/embeddings",dialect:"openai-embeddings"}},modelRules:[{match:{prefix:["deepseek-ai/DeepSeek-V4"]},capabilities:{thinking:!0,thinkingBudget:!0,showThoughts:!0,structuredOutputs:!1,structuredOutputModes:["function"]},request:{reasoning:"effort",toolChoice:"unforced",effortMap:{none:null,minimal:"high",low:"high",medium:"high",high:"max",highest:"max",xhigh:"max",max:"max"},defaultThinkingLevel:"max"},response:{reasoningFields:["reasoning","reasoning_content"]},replay:{assistantReasoningField:"reasoning"}}],sources:["https://docs.together.ai/docs/inference/chat/reasoning"],reviewedAt:"2026-08-18"},openrouter:{id:"openrouter",name:"OpenRouter",aliases:["openrouter"],transport:"openai-chat",baseURL:"https://openrouter.ai/api/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:["standard","flex","priority"]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[{match:{prefix:["deepseek/"]},capabilities:{thinking:!0,thinkingBudget:!0,showThoughts:!0,structuredOutputs:!1,structuredOutputModes:["function"]},request:{reasoning:"openrouter",toolChoice:"unforced",effortMap:{none:"none",minimal:"low",low:"low",medium:"medium",high:"high",highest:"max",xhigh:"xhigh",max:"max"},defaultThinkingLevel:"max"},response:{reasoningFields:["reasoning","reasoning_content"],reasoningDetailsFields:["reasoning_details"]},replay:{assistantReasoningField:"reasoning",assistantReasoningDetailsField:"reasoning_details"}}],sources:["https://openrouter.ai/docs/guides/best-practices/reasoning-tokens","https://openrouter.ai/docs/guides/features/service-tiers"],reviewedAt:"2026-08-18",request:{serviceTierMap:{auto:null,standard:null,flex:"flex",priority:"priority"}}},orcarouter:{id:"orcarouter",name:"OrcaRouter",aliases:["orcarouter"],transport:"openai-chat",baseURL:"https://api.orcarouter.ai/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:"orcarouter/auto"},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://www.orcarouter.ai"],reviewedAt:"2026-08-19"},fireworks:{id:"fireworks",name:"Fireworks AI",aliases:["fireworks","fireworks-ai"],transport:"openai-chat",baseURL:"https://api.fireworks.ai/inference/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!0,thinking:!1,multiTurn:!0,structuredOutputModes:["native","function"],serviceTiers:["standard","priority"]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"},embed:{path:"/embeddings",dialect:"openai-embeddings"}},modelRules:[{match:{contains:["deepseek-v4"]},capabilities:{thinking:!0,thinkingBudget:!0,showThoughts:!0,structuredOutputs:!1,structuredOutputModes:["function"]},request:{reasoning:"effort",toolChoice:"unforced",effortMap:{none:"none",minimal:"high",low:"high",medium:"high",high:"high",highest:"max",xhigh:"max",max:"max"},defaultThinkingLevel:"max"},response:{reasoningFields:["reasoning_content","reasoning"]},replay:{assistantReasoningField:"reasoning_content"}}],sources:["https://docs.fireworks.ai/api-reference/post-chatcompletions","https://docs.fireworks.ai/guides/reasoning"],reviewedAt:"2026-08-18",request:{serviceTierMap:{auto:null,standard:"default",priority:"priority"}}},"huggingface-router":{id:"huggingface-router",name:"Hugging Face Router",aliases:["huggingface-router","huggingface","hf-router"],transport:"openai-chat",baseURL:"https://router.huggingface.co/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://huggingface.co/docs/inference-providers/en/index","https://huggingface.co/docs/inference-providers/en/tasks/chat-completion"],reviewedAt:"2026-08-18"},"amazon-bedrock":{id:"amazon-bedrock",name:"Amazon Bedrock",aliases:["amazon-bedrock","bedrock"],transport:"openai-chat",baseURL:null,requiresApiURL:!0,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:["standard","flex","priority"]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.aws.amazon.com/bedrock/latest/userguide/inference-chat-completions-mantle.html","https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html"],reviewedAt:"2026-08-17",request:{serviceTierMap:{auto:null,standard:"default",flex:"flex",priority:"priority"}}},"azure-foundry":{id:"azure-foundry",name:"Azure AI Foundry",aliases:["azure-foundry","azure-ai-foundry","microsoft-foundry"],transport:"openai-chat",baseURL:null,requiresApiURL:!0,auth:{type:"api-key-header",header:"api-key",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:["standard","priority"]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://learn.microsoft.com/en-us/rest/api/microsoft-foundry/azureopenai/chat","https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/priority-processing"],reviewedAt:"2026-08-17",request:{serviceTierMap:{auto:"auto",standard:"default",flex:"flex",priority:"priority"}}},"vertex-ai":{id:"vertex-ai",name:"Vertex AI OpenAI Compatibility",aliases:["vertex-ai","vertex-openai"],transport:"openai-chat",baseURL:null,requiresApiURL:!0,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[{match:{exact:["google/gemma-4-26b-a4b-it-maas"]},capabilities:{structuredOutputs:!1,structuredOutputModes:["json_object","function"],thinking:!0},request:{defaultThinkingLevel:"max",thinkingBoolean:{path:["chat_template_kwargs","enable_thinking"]}},response:{reasoningFields:["reasoning_content"]},replay:{assistantReasoningField:"reasoning_content"}},{match:{prefix:["google/gemini-","gemini-"]},capabilities:{structuredOutputs:!0,structuredOutputModes:["native","function","json_object"]}}],sources:["https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/call-vertex-using-openai-library","https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/maas/capabilities/structured-output","https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/maas/capabilities/thinking"],reviewedAt:"2026-08-18"},databricks:{id:"databricks",name:"Databricks Model Serving",aliases:["databricks"],transport:"openai-chat",baseURL:null,requiresApiURL:!0,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:["standard","priority"]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.databricks.com/aws/en/machine-learning/model-serving/query-chat-models","https://docs.databricks.com/aws/en/machine-learning/foundation-model-apis/priority-mode"],reviewedAt:"2026-08-17",request:{serviceTierMap:{auto:null,standard:"default",priority:"priority"}}},baseten:{id:"baseten",name:"Baseten Model APIs",aliases:["baseten"],transport:"openai-chat",baseURL:"https://inference.baseten.co/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!0,thinking:!1,multiTurn:!0,structuredOutputModes:["native","function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.baseten.co/inference/model-apis/overview"],reviewedAt:"2026-08-17"},groq:{id:"groq",name:"Groq",aliases:["groq"],transport:"openai-chat",baseURL:"https://api.groq.com/openai/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!0,thinking:!1,multiTurn:!0,structuredOutputModes:["native","function"],serviceTiers:["standard","flex","priority"]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[{match:{exact:["openai/gpt-oss-20b","openai/gpt-oss-120b"]},capabilities:{thinking:!0,thinkingBudget:!0},request:{reasoning:"effort",defaultThinkingLevel:"max",effortMap:{none:null,minimal:"low",low:"low",medium:"medium",high:"high",highest:"high",xhigh:"high",max:"high"},unsupportedThinkingLevels:{none:"Groq GPT-OSS reasoning does not support the none effort level"}}},{match:{exact:["qwen/qwen3.6-27b"]},capabilities:{thinking:!0,thinkingBudget:!0},request:{reasoning:"effort",defaultThinkingLevel:"max",effortMap:{none:"none",minimal:"default",low:"default",medium:"default",high:"default",highest:"default",xhigh:"default",max:"default"}}}],sources:["https://console.groq.com/docs/reasoning","https://console.groq.com/docs/api-reference","https://console.groq.com/docs/service-tiers"],reviewedAt:"2026-08-18",request:{serviceTierMap:{auto:"auto",standard:"on_demand",flex:"flex",priority:"performance"}}},cerebras:{id:"cerebras",name:"Cerebras Inference",aliases:["cerebras"],transport:"openai-chat",baseURL:"https://api.cerebras.ai/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!0,thinking:!1,multiTurn:!0,structuredOutputModes:["native","function"],serviceTiers:["standard","flex","priority"]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[{match:{exact:["gpt-oss-120b"]},capabilities:{thinking:!0,thinkingBudget:!0},request:{reasoning:"effort",defaultThinkingLevel:"max",effortMap:{none:null,minimal:"low",low:"low",medium:"medium",high:"high",highest:"high",xhigh:"high",max:"high"},unsupportedThinkingLevels:{none:"Cerebras GPT-OSS reasoning does not support the none effort level"}}},{match:{exact:["gemma-4-31b"]},capabilities:{thinking:!0,thinkingBudget:!0},request:{reasoning:"effort",defaultThinkingLevel:"max",effortMap:{none:"none",minimal:"high",low:"high",medium:"high",high:"high",highest:"high",xhigh:"high",max:"high"}}}],sources:["https://inference-docs.cerebras.ai/capabilities/reasoning","https://inference-docs.cerebras.ai/api-reference/chat-completions","https://inference-docs.cerebras.ai/capabilities/service-tiers"],reviewedAt:"2026-08-18",request:{serviceTierMap:{auto:"auto",standard:"default",flex:"flex",priority:"priority"}}},deepinfra:{id:"deepinfra",name:"DeepInfra",aliases:["deepinfra"],transport:"openai-chat",baseURL:"https://api.deepinfra.com/v1/openai",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:["standard","priority"]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[{match:{prefix:["deepseek-ai/DeepSeek-R1"]},capabilities:{thinking:!0,thinkingBudget:!0},request:{reasoning:"effort",defaultThinkingLevel:"max",effortMap:{none:"none",minimal:"low",low:"low",medium:"medium",high:"high",highest:"high",xhigh:"high",max:"high"}}}],sources:["https://docs.deepinfra.com/chat/reasoning","https://docs.deepinfra.com/api-reference/introduction","https://docs.deepinfra.com/chat/overview"],reviewedAt:"2026-08-18",request:{serviceTierMap:{auto:null,standard:null,priority:"priority"}}},sambanova:{id:"sambanova",name:"SambaNova Cloud",aliases:["sambanova","sambanova-cloud"],transport:"openai-chat",baseURL:"https://api.sambanova.ai/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.sambanova.ai/docs/en/api-reference/overview"],reviewedAt:"2026-08-17"},nebius:{id:"nebius",name:"Nebius AI Studio",aliases:["nebius"],transport:"openai-chat",baseURL:"https://api.tokenfactory.nebius.com/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://api.studio.nebius.com/docs"],reviewedAt:"2026-08-17"},novita:{id:"novita",name:"Novita AI",aliases:["novita","novita-ai"],transport:"openai-chat",baseURL:"https://api.novita.ai/v3/openai",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://novita.ai/docs/guides/llm-api"],reviewedAt:"2026-08-17"},hyperbolic:{id:"hyperbolic",name:"Hyperbolic",aliases:["hyperbolic"],transport:"openai-chat",baseURL:"https://api.hyperbolic.xyz/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.hyperbolic.xyz/docs/inference-api"],reviewedAt:"2026-08-17"},siliconflow:{id:"siliconflow",name:"SiliconFlow",aliases:["siliconflow"],transport:"openai-chat",baseURL:"https://api.siliconflow.com/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.siliconflow.com/en/userguide/quickstart"],reviewedAt:"2026-08-17"},friendli:{id:"friendli",name:"FriendliAI",aliases:["friendli","friendli-ai"],transport:"openai-chat",baseURL:"https://api.friendli.ai/serverless/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://friendli.ai/docs/guides/tool-calling"],reviewedAt:"2026-08-17"},"cloudflare-workers-ai":{id:"cloudflare-workers-ai",name:"Cloudflare Workers AI",aliases:["cloudflare-workers-ai","workers-ai"],transport:"openai-chat",baseURL:null,requiresApiURL:!0,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://developers.cloudflare.com/workers-ai/configuration/open-ai-compatibility/"],reviewedAt:"2026-08-17"},featherless:{id:"featherless",name:"Featherless AI",aliases:["featherless","featherless-ai"],transport:"openai-chat",baseURL:"https://api.featherless.ai/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://featherless.ai/docs/quickstart-guide"],reviewedAt:"2026-08-17"},nscale:{id:"nscale",name:"Nscale",aliases:["nscale"],transport:"openai-chat",baseURL:null,requiresApiURL:!0,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.nscale.com/docs/use-cases/chat"],reviewedAt:"2026-08-17"},ovhcloud:{id:"ovhcloud",name:"OVHcloud AI Endpoints",aliases:["ovhcloud","ovh"],transport:"openai-chat",baseURL:null,requiresApiURL:!0,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.ovhcloud.com/en/guides/public-cloud/ai-machine-learning/ai-endpoints-capabilities"],reviewedAt:"2026-08-17"},scaleway:{id:"scaleway",name:"Scaleway Generative APIs",aliases:["scaleway"],transport:"openai-chat",baseURL:"https://api.scaleway.ai/v1",requiresApiURL:!1,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://www.scaleway.com/en/developers/api/generative-apis"],reviewedAt:"2026-08-17"},"nvidia-nim":{id:"nvidia-nim",name:"NVIDIA NIM",aliases:["nvidia-nim","nim"],transport:"openai-chat",baseURL:null,requiresApiURL:!0,auth:{type:"bearer",required:!1},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.nvidia.com/nim/large-language-models/latest/getting-started.html"],reviewedAt:"2026-08-17"},"runpod-vllm":{id:"runpod-vllm",name:"RunPod vLLM",aliases:["runpod-vllm","runpod"],transport:"openai-chat",baseURL:null,requiresApiURL:!0,auth:{type:"bearer",required:!0},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.runpod.io/serverless/vllm/openai-compatibility"],reviewedAt:"2026-08-17"},"sagemaker-vllm":{id:"sagemaker-vllm",name:"SageMaker vLLM",aliases:["sagemaker-vllm","sagemaker"],transport:"openai-chat",baseURL:null,requiresApiURL:!0,auth:{type:"bearer",required:!1},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.aws.amazon.com/sagemaker/latest/dg/realtime-endpoints-openai-compatible.html"],reviewedAt:"2026-08-17"},vllm:{id:"vllm",name:"vLLM",aliases:["vllm"],transport:"openai-chat",baseURL:"http://localhost:8000/v1",requiresApiURL:!1,auth:{type:"bearer",required:!1},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.vllm.ai/en/latest/serving/openai_compatible_server/"],reviewedAt:"2026-08-17"},ollama:{id:"ollama",name:"Ollama",aliases:["ollama"],transport:"openai-chat",baseURL:"http://localhost:11434/v1",requiresApiURL:!1,auth:{type:"bearer",required:!1},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.ollama.com/api/openai-compatibility"],reviewedAt:"2026-08-17"},"lm-studio":{id:"lm-studio",name:"LM Studio",aliases:["lm-studio","lmstudio"],transport:"openai-chat",baseURL:"http://localhost:1234/v1",requiresApiURL:!1,auth:{type:"bearer",required:!1},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://lmstudio.ai/docs/developer/openai-compat"],reviewedAt:"2026-08-17"},"llama-cpp":{id:"llama-cpp",name:"llama.cpp Server",aliases:["llama-cpp","llama.cpp"],transport:"openai-chat",baseURL:"http://localhost:8080/v1",requiresApiURL:!1,auth:{type:"bearer",required:!1},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://github.com/ggml-org/llama.cpp/blob/master/tools/server/README.md"],reviewedAt:"2026-08-17"},localai:{id:"localai",name:"LocalAI",aliases:["localai","local-ai"],transport:"openai-chat",baseURL:"http://localhost:8080/v1",requiresApiURL:!1,auth:{type:"bearer",required:!1},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://localai.io/features/openai-functions/"],reviewedAt:"2026-08-17"},"baseten-engine":{id:"baseten-engine",name:"Baseten Inference Engine",aliases:["baseten-engine","truss"],transport:"openai-chat",baseURL:null,requiresApiURL:!0,auth:{type:"bearer",required:!1},defaults:{model:""},capabilities:{functions:!0,streaming:!0,structuredOutputs:!1,thinking:!1,multiTurn:!0,structuredOutputModes:["function"],serviceTiers:[]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.baseten.co/development/model/deployment/inference"],reviewedAt:"2026-08-17"}},ip={openai:"openai","openai-compatible":"openai-compatible",openai_compatible:"openai-compatible",compatible:"openai-compatible","openai-responses":"openai-responses",openai_responses:"openai-responses",responses:"openai-responses",anthropic:"anthropic",claude:"anthropic","google-gemini":"google-gemini",google_gemini:"google-gemini",gemini:"google-gemini",webllm:"webllm","azure-openai":"azure-openai",azure_openai:"azure-openai",azure:"azure-openai",deepseek:"deepseek","deepseek-responses":"deepseek-responses",deepseek_responses:"deepseek-responses",mistral:"mistral",cohere:"cohere",grok:"grok",xai:"grok","x-grok":"grok",x_grok:"grok",reka:"reka",together:"together","together-ai":"together",together_ai:"together",openrouter:"openrouter",orcarouter:"orcarouter",fireworks:"fireworks","fireworks-ai":"fireworks","huggingface-router":"huggingface-router",huggingface:"huggingface-router","hf-router":"huggingface-router","amazon-bedrock":"amazon-bedrock",bedrock:"amazon-bedrock","azure-foundry":"azure-foundry","azure-ai-foundry":"azure-foundry","microsoft-foundry":"azure-foundry","vertex-ai":"vertex-ai","vertex-openai":"vertex-ai",databricks:"databricks",baseten:"baseten",groq:"groq",cerebras:"cerebras",deepinfra:"deepinfra",sambanova:"sambanova","sambanova-cloud":"sambanova",nebius:"nebius",novita:"novita","novita-ai":"novita",hyperbolic:"hyperbolic",siliconflow:"siliconflow",friendli:"friendli","friendli-ai":"friendli","cloudflare-workers-ai":"cloudflare-workers-ai","workers-ai":"cloudflare-workers-ai",featherless:"featherless","featherless-ai":"featherless",nscale:"nscale",ovhcloud:"ovhcloud",ovh:"ovhcloud",scaleway:"scaleway","nvidia-nim":"nvidia-nim",nim:"nvidia-nim","runpod-vllm":"runpod-vllm",runpod:"runpod-vllm","sagemaker-vllm":"sagemaker-vllm",sagemaker:"sagemaker-vllm",vllm:"vllm",ollama:"ollama","lm-studio":"lm-studio",lmstudio:"lm-studio","llama-cpp":"llama-cpp","llama.cpp":"llama-cpp",localai:"localai","local-ai":"localai","baseten-engine":"baseten-engine",truss:"baseten-engine"},Ac=["openai","openai-compatible","openai-responses","anthropic","google-gemini","webllm","azure-openai","deepseek","deepseek-responses","mistral","cohere","grok","reka","together","openrouter","orcarouter","fireworks","huggingface-router","amazon-bedrock","azure-foundry","vertex-ai","databricks","baseten","groq","cerebras","deepinfra","sambanova","nebius","novita","hyperbolic","siliconflow","friendli","cloudflare-workers-ai","featherless","nscale","ovhcloud","scaleway","nvidia-nim","runpod-vllm","sagemaker-vllm","vllm","ollama","lm-studio","llama-cpp","localai","baseten-engine"];var Ui=(v=>(v.Grok46="grok-4.6",v.Grok45="grok-4.5",v.Grok45Latest="grok-4.5-latest",v.GrokBuildLatest="grok-build-latest",v.Grok43="grok-4.3",v.Grok43Latest="grok-4.3-latest",v.GrokLatest="grok-latest",v.Grok420Reasoning="grok-4.20-reasoning",v.Grok420Reasoning0309="grok-4.20-0309-reasoning",v.Grok420NonReasoning="grok-4.20-non-reasoning",v.Grok420NonReasoning0309="grok-4.20-0309-non-reasoning",v.Grok420MultiAgent="grok-4.20-multi-agent",v.Grok420MultiAgent0309="grok-4.20-multi-agent-0309",v.Grok41FastReasoning="grok-4-1-fast-reasoning",v.Grok41FastNonReasoning="grok-4-1-fast-non-reasoning",v.GrokVoiceThinkFast="grok-voice-think-fast-1.0",v.GrokVoiceFast="grok-voice-fast-1.0",v.Grok3="grok-3",v.Grok3Mini="grok-3-mini",v.Grok3Fast="grok-3-fast",v.Grok3MiniFast="grok-3-mini-fast",v))(Ui||{}),kb=(e=>(e.GrokEmbedSmall="grok-embed-small",e))(kb||{});var Tb=()=>({input:{format:"pcm16",mimeType:"audio/pcm",sampleRate:24e3,channels:1},output:{enabled:!0,voice:"eve",format:"pcm16",mimeType:"audio/pcm",sampleRate:24e3,channels:1,includeTranscript:!0},live:{turnTimeoutMs:3e4}}),Pb=()=>structuredClone({model:"grok-4.6",...Qe()}),DP=()=>structuredClone({...Pb(),model:"grok-4.6"}),jP=()=>structuredClone({...Qe(),model:"grok-voice-think-fast-1.0",audio:Tb(),stream:!1}),bc=r=>r==="grok-voice-think-fast-1.0"||r==="grok-voice-fast-1.0"||r.startsWith("grok-voice-"),Cc=(r,e)=>At(At(Tb(),r),e),sp=(r,e,t)=>bc(r)&&Zt(Cc(e,t)),ap=r=>Li({...r,apiName:"grok-realtime-audio",providerName:"Grok Realtime",wsURL:e=>`wss://api.x.ai/v1/realtime?model=${encodeURIComponent(e)}`,createSessionUpdate:({request:e,audio:t})=>{let n=e.messages.filter(s=>s.role==="system").map(s=>s.content).join(`
1258
1258
  `),o=t.input,i=t.output;return{type:"session.update",session:{voice:typeof i?.voice=="object"?i.voice.id:i?.voice??"eve",...n?{instructions:n}:{},turn_detection:null,audio:{input:{format:{type:"audio/pcm",rate:o?.sampleRate??24e3}},output:{format:{type:"audio/pcm",rate:i?.sampleRate??24e3}}}}}}});var Ib=op,GP=ip,Gi=()=>Ac.map(r=>{let e=Ib[r];return{id:r,name:e.name,aliases:e.aliases,transport:e.transport,...e.baseURL?{baseURL:e.baseURL}:{},requiresApiURL:e.requiresApiURL,...e.endpoint?{endpoint:e.endpoint}:{},...e.defaults.model?{defaultModel:e.defaults.model}:{},...e.defaults.embedModel?{defaultEmbedModel:e.defaults.embedModel}:{},authentication:e.auth,operations:e.operations,modelRules:e.modelRules,capabilities:e.capabilities,sources:e.sources,reviewedAt:e.reviewedAt}}),up=r=>{let e=wc(r);return Gi().find(t=>t.id===e.id)},Mb=r=>{let e=GP[r.toLowerCase()];if(!e)throw new Error(`Unknown AI profile "${r}". Use one of: ${Ac.join(", ")}`);return e},wc=r=>Ib[Mb(r)],BP=(r,e)=>(e.match.exact?.includes(r)??!1)||(e.match.prefix?.some(t=>r.startsWith(t))??!1)||(e.match.contains?.some(t=>r.toLowerCase().includes(t.toLowerCase()))??!1),Eb=(r,e)=>r.modelRules.find(t=>BP(e,t)),qP=r=>({images:{supported:r.images??!1,formats:r.images?["image/jpeg","image/png"]:[]},audio:{supported:r.audio??!1,formats:r.audio?["wav","mp3","pcm16"]:[],output:{supported:r.audioOutput??!1,formats:r.audioOutput?["wav","mp3","pcm16"]:[]}},files:r.files?{supported:!0,formats:["application/pdf","text/plain"],uploadMethod:r.files.uploadMethod}:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:r.webSearch??!1,contextFetching:!1}}),Yn=(r,e)=>{let t=wc(r),n=Eb(t,e),o={...t.capabilities,...n?.capabilities};return{functions:o.functions,streaming:o.streaming,structuredOutputs:o.structuredOutputs,structuredOutputModes:o.structuredOutputModes,hasThinkingBudget:o.thinkingBudget??o.thinking??!1,hasShowThoughts:o.showThoughts??o.thinking??!1,media:qP(o),caching:o.caching?{supported:!0,types:[...o.caching.types],cacheBreakpoints:o.caching.cacheBreakpoints}:{supported:!1,types:[]},thinking:o.thinking,multiTurn:o.multiTurn,serviceTiers:o.serviceTiers??[]}},zP=r=>{let e=r.modelRules.flatMap(i=>i.response?.reasoningFields??[]),t=r.modelRules.flatMap(i=>i.response?.reasoningDetailsFields??[]),n=r.modelRules.find(i=>i.replay?.assistantReasoningField)?.replay?.assistantReasoningField,o=r.modelRules.find(i=>i.replay?.assistantReasoningDetailsField)?.replay?.assistantReasoningDetailsField;return!n&&!o&&e.length===0&&t.length===0?"none":{...n||e[0]?{assistantField:n??e[0]}:{},responseFields:[...new Set(e)],...o?{assistantDetailsField:o}:{},responseDetailsFields:[...new Set(t)]}},HP=r=>{let e=r.messages;if(Array.isArray(e))for(let t of e){if(!t||typeof t!="object")continue;let n=t.content;if(Array.isArray(n))for(let o of n){if(!o||typeof o!="object")continue;let i=o;if(i.type!=="image_url")continue;let s=i.image_url;typeof s=="string"?i.image_url={url:s}:s&&typeof s=="object"&&(i.image_url={url:s.url})}}},cp=r=>Array.isArray(r)?r.map(cp):!r||typeof r!="object"?r:Object.fromEntries(Object.entries(r).map(([e,t])=>[e.replace(/[A-Z]/g,n=>`_${n.toLowerCase()}`),cp(t)])),lp=(r,e,t)=>{if(!e)return;let o=t.thinkingTokenBudget??(typeof r.reasoning_effort=="string"?r.reasoning_effort:e.defaultThinkingLevel),i=o?e.unsupportedThinkingLevels?.[o]:void 0;if(i)throw new Error(i);let s=o?e.effortMap?.[o]:void 0;o&&e.effortMap&&(s===null?delete r.reasoning_effort:s!==void 0&&(r.reasoning_effort=s));let a=o!==void 0&&o!=="none"&&(s!==null||!e.effortMap),c=o!==void 0&&s!==null;if(e.thinkingBoolean){let l=r,u=e.thinkingBoolean.path;for(let d of u.slice(0,-1)){let p=l[d],m=p&&typeof p=="object"&&!Array.isArray(p)?{...p}:{};l[d]=m,l=m}l[u[u.length-1]]=o!=="none"}if(e.reasoning==="thinking-object"?r.thinking={type:a?"enabled":"disabled"}:e.reasoning==="openrouter"&&(c&&typeof r.reasoning_effort=="string"&&(r.reasoning={effort:r.reasoning_effort}),delete r.reasoning_effort),e.toolChoice==="unforced"){let l=r.tool_choice,u=l&&typeof l=="object"?l.function?.name:void 0,d=t.functionCallSource==="ax"&&u==="__axOutput";if(l==="required"||l!==void 0&&l!=="auto"&&l!=="none"&&!d)throw new Error("This deployment profile does not support explicitly forced tool choices");l==="none"&&delete r.tools,delete r.tool_choice}if(a)for(let l of e.dropWhenThinking??[])delete r[l];for(let l of e.dropFields??[])delete r[l];for(let[l,u]of Object.entries(e.copyFields??{}))r[l]!==void 0&&(r[u]=r[l]);for(let[l,u]of Object.entries(e.renameFields??{}))r[l]!==void 0&&(r[u]=r[l],delete r[l]);for(let[l,u]of Object.entries(e.enumMaps??{})){let d=r[l];typeof d=="string"&&d in u&&(r[l]=u[d])}for(let[l,u]of Object.entries(e.constructObjects??{})){let d=Object.fromEntries(Object.entries(u).filter(([,p])=>r[p]!==void 0).map(([p,m])=>[p,r[m]]));Object.keys(d).length>0&&(r[l]=d)}if(e.imageURLShape==="object"&&HP(r),e.optionDialect==="search-parameters"){let l=t.searchParameters??t.search_parameters;l!==void 0&&(r.search_parameters=cp(l))}},vc=(r,e,t)=>{let n=t?.find(s=>s.name===e||s.aliases?.includes(e))?.supported;if(!n)return r;let o=n.thinkingBudget!==void 0||n.showThoughts!==void 0,i=r.structuredOutputModes;if(n.structuredOutputModes!==void 0)i=[...n.structuredOutputModes];else if(n.structuredOutputs!==void 0){let s=(i??[]).filter(a=>a!=="native");i=n.structuredOutputs?["native",...s]:s}return{...r,hasThinkingBudget:n.thinkingBudget??r.hasThinkingBudget??!1,hasShowThoughts:n.showThoughts??r.hasShowThoughts??!1,structuredOutputs:i?i.includes("native"):n.structuredOutputs??r.structuredOutputs??!1,...i?{structuredOutputModes:i}:{},thinking:o?!!(n.thinkingBudget||n.showThoughts):r.thinking,serviceTiers:n.serviceTiers!==void 0?[...n.serviceTiers]:r.serviceTiers}},VP=(r,e,t,n,o)=>{let i={...e},s=String(e.model),a=Eb(r,s),c=vc(Rc(r,Yn(r.id,s),o),s,n),l=t.serviceTier??i.service_tier;delete i.service_tier;let u=pn({requested:l,supported:c.serviceTiers,mapping:a?.request?.serviceTierMap??r.request?.serviceTierMap,provider:r.id,model:s});if(u!==void 0&&(i.service_tier=u),t.thinkingTokenBudget&&t.thinkingTokenBudget!=="none"&&!c.thinking)throw new Error(`Thinking is not verified for profile ${r.id} and model ${s}; add an exact modelInfo override to opt in`);let d=i.response_format;if(d?.type==="json_schema"&&!c.structuredOutputs)throw new Error(`Structured output is not verified for profile ${r.id} and model ${s}: native JSON Schema is unsupported`);if(d?.type==="json_object"&&!c.structuredOutputModes?.includes("json_object"))throw new Error(`Structured output is not verified for profile ${r.id} and model ${s}: JSON object mode is unsupported`);return lp(i,r.request,t),lp(i,a?.request,t),i},Ob=(r,e)=>{if(typeof e.apiURL=="string"&&e.apiURL.length>0)return e.apiURL;if(r.endpoint){let t={...r.endpoint.defaults};for(let[a,c]of Object.entries(r.endpoint.fields??{}))for(let l of c)e[l]!==void 0&&e[l]!==""&&(t[a]=e[l]);for(let a of r.endpoint.required)if(t[a]===void 0||t[a]==="")throw new Error(`${r.name} endpoint field ${a} is required`);let n=String(t[r.endpoint.hostField]),o=n.includes("://")?n.replace(/\/$/,""):`${r.endpoint.scheme??"https"}://${n}${r.endpoint.hostSuffix}`,i=r.endpoint.path;for(let[a,c]of Object.entries(t)){let l=String(c);r.endpoint.normalizers?.[a]==="api-version"&&(l=new URLSearchParams(l.includes("api-version=")?l:"").get("api-version")??l),i=i.replaceAll(`{${a}}`,encodeURIComponent(l)),t[a]=l}let s=new URL(`${o}${i}`);if(r.endpoint.apiVersionField){let a=r.endpoint.apiVersionField,c=r.endpoint.normalizers?.[a],l=t[a];c&&l!==void 0&&l!==""&&s.searchParams.set(c,String(l))}return s.toString().replace(/\/$/,"")}if(r.baseURL)return r.baseURL;throw new Error(`${r.name} requires apiURL`)},Rc=(r,e,t)=>{let n=r.capabilityGates?.structuredOutputs;if(!n)return e;let s=(String(t[n.option]??"").match(/\d{4}-\d{2}-\d{2}/)?.[0]??"")>=n.min,a=(e.structuredOutputModes??[]).filter(c=>c!=="native");return{...e,structuredOutputs:s,structuredOutputModes:s?["native",...a]:a}},Fb=(r,e,t)=>{if(r.auth.required&&!e&&!t)throw new Error(`${r.name} API key not set`);return e??""},WP=r=>r?.map(e=>{let t=e;return t.config?{...e,modelConfig:{...t.config,...t.modelConfig},...t.config.serviceTier!==void 0?{serviceTier:Bt(t.config.serviceTier)}:{}}:e}),_b=(r,e)=>!e||r.auth.type==="none"?{}:r.auth.type==="api-key-header"?{[r.auth.header??"api-key"]:e}:r.auth.type==="x-api-key"?{"x-api-key":e}:{Authorization:`Bearer ${e}`},Di=class extends yo{profileSpec;profileApiURL;constructor(e){let t=wc(e.name);if(t.transport!=="openai-chat")throw new Error(`${t.id} is not an OpenAI Chat profile`);let n=Fb(t,e.apiKey,e.credentialProvider),o=Ob(t,e),i={model:t.defaults.model,...t.defaults.embedModel?{embedModel:t.defaults.embedModel}:{},...Qe(),...e.config},a=t.operations.realtime?.dialect==="xai-realtime"?{apiName:`${t.name} Realtime`,shouldUse:sp,resolveAudioConfig:Cc,createApi:ap}:void 0;super({apiKey:n||"local-no-key",credentialProvider:e.credentialProvider,credentialProfile:t.id,apiURL:o,config:i,options:e.options,modelInfo:e.modelInfo??[],models:WP(e.models),supportFor:c=>vc(Rc(t,Yn(t.id,c),e),c,e.modelInfo),reasoningContentMode:zP(t),realtime:a,chatReqUpdater:(c,l)=>VP(t,c,{...e.options??{},...l},e.modelInfo,e)}),this.setName(t.name),this.setHeaders(async()=>_b(t,n)),this.profileSpec=t,this.profileApiURL=o}async transcribe(e,t){let n=this.profileSpec.operations.transcribe;if(!n)throw new Error(`Transcription is not supported by profile ${this.profileSpec.id}`);let o=this.getOptions(),i=typeof e.model=="string"?e.model:void 0,s=n.dialect==="xai-transcription"?{language:e.language,keyterm:e.prompt,format:!0}:{model:i,language:e.language,prompt:e.prompt,temperature:e.temperature,response_format:e.responseFormat??"json"};return await Ti({url:`${this.profileApiURL.replace(/\/$/,"")}${n.path}`,headers:await this.buildHeaders({},{operation:"transcribe",method:"POST",url:`${this.profileApiURL.replace(/\/$/,"")}${n.path}`}),audio:e.audio,fields:s,fetch:t?.fetch??o.fetch,abortSignal:t?.abortSignal??o.abortSignal})}async speak(e,t){let n=this.profileSpec.operations.speak;if(!n)throw new Error(`Speech is not supported by profile ${this.profileSpec.id}`);let o=this.getOptions(),i=e.format??"mp3",s=typeof e.model=="string"?e.model:void 0,a=typeof e.voice=="object"?e.voice.id:e.voice,c;if(n.dialect==="xai-speech"){let l=i==="pcm16"||i==="raw"?"pcm":i==="ulaw"?"mulaw":i;c={text:e.text,voice_id:a??"eve",language:e.language??"auto",output_format:{codec:l,...e.sampleRate?{sample_rate:e.sampleRate}:{}},...e.speed!==void 0?{speed:e.speed}:{}}}else n.dialect==="mistral-speech"?c={model:s??"voxtral-mini-tts-2603",input:e.text,response_format:i,...a?{voice_id:a}:{}}:c={model:s,input:e.text,voice:a??"alloy",response_format:i==="pcm"?"pcm16":i,...e.speed!==void 0?{speed:e.speed}:{}};return await wr({url:`${this.profileApiURL.replace(/\/$/,"")}${n.path}`,headers:await this.buildHeaders({},{operation:"speak",method:"POST",url:`${this.profileApiURL.replace(/\/$/,"")}${n.path}`}),body:c,format:i,transcript:e.text,fetch:t?.fetch??o.fetch,abortSignal:t?.abortSignal??o.abortSignal})}},ji=class extends Ao{constructor(e){let t=wc(e.name);if(t.transport!=="openai-responses")throw new Error(`${t.id} is not an OpenAI Responses profile`);let n=Fb(t,e.apiKey,e.credentialProvider),o=Ob(t,e),i={model:t.defaults.model,...t.defaults.embedModel?{embedModel:t.defaults.embedModel}:{},stream:!0,...e.config};super({apiKey:n||"local-no-key",credentialProvider:e.credentialProvider,credentialProfile:t.id,apiURL:o,config:i,options:e.options,modelInfo:e.modelInfo??[],models:e.models,supportFor:s=>vc(Rc(t,Yn(t.id,s),e),s,e.modelInfo),responsesReqUpdater:s=>{let a={...s},c=String(s.model),l=vc(Rc(t,Yn(t.id,c),e),c,e.modelInfo),u=pn({requested:a.service_tier,supported:l.serviceTiers,mapping:t.request?.serviceTierMap,provider:t.id,model:c});if(delete a.service_tier,u!==void 0&&(a.service_tier=u),lp(a,t.request,e.options??{}),t.request?.reasoningObjectFields&&a.reasoning){let d=a.reasoning;a.reasoning=Object.fromEntries(t.request.reasoningObjectFields.filter(p=>d[p]!==void 0).map(p=>[p,d[p]]))}return a}}),this.setName(t.name),this.setHeaders(async()=>_b(t,n))}};var Sc=(n=>(n.RekaCore="reka-core",n.RekaFlash="reka-flash",n.RekaEdge="reka-edge",n))(Sc||{});var dp=[{name:"reka-core",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15},{name:"reka-flash",currency:"usd",promptTokenCostPer1M:.8,completionTokenCostPer1M:2},{name:"reka-edge",currency:"usd",promptTokenCostPer1M:.4,completionTokenCostPer1M:1}];var Bi=(p=>(p.Llama31_8B_Instruct="Llama-3.1-8B-Instruct-q4f32_1-MLC",p.Llama31_70B_Instruct="Llama-3.1-70B-Instruct-q4f16_1-MLC",p.Llama32_1B_Instruct="Llama-3.2-1B-Instruct-q4f32_1-MLC",p.Llama32_3B_Instruct="Llama-3.2-3B-Instruct-q4f32_1-MLC",p.Mistral7B_Instruct="Mistral-7B-Instruct-v0.3-q4f32_1-MLC",p.Phi35_Mini_Instruct="Phi-3.5-mini-instruct-q4f32_1-MLC",p.Gemma2_2B_Instruct="gemma-2-2b-it-q4f32_1-MLC",p.Gemma2_9B_Instruct="gemma-2-9b-it-q4f32_1-MLC",p.Qwen2_5_0_5B_Instruct="Qwen2.5-0.5B-Instruct-q4f32_1-MLC",p.Qwen2_5_1_5B_Instruct="Qwen2.5-1.5B-Instruct-q4f32_1-MLC",p.Qwen2_5_3B_Instruct="Qwen2.5-3B-Instruct-q4f32_1-MLC",p.Qwen2_5_7B_Instruct="Qwen2.5-7B-Instruct-q4f32_1-MLC",p))(Bi||{});var qi=[{name:"Llama-3.1-8B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:128e3,maxTokens:4096},{name:"Llama-3.1-70B-Instruct-q4f16_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:128e3,maxTokens:4096,isExpensive:!0},{name:"Llama-3.2-1B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:128e3,maxTokens:2048},{name:"Llama-3.2-3B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:128e3,maxTokens:2048},{name:"Mistral-7B-Instruct-v0.3-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:32768,maxTokens:4096},{name:"Phi-3.5-mini-instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:128e3,maxTokens:4096},{name:"gemma-2-2b-it-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:8192,maxTokens:2048},{name:"gemma-2-9b-it-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:8192,maxTokens:2048},{name:"Qwen2.5-0.5B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:32768,maxTokens:2048},{name:"Qwen2.5-1.5B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:32768,maxTokens:2048},{name:"Qwen2.5-3B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:32768,maxTokens:2048},{name:"Qwen2.5-7B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:32768,maxTokens:4096}];var pp=[{name:"grok-4.6",currency:"USD",supported:{thinkingBudget:!0,structuredOutputs:!0}},{name:"grok-4.5",currency:"USD",promptTokenCostPer1M:2,cacheReadTokenCostPer1M:.3,completionTokenCostPer1M:6,contextWindow:5e5,aliases:["grok-4.5-latest","grok-build-latest"],supported:{thinkingBudget:!0,structuredOutputs:!0}},{name:"grok-4.3",currency:"USD",promptTokenCostPer1M:1.25,cacheReadTokenCostPer1M:.2,completionTokenCostPer1M:2.5,contextWindow:1e6,aliases:["grok-4.3-latest","grok-latest"],supported:{thinkingBudget:!0,structuredOutputs:!0}},{name:"grok-4.20-reasoning",currency:"USD",promptTokenCostPer1M:1.25,cacheReadTokenCostPer1M:.2,completionTokenCostPer1M:2.5,contextWindow:2e6,aliases:["grok-4.20-0309-reasoning","grok-4.20-reasoning-latest","grok-4.20","grok-4.20-0309"],supported:{structuredOutputs:!0}},{name:"grok-4.20-non-reasoning",currency:"USD",promptTokenCostPer1M:1.25,cacheReadTokenCostPer1M:.2,completionTokenCostPer1M:2.5,contextWindow:2e6,aliases:["grok-4.20-0309-non-reasoning","grok-4.20-non-reasoning-latest"],supported:{structuredOutputs:!0}},{name:"grok-4.20-multi-agent",currency:"USD",promptTokenCostPer1M:1.25,cacheReadTokenCostPer1M:.2,completionTokenCostPer1M:2.5,contextWindow:2e6,aliases:["grok-4.20-multi-agent-0309","grok-4.20-multi-agent-latest"],supported:{structuredOutputs:!0}},{name:"grok-4-1-fast-reasoning",currency:"USD",promptTokenCostPer1M:.2,cacheReadTokenCostPer1M:.05,completionTokenCostPer1M:.5,contextWindow:2e6,aliases:["grok-4-1-fast-reasoning-latest"],supported:{structuredOutputs:!0}},{name:"grok-4-1-fast-non-reasoning",currency:"USD",promptTokenCostPer1M:.2,cacheReadTokenCostPer1M:.05,completionTokenCostPer1M:.5,contextWindow:2e6,aliases:["grok-4-1-fast-non-reasoning-latest"],supported:{structuredOutputs:!0}},{name:"grok-voice-think-fast-1.0",currency:"USD"},{name:"grok-voice-fast-1.0",currency:"USD"},{name:"grok-3",currency:"USD",promptTokenCostPer1M:3,completionTokenCostPer1M:15},{name:"grok-3-mini",currency:"USD",promptTokenCostPer1M:.3,completionTokenCostPer1M:.5,supported:{thinkingBudget:!0}},{name:"grok-3-fast",currency:"USD",promptTokenCostPer1M:5,completionTokenCostPer1M:25},{name:"grok-3-mini-fast",currency:"USD",promptTokenCostPer1M:.6,completionTokenCostPer1M:4,supported:{thinkingBudget:!0}}];var KP={openai:{displayName:"OpenAI",defaultModel:"gpt-5-mini",defaultEmbedModel:"text-embedding-3-small",isDynamic:!1,modelInfo:Ei},"openai-responses":{displayName:"OpenAI Responses",defaultModel:"gpt-4o",defaultEmbedModel:"text-embedding-ada-002",isDynamic:!1,modelInfo:Oi},"azure-openai":{displayName:"Azure OpenAI",isDynamic:!0,modelInfo:[]},anthropic:{displayName:"Anthropic",defaultModel:"claude-3-7-sonnet-latest",isDynamic:!1,modelInfo:lo},"google-gemini":{displayName:"Google Gemini",defaultModel:"gemini-2.5-flash",defaultEmbedModel:"gemini-embedding-2",isDynamic:!1,modelInfo:Ii},cohere:{displayName:"Cohere",defaultModel:"command-r-plus",isDynamic:!1,modelInfo:jd},deepseek:{displayName:"DeepSeek",defaultModel:"deepseek-v4-flash",isDynamic:!1,modelInfo:cc},"deepseek-responses":{displayName:"DeepSeek Responses",defaultModel:"deepseek-v4-flash",isDynamic:!1,modelInfo:cc},mistral:{displayName:"Mistral AI",defaultModel:"mistral-small-latest",isDynamic:!1,modelInfo:Vd},reka:{displayName:"Reka",defaultModel:"reka-core",isDynamic:!1,modelInfo:dp},grok:{displayName:"xAI Grok",defaultModel:"grok-4.6",isDynamic:!1,modelInfo:pp},webllm:{displayName:"WebLLM",defaultModel:"Llama-3.2-3B-Instruct-q4f32_1-MLC",isDynamic:!1,modelInfo:qi}},JP={...Object.fromEntries(Gi().map(r=>[r.id,{displayName:r.name,isDynamic:!0,modelInfo:[]}])),...KP},YP=new Map(Gi().map(r=>[r.id,r])),QP=["none","minimal","low","medium","high","highest"],ZP=(r,e)=>r.modelRules.find(t=>(t.match.exact?.includes(e)??!1)||(t.match.prefix?.some(n=>e.startsWith(n))??!1)||(t.match.contains?.some(n=>e.toLowerCase().includes(n.toLowerCase()))??!1)),Lb=(r,e,t)=>{if(!t)return[];let n=ZP(r,e)?.request?.unsupportedThinkingLevels;return QP.filter(o=>!Object.hasOwn(n??{},o))},XP=(r,e,t)=>t==="embeddings"?[]:r==="openai"&&xo(e.name)||r==="google-gemini"&&lc(e.name)||r==="grok"&&bc(e.name)?[]:e.supported?.serviceTiers!==void 0?[...e.supported.serviceTiers]:[...Yn(r,e.name).serviceTiers??[]],eI=(r,e)=>{let t=e??r.defaultModel??"",n=Yn(r.id,t);return{thinking:n.thinking,thinkingLevels:Lb(r,t,n.hasThinkingBudget??!1),serviceTiers:[...n.serviceTiers??[]]}},tI=r=>{let e={...r};return r.aliases&&(e.aliases=[...r.aliases]),r.supported&&(e.supported={...r.supported,...r.supported.structuredOutputModes?{structuredOutputModes:[...r.supported.structuredOutputModes]}:void 0,...r.supported.serviceTiers?{serviceTiers:[...r.supported.serviceTiers]}:void 0}),r.notSupported&&(e.notSupported={...r.notSupported}),r.audio&&(e.audio={...r.audio}),e},nI=(r,e,t)=>{let n=Nb(t),o=t.name.toLowerCase(),i=t.supported?.thinkingBudget??!1;return{thinkingBudget:i,thinkingLevels:Lb(e,t.name,i),showThoughts:t.supported?.showThoughts??!1,structuredOutputs:t.supported?.structuredOutputs??!1,temperature:!(t.notSupported?.temperature??!1),topP:!(t.notSupported?.topP??!1),audioInput:t.audio?.input??n==="audio",audioOutput:t.audio?.output??(n==="audio"&&!o.includes("whisper")&&!o.includes("transcription")),serviceTiers:XP(r,t,n)}},Nb=r=>{let e=r.name.toLowerCase();return r.audio?.input||r.audio?.output||e.includes("audio")||e.includes("realtime")||e.includes("voice")||e.includes("whisper")||e.includes("native-audio")?"audio":e.includes("embedding")||e.includes("embed")?"embeddings":e.includes("code")||e.includes("codex")||e.includes("coder")||e.includes("codestral")?"code":"text"},rI=r=>{if(r===void 0)return;let e=Array.isArray(r)?r:[r];if(!e.includes("all"))return new Set(e)},oI=(r,e)=>!e||e.has(r.type)?!0:e.has("text")&&r.type==="code",kc=r=>{let e=typeof r?.promptTokenCostPer1M=="number",t=typeof r?.completionTokenCostPer1M=="number";return!e&&!t?Number.POSITIVE_INFINITY:(r?.promptTokenCostPer1M??0)+(r?.completionTokenCostPer1M??0)},iI=(r,e)=>{let t=kc(r)-kc(e);return t!==0?t:r.name.localeCompare(e.name)},sI=(r,e,t,n,o)=>{let i=tI(o),s=[t,n].filter(a=>a!==void 0);return{...i,provider:r,type:Nb(o),isDefault:s.some(a=>o.name===a||(o.aliases?.includes(a)??!1)),capabilities:nI(r,e,o)}},aI=r=>{let e=rI(r?.type);return Object.entries(JP).map(([t,{displayName:n,defaultModel:o,defaultEmbedModel:i,isDynamic:s,modelInfo:a}])=>{let c=YP.get(t);if(!c)throw new Error(`Missing AI profile metadata for catalog provider ${t}`);let l=a.map(u=>sI(t,c,o,i,u)).filter(u=>oI(u,e)).sort(iI);return{name:t,displayName:n,...o!==void 0?{defaultModel:o}:void 0,...i!==void 0?{defaultEmbedModel:i}:void 0,isDynamic:s,capabilities:eI(c,o),models:l}}).sort((t,n)=>{let o=kc(t.models[0])-kc(n.models[0]);return o!==0?o:t.displayName.localeCompare(n.displayName)})};var Tc=r=>r.includes("gemini-3"),$b=r=>r==="gemini-3.7-flash"||r==="gemini-3.6-flash"||r==="gemini-3.5-flash-lite",cI=new Set(["none","minimal","low","medium","high","highest"]),Db=r=>{let e=r.toLowerCase();if(Tc(e))return e.includes("-image")?"image":e.includes("gemini-3-pro")?"legacy-pro":e.includes("gemini-3.7-flash")||e.includes("gemini-3.1-pro")?"no-minimal":"full"},Ub=(r,e)=>{switch(Db(r)){case"image":return e==="minimal"||e==="low"?"minimal":"high";case"legacy-pro":return e==="minimal"||e==="low"?"low":"high";case"no-minimal":return e==="minimal"?"low":e;case"full":case void 0:return e}},mp=({model:r,direct:e,logicalLevel:t,showThoughts:n,thinkingLevelMapping:o,thinkingTokenBudgetLevels:i})=>{let s={},a=Db(r);if(e?.thinkingTokenBudget!==void 0){if(a)throw new Error(`Gemini 3 models (${r}) do not support numeric thinkingTokenBudget. Use a logical thinkingTokenBudget level or thinkingLevel instead.`);s.thinkingBudget=r.includes("gemini-2.5-pro")&&e.thinkingTokenBudget===0?i?.minimal??200:e.thinkingTokenBudget}if(e?.thinkingLevel!==void 0&&a&&(s.thinkingLevel=Ub(r,e.thinkingLevel)),e?.includeThoughts!==void 0&&(s.includeThoughts=e.includeThoughts),t!==void 0)if(typeof t=="number"){if(a)throw new Error(`Gemini 3 models (${r}) do not support numeric thinkingTokenBudget. Use a logical thinkingTokenBudget level instead.`);s.thinkingBudget=r.includes("gemini-2.5-pro")&&t===0?i?.minimal??200:t,delete s.thinkingLevel}else{if(!cI.has(t))throw new Error(`Unsupported Gemini thinkingTokenBudget level '${String(t)}'`);if(a){let c=t==="none"?"minimal":t,l=c==="highest"?"high":c,u=o?.[c]??l;s.thinkingLevel=Ub(r,u),delete s.thinkingBudget}else{let c=i?.minimal??200,l={none:r.includes("gemini-2.5-pro")?c:0,minimal:c,low:i?.low??800,medium:i?.medium??5e3,high:i?.high??1e4,highest:i?.highest??24500};s.thinkingBudget=l[t],delete s.thinkingLevel}}return n!==void 0&&(s.includeThoughts=n),t==="none"&&(s.includeThoughts=!1),s.thinkingLevel!==void 0&&delete s.thinkingBudget,s},lI=(r,e)=>e?"v1beta1":"v1",Pc=r=>{if(!r||typeof r!="object")return r;let e={...r},t=Array.isArray(e.type)&&e.type.length===2&&e.type.includes("null");return delete e.default,delete e.optional,delete e.oneOf,delete e.anyOf,Array.isArray(e.type)&&!t&&(e.type=e.type.includes("object")?"object":e.type[0]??"string"),e.properties&&typeof e.properties=="object"&&(e.properties=Object.fromEntries(Object.entries(e.properties).map(([n,o])=>[n,Pc(o)]))),e.items&&(e.items=Pc(e.items)),e},uI=(r,e,t)=>{for(let n=e-1;n>=0;n--){let o=r[n];if(o?.role!=="assistant"||!o.functionCalls)continue;let i=o.functionCalls.find(s=>s.id===t);if(i?.function?.name)return i.function.name}return t},gp=(r,e,t)=>{let n=t.protocolResult?.value,o=[{functionResponse:{name:uI(r,e,t.functionId),response:{result:t.result,...n?.structuredContent?{structuredContent:n.structuredContent}:{}}}}];for(let i of t.content??[])i.type==="text"?o.push({text:i.text}):i.type==="image"?o.push({inlineData:{mimeType:i.mimeType,data:i.image}}):i.type==="audio"?o.push({inlineData:{mimeType:i.mimeType??"application/octet-stream",data:i.data}}):i.type==="file"?o.push({inlineData:{mimeType:i.mimeType,data:i.data}}):o.push({text:i.cachedContent??[i.title,i.description,i.url].filter(Boolean).join(`
1259
- `)});return o},jb=[{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"}],Gb=()=>structuredClone({model:"gemini-2.5-flash",embedModel:"text-embedding-005",safetySettings:jb,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...Qe()}),dI=()=>structuredClone({model:"gemini-2.0-flash",embedModel:"text-embedding-005",safetySettings:jb,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...Rr()}),fp=class{constructor(e,t,n,o,i,s,a){this.config=e;this.vertexConfig=t;this.endpointId=n;this.apiKey=o;this.credentialProvider=i;this.options=s;this.vertexApiURLForModel=a;if(!this.isVertex&&this.config.autoTruncate)throw new Error("Auto truncate is not supported for GoogleGemini");mp({model:this.config.model,direct:this.config.thinking,thinkingLevelMapping:this.config.thinkingLevelMapping,thinkingTokenBudgetLevels:this.config.thinkingTokenBudgetLevels})}tokensUsed;models;serviceTierSupportFor;setModels(e){this.models=e}setServiceTierSupportFor(e){this.serviceTierSupportFor=e}resolveServiceTier(e,t,n=!1){let o=t.serviceTier??this.options?.serviceTier??this.config.serviceTier,i=Bt(o),s=n||this.isVertex;if(s&&i&&i!=="auto")throw new Error(`Gemini inference service tiers are not supported by ${n?"the Live API":"Vertex AI"}`);return pn({requested:o,supported:s?[]:this.serviceTierSupportFor?.(e)??["standard","flex","priority"],mapping:{auto:null,standard:"standard",flex:"flex",priority:"priority"},provider:"Google Gemini",model:e})}getEffectiveMappings(e){let t=this.models?.find(n=>n.model===e);return{thinkingLevelMapping:{...this.config.thinkingLevelMapping,...t?.thinkingLevelMapping??{}},thinkingTokenBudgetLevels:{...this.config.thinkingTokenBudgetLevels,...t?.thinkingTokenBudgetLevels??{}}}}getTokenUsage(){return this.tokensUsed}get isVertex(){return this.vertexConfig!==void 0}getVertexApiURL(e,t){return this.isVertex?this.vertexApiURLForModel?.(e,t):void 0}async transcribe(e,t){let n=e.model??"gemini-2.5-flash",o=typeof this.apiKey=="function"?await this.apiKey():this.apiKey,i=this.isVertex?`${this.getVertexApiURL(n,t?.beta)}/models/${n}:generateContent`:`https://generativelanguage.googleapis.com/v1beta/models/${n}:generateContent`,s={contents:[{role:"user",parts:[{inlineData:{mimeType:e.audio.mimeType??Gt(e.audio.format,e.audio.sampleRate),data:e.audio.data}},{text:e.prompt??"Generate a transcript of the speech in this audio."}]}]},a={"Content-Type":"application/json",...this.isVertex&&o?{Authorization:`Bearer ${o}`}:!this.isVertex&&o?{"x-goog-api-key":o}:{}},c=await(t?.fetch??globalThis.fetch)(i,{method:"POST",headers:this.credentialProvider?{...a,...await this.credentialProvider({profile:"google-gemini",operation:"transcribe",method:"POST",url:i})}:a,body:JSON.stringify(s),signal:t?.abortSignal});if(!c.ok)throw new Error(`Gemini transcription failed: ${c.status} ${c.statusText}`);return{text:(await c.json()).candidates?.[0]?.content?.parts?.map(d=>"text"in d?d.text:"").join("").trim()??""}}async speak(e,t){let n=e.model??"gemini-2.5-flash-preview-tts",o=typeof this.apiKey=="function"?await this.apiKey():this.apiKey,i=this.isVertex?`${this.getVertexApiURL(n,t?.beta)}/models/${n}:generateContent`:`https://generativelanguage.googleapis.com/v1beta/models/${n}:generateContent`,s=typeof e.voice=="object"?e.voice.id:e.voice??"Kore";return await wr({url:i,headers:{...this.isVertex&&o?{Authorization:`Bearer ${o}`}:!this.isVertex&&o?{"x-goog-api-key":o}:{},...this.credentialProvider?await this.credentialProvider({profile:"google-gemini",operation:"speak",method:"POST",url:i}):{}},body:{contents:[{role:"user",parts:[{text:e.text}]}],generationConfig:{responseModalities:["AUDIO"],speechConfig:{voiceConfig:{prebuiltVoiceConfig:{voiceName:s}}}}},format:e.format??"wav",transcript:e.text,fetch:t?.fetch,abortSignal:t?.abortSignal})}getVertexCacheContext(){if(!this.vertexConfig)return;let{projectId:e,region:t}=this.vertexConfig,o=`https://${wi(t)}/v1`,i=`projects/${e}/locations/${t}`;return{baseUrl:o,parent:i,modelResource:s=>`${i}/publishers/google/models/${s}`}}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}}hasProviderDeclaredTools(){return!!(this.options?.codeExecution||this.options?.googleSearchRetrieval||this.options?.googleSearch||this.options?.googleMaps||this.options?.urlContext)}buildToolState(e,t){let n=[];if(e.functions&&e.functions.length>0){let l=e.functions.map(u=>{let d={type:"object",properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]},p=u.parameters?Pc(u.parameters):void 0;return p===void 0||p&&typeof p=="object"&&Object.keys(p).length===0?p={...d}:p&&typeof p=="object"&&p.type==="object"&&(!("properties"in p)||!p.properties||Object.keys(p.properties).length===0)&&(p={...p,properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]}),{name:u.name,description:u.description,parameters:p}});n.push({function_declarations:l})}if(this.options?.codeExecution&&n.push({code_execution:{}}),this.options?.googleSearchRetrieval&&n.push({google_search_retrieval:{dynamic_retrieval_config:this.options.googleSearchRetrieval}}),this.options?.googleSearch&&n.push({google_search:{}}),this.options?.googleMaps){let l=this.options.googleMaps,u=l?.enableWidget!==void 0?{enableWidget:l.enableWidget}:{};n.push({google_maps:u})}this.options?.urlContext&&n.push({url_context:{}}),n.length===0&&(n=void 0);let o,i=Array.isArray(n)?n.some(l=>l&&Array.isArray(l.function_declarations)&&l.function_declarations.length>0):!1;if(e.functionCall)if(e.functionCall==="none")o={function_calling_config:{mode:"NONE"}};else if(e.functionCall==="auto")o={function_calling_config:{mode:"AUTO"}};else if(e.functionCall==="required")o={function_calling_config:{mode:"ANY"}};else{let l=e.functionCall.function?.name?{allowedFunctionNames:[e.functionCall.function.name]}:{};o={function_calling_config:{mode:"ANY"},...l}}else i&&(o={function_calling_config:{mode:"AUTO"}});this.options?.retrievalConfig&&(o={...o??{},retrievalConfig:{...this.options.retrievalConfig.latLng?{latLng:this.options.retrievalConfig.latLng}:{}}});let s=e.functions?.some(l=>l.cache)??!1,a=!!(n&&n.length>0)||!!o,c=!!t?.contextCache&&a;return{tools:n,toolConfig:o,cacheableTools:s||c}}createChatReq=async(e,t)=>{let n=e.model,o=e.modelConfig?.stream??this.config.stream,i=uc(this.config.audio,e.modelConfig?.audio),s=dc(n,this.config.audio,e.modelConfig?.audio),a=this.resolveServiceTier(n,t,s);if(!e.chatPrompt||e.chatPrompt.length===0)throw new Error("Chat prompt is empty");let c;if(s){if(this.isVertex)throw new Error("Gemini Live audio currently supports Google AI API-key WebSocket sessions only");c={name:"gemini-live-audio"}}else this.endpointId?c={name:o?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:c={name:o?`/models/${n}:streamGenerateContent?alt=sse`:`/models/${n}:generateContent`};!s&&this.isVertex&&(c.url=this.getVertexApiURL(n,t?.beta));let l=e.chatPrompt.filter(w=>w.role==="system").map(w=>w.content),u=l.length>0?{role:"user",parts:[{text:l.join(" ")}]}:void 0,d=[],p=e.chatPrompt.filter(w=>w.role!=="system");for(let w=0;w<p.length;w++){let N=p[w];switch(N.role){case"user":{let E=Array.isArray(N.content)?N.content.map((I,L)=>{switch(I.type){case"text":return{text:I.text};case"image":return{inlineData:{mimeType:I.mimeType,data:I.image}};case"audio":return{inlineData:{mimeType:I.mimeType??Gt(I.format,I.sampleRate),data:I.data}};case"file":return"fileUri"in I?{fileData:{mimeType:I.mimeType,fileUri:I.fileUri}}:{inlineData:{mimeType:I.mimeType,data:I.data}};default:throw new Error(`Chat prompt content type not supported (index: ${L})`)}}):[{text:N.content}];d.push({role:"user",parts:E});break}case"assistant":{let E=[],I=N.thoughtBlocks,L=N.functionCalls&&N.functionCalls.length>0,$=I?.[0],U=I?.map(F=>F.data).join("")??"",S=$?.signature;if(U&&E.push({...L?{}:{thought:!0},text:U,...S&&!L?{thought_signature:S}:{}}),N.functionCalls){let F=N.functionCalls.map((H,O)=>{let V;if(typeof H.function.params=="string"){let Y=H.function.params;if(Y.trim().length===0)V={};else try{V=JSON.parse(Y)}catch{throw new Error(`Failed to parse function params JSON: ${Y}`)}}else V=H.function.params;let W={functionCall:{name:H.function.name,args:V}};return S&&O===0&&(W.thought_signature=S),W});E.push(...F)}let T=N.audio?.transcript;if((N.content||T)&&E.push({text:N.content??T??""}),E.length===0)throw new Error("Assistant content is empty");d.push({role:"model",parts:E});break}case"function":{let E=[],I=N,L=w;for(;;){if(!("functionId"in I))throw new Error(`Chat prompt functionId is empty (index: ${L})`);if(E.push(...gp(p,L,I)),L+1<p.length&&p[L+1].role==="function")L++,I=p[L];else break}w=L,d.push({role:"user",parts:E});break}default:throw new Error(`Invalid role: ${JSON.stringify(N)} (index: ${w})`)}}let{tools:m,toolConfig:g}=this.buildToolState(e,t),h=this.getEffectiveMappings(n),x=mp({model:n,direct:this.config.thinking,logicalLevel:t?.thinkingTokenBudget,showThoughts:t?.showThoughts,...h}),f=e.modelConfig?.maxTokens??this.config.maxTokens;if(x.thinkingLevel&&f!==void 0)throw new Error("Cannot set maxTokens when using thinkingLevel with Gemini models. When thinking is enabled, the model manages output tokens automatically. Remove the maxTokens setting or disable thinking.");let y=$b(n),A={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,...y?{}:{temperature:e.modelConfig?.temperature??this.config.temperature},...!y&&e.modelConfig?.topP!==void 0?{topP:e.modelConfig.topP}:{},...y?{}:{topK:e.modelConfig?.topK??this.config.topK},frequencyPenalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,candidateCount:e.modelConfig?.n??this.config.n??1,stopSequences:e.modelConfig?.stopSequences??this.config.stopSequences,responseMimeType:"text/plain",...Object.keys(x).length>0?{thinkingConfig:x}:{}};if(!y&&Tc(n)&&(A.temperature===void 0||A.temperature<1)&&(A.temperature=1),s&&(e.responseFormat||this.config.responseFormat))throw new Error("Gemini Live audio models do not support structured response formats with audio output");if(e.responseFormat)if(e.responseFormat.type==="json_schema"&&e.responseFormat.schema){let w=e.responseFormat.schema.schema||e.responseFormat.schema;A.responseMimeType="application/json",A.responseJsonSchema=Pc(w)}else A.responseMimeType="application/json";else this.config.responseFormat&&this.config.responseFormat==="json_object"&&(A.responseMimeType="application/json");let b=this.config.safetySettings,v={contents:d,tools:m,toolConfig:g,systemInstruction:u,generationConfig:A,safetySettings:b,...a?{service_tier:a}:{}};if(s){let w=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;if(!w)throw new Error("GoogleGemini AI API key not set");c=Hd({model:n,request:v,apiKey:w,audio:i})}return[c,v]};createEmbedReq=async(e,t)=>{let n=e.embedModel;if(!n)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let o,i;return this.isVertex?(this.endpointId?o={name:`/${this.endpointId}:predict`}:o={name:`/models/${n}:predict`},o.url=this.getVertexApiURL(n,t?.beta),i={instances:e.texts.map(s=>({content:s,...this.config.embedType&&{taskType:this.config.embedType}})),parameters:{autoTruncate:this.config.autoTruncate,outputDimensionality:this.config.dimensions}}):(o={name:`/models/${n}:batchEmbedContents`},i={requests:e.texts.map(s=>({model:`models/${n}`,content:{parts:[{text:s}]},outputDimensionality:this.config.dimensions,...this.config.embedType&&{taskType:this.config.embedType}}))}),[o,i]};createChatResp=e=>{let t,n=e.candidates?.map(i=>{let s={index:0};switch(i.finishReason){case"MAX_TOKENS":s.finishReason="length";break;case"STOP":s.finishReason="stop";break;case"SAFETY":throw new He("Content was blocked due to safety settings",void 0,void 0);case"RECITATION":throw new He("Content was blocked due to recitation policy",void 0,void 0);case"MALFORMED_FUNCTION_CALL":throw new He("Function call was malformed and blocked",void 0,void 0);case"UNEXPECTED_TOOL_CALL":throw new He("Unexpected tool call",void 0,void 0);case"FINISH_REASON_UNSPECIFIED":throw new He("Finish reason unspecified",void 0,void 0);case"BLOCKLIST":throw new He("Content was blocked due to blocklist",void 0,void 0);case"PROHIBITED_CONTENT":throw new He("Content was blocked due to prohibited content",void 0,void 0);case"SPII":throw new He("Content was blocked due to SPII",void 0,void 0);case"OTHER":throw new He("Other finish reason",void 0,void 0)}if(!i.content||!i.content.parts)return s;for(let l of i.content.parts){if("text"in l){if("thought"in l&&l.thought||l.thought===!0){s.thought=l.text;let d=l.thoughtSignature||l.thought_signature;s.thoughtBlocks||(s.thoughtBlocks=[]),s.thoughtBlocks.push({data:l.text,encrypted:!1,...d?{signature:d}:{}})}else s.content=l.text;continue}if("functionCall"in l){let d=l.thoughtSignature||l.thought_signature;if(d)if(!s.thoughtBlocks||s.thoughtBlocks.length===0)s.thoughtBlocks=[{data:"",encrypted:!1,signature:d}];else{let p=s.thoughtBlocks[s.thoughtBlocks.length-1];p&&!p.signature&&(p.signature=d)}s.functionCalls=[...s.functionCalls??[],{id:qe(),type:"function",function:{name:l.functionCall.name,params:l.functionCall.args}}]}let u=zd(l);u&&(s.audio=u)}let a=i.citationMetadata?.citations;if(Array.isArray(a)&&a.length){let l=u=>u?`${u.year}-${String(u.month).padStart(2,"0")}-${String(u.day).padStart(2,"0")}`:void 0;s.citations=a.filter(u=>typeof u?.uri=="string").map(u=>({url:u.uri,title:u.title,license:u.license,publicationDate:l(u.publicationDate)}))}let c=i.groundingMetadata;if(c){if(Array.isArray(c.groundingChunks)){let l=c.groundingChunks.map(d=>d?.maps).filter(d=>d&&typeof d.uri=="string").map(d=>({url:d.uri,title:d.title}));l.length&&(s.citations=[...s.citations??[],...l]);let u=c.groundingChunks.map(d=>d?.retrievedContext).filter(d=>d&&(typeof d.uri=="string"||typeof d.media_id=="string")).map(d=>({url:d.uri??"",title:d.title,...typeof d.media_id=="string"?{mediaId:d.media_id}:{},...Array.isArray(d.page_numbers)?{pageNumbers:d.page_numbers}:{}}));u.length&&(s.citations=[...s.citations??[],...u])}typeof c.googleMapsWidgetContextToken=="string"&&(t=c.googleMapsWidgetContextToken)}return s});if(e.usageMetadata){let i=e.usageMetadata.cachedContentTokenCount??0,s=e.usageMetadata.serviceTier==="unspecified"?"standard":e.usageMetadata.serviceTier;this.tokensUsed={totalTokens:e.usageMetadata.totalTokenCount,promptTokens:e.usageMetadata.promptTokenCount-i,completionTokens:e.usageMetadata.candidatesTokenCount,thoughtsTokens:e.usageMetadata.thoughtsTokenCount,...i>0?{cacheReadTokens:i}:{},...s?{serviceTier:s}:{}}}let o={results:n,...e.responseId?{remoteId:e.responseId}:{},...e.modelVersion?{providerMetadata:{google:{modelVersion:e.modelVersion}}}:{}};return t&&(o.providerMetadata={...o.providerMetadata,google:{...o.providerMetadata?.google??{},mapsWidgetContextToken:t}}),o};createChatStreamResp=e=>this.createChatResp(e);createEmbedResp=e=>{let t;return this.isVertex?t=e.predictions.map(n=>n.embeddings.values):t=e.embeddings.map(n=>n.values),{embeddings:t}};supportsContextCache=e=>{let t=e;return ib.some(n=>t.includes(n)||n.includes(t))};buildCacheCreateOp=(e,t)=>{let n=e.model,o=t.contextCache?.ttlSeconds??3600,{tools:i,toolConfig:s,cacheableTools:a}=this.buildToolState(e,t),{systemInstruction:c,contents:l}=this.extractCacheableContent(e.chatPrompt);if(!c&&(!l||l.length===0)&&!a)return;let u=this.getVertexCacheContext(),d={model:u?u.modelResource(n):`models/${n}`,ttl:`${o}s`,displayName:`ax-cache-${Date.now()}`};c&&(d.systemInstruction=c),l&&l.length>0&&(d.contents=l),a&&(i&&i.length>0&&(d.tools=i),s&&(d.toolConfig=s));let p;return u?p=`/${u.parent}/cachedContents`:p="/cachedContents",{type:"create",apiConfig:{name:p,...u?{url:u.baseUrl}:{}},request:d,parseResponse:m=>{let g=m;if(g?.name)return{name:g.name,expiresAt:g.expireTime,tokenCount:g.usageMetadata?.totalTokenCount}}}};getContextCacheToolState=(e,t)=>{let{tools:n,toolConfig:o,cacheableTools:i}=this.buildToolState(e,t);if(!i)return;let s=e.functions?.map(({cache:c,...l})=>l);if(!!(s&&s.length>0)||!!e.functionCall)return{functions:s,functionCall:e.functionCall};if(n||o)return{functions:[{name:"__gemini_tool_state__",description:JSON.stringify({tools:n,toolConfig:o})}]}};buildCacheUpdateTTLOp=(e,t)=>{let n={ttl:`${t}s`},o=new URLSearchParams({updateMask:"ttl"}),i=`/${e}?${o.toString()}`,s=this.getVertexCacheContext();return{type:"update",apiConfig:{name:i,method:"PATCH",headers:{"Content-Type":"application/json"},...s?{url:s.baseUrl}:{}},request:n,parseResponse:a=>{let c=a;if(c?.name)return{name:c.name,expiresAt:c.expireTime,tokenCount:c.usageMetadata?.totalTokenCount}}}};buildCacheDeleteOp=e=>{let t=`/${e}`,n=this.getVertexCacheContext();return{type:"delete",apiConfig:{name:t,headers:{"Content-Type":"application/json"},...n?{url:n.baseUrl}:{}},request:{},parseResponse:()=>{}}};prepareCachedChatReq=async(e,t,n)=>{let o=e.model,i=this.resolveServiceTier(o,t),s=e.modelConfig?.stream??this.config.stream,{tools:a,toolConfig:c,cacheableTools:l}=this.buildToolState(e,t),{dynamicContents:u,dynamicSystemInstruction:d}=this.extractDynamicContent(e.chatPrompt),p;this.endpointId?p={name:s?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:p={name:s?`/models/${o}:streamGenerateContent?alt=sse`:`/models/${o}:generateContent`};let m=$b(o),g=this.getEffectiveMappings(o),h=mp({model:o,direct:this.config.thinking,logicalLevel:t.thinkingTokenBudget,showThoughts:t.showThoughts,...g}),x={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,...m?{}:{temperature:e.modelConfig?.temperature??this.config.temperature},...!m&&e.modelConfig?.topP!==void 0?{topP:e.modelConfig.topP}:{},...m?{}:{topK:e.modelConfig?.topK??this.config.topK},frequencyPenalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,candidateCount:e.modelConfig?.n??this.config.n??1,stopSequences:e.modelConfig?.stopSequences??this.config.stopSequences,responseMimeType:"text/plain",...Object.keys(h).length>0?{thinkingConfig:h}:{}};!m&&Tc(o)&&(x.temperature===void 0||x.temperature<1)&&(x.temperature=1);let f=this.config.safetySettings,y={contents:u,cachedContent:n,generationConfig:x,safetySettings:f,...i?{service_tier:i}:{}};return l||(a&&a.length>0&&(y.tools=a),c&&(y.toolConfig=c)),d&&(y.systemInstruction=d),{apiConfig:p,request:y}};extractCacheableContent(e){let t,n=[],o=-1;for(let i=e.length-1;i>=0;i--){let s=e[i];if("cache"in s&&s.cache){o=i;break}}for(let i=0;i<e.length;i++){let s=e[i];if(s.role==="system"){t={role:"user",parts:[{text:s.content}]};continue}if(o>=0&&i<=o)if(s.role==="user"){let a=[];if(typeof s.content=="string")a.push({text:s.content});else if(Array.isArray(s.content))for(let c of s.content)switch(c.type){case"text":a.push({text:c.text});break;case"image":a.push({inlineData:{mimeType:c.mimeType,data:c.image}});break;case"audio":a.push({inlineData:{mimeType:c.mimeType??Gt(c.format,c.sampleRate),data:c.data}});break;case"file":"fileUri"in c?a.push({fileData:{mimeType:c.mimeType,fileUri:c.fileUri}}):a.push({inlineData:{mimeType:c.mimeType,data:c.data}});break}a.length>0&&n.push({role:"user",parts:a})}else if(s.role==="assistant"){let a=[],c=s.thoughtBlocks,l=s.functionCalls&&s.functionCalls.length>0,u=c?.[0]?.signature,d=c?.map(p=>p.data).join("")??"";if(d&&a.push({...l?{}:{thought:!0},text:d,...u&&!l?{thought_signature:u}:{}}),s.functionCalls)for(let[p,m]of s.functionCalls.entries()){let g;if(typeof m.function.params=="string")try{g=JSON.parse(m.function.params)}catch{g={}}else g=m.function.params??{};let h={functionCall:{name:m.function.name,args:g}};u&&p===0&&(h.thought_signature=u),a.push(h)}s.content&&a.push({text:s.content}),a.length>0&&n.push({role:"model",parts:a})}else s.role==="function"&&n.push({role:"user",parts:gp(e,i,s)})}return{systemInstruction:t,contents:n}}extractDynamicContent(e){let n=[],o=-1;for(let i=e.length-1;i>=0;i--){let s=e[i];if("cache"in s&&s.cache){o=i;break}}for(let i=0;i<e.length;i++){let s=e[i];if(s.role!=="system"&&!(o>=0&&i<=o))if(s.role==="user"){let a=[];if(typeof s.content=="string")a.push({text:s.content});else if(Array.isArray(s.content)){for(let c of s.content)if(!("cache"in c&&c.cache))switch(c.type){case"text":a.push({text:c.text});break;case"image":a.push({inlineData:{mimeType:c.mimeType,data:c.image}});break;case"audio":a.push({inlineData:{mimeType:c.mimeType??Gt(c.format,c.sampleRate),data:c.data}});break;case"file":"fileUri"in c?a.push({fileData:{mimeType:c.mimeType,fileUri:c.fileUri}}):a.push({inlineData:{mimeType:c.mimeType,data:c.data}});break}}a.length>0&&n.push({role:"user",parts:a})}else if(s.role==="assistant"){let a=[],c=s.thoughtBlocks,l=s.functionCalls&&s.functionCalls.length>0,u=c?.[0]?.signature,d=c?.map(p=>p.data).join("")??"";if(d&&a.push({...l?{}:{thought:!0},text:d,...u&&!l?{thought_signature:u}:{}}),s.functionCalls)for(let[p,m]of s.functionCalls.entries()){let g;if(typeof m.function.params=="string")try{g=JSON.parse(m.function.params)}catch{g={}}else g=m.function.params??{};let h={functionCall:{name:m.function.name,args:g}};u&&p===0&&(h.thought_signature=u),a.push(h)}s.content&&a.push({text:s.content}),a.length>0&&n.push({role:"model",parts:a})}else s.role==="function"&&n.push({role:"user",parts:gp(e,i,s)})}return{dynamicContents:n,dynamicSystemInstruction:void 0}}},zi=class r extends It{static create(e){return new r(e)}constructor({apiKey:e,credentialProvider:t,projectId:n,region:o,endpointId:i,config:s,options:a,models:c,modelInfo:l}){let u=n!==void 0&&o!==void 0?{projectId:n,region:o}:void 0,d={...Gb(),...s};if(u&&d.serviceTier&&d.serviceTier!=="auto")throw new Error("Gemini inference service tiers are not supported by Vertex AI");let p,m,g;if(u){let{projectId:y,region:A}=u;if(!e&&!t)throw new Error("GoogleGemini Vertex API key or credential provider not set");if(e&&typeof e!="function")throw new Error("GoogleGemini Vertex API key must be a function for token-based authentication");let b;i?b="endpoints":b="publishers/google";let v=wi(A);g=(w,N)=>`https://${v}/${lI(w,N)}/projects/${y}/locations/${A}/${b}`,p=g(d.model),m=async()=>({...e?{Authorization:`Bearer ${typeof e=="function"?await e():e}`}:{}})}else{if(!e&&!t)throw new Error("GoogleGemini AI API key or credential provider not set");p="https://generativelanguage.googleapis.com/v1beta",m=async()=>({...e?{"x-goog-api-key":typeof e=="function"?await e():e}:{}})}let h=new fp(d,u,i,e,t,a,g);l=[...Ii,...l??[]];let x=y=>{let A=dc(y,{output:{enabled:!0}}),b=jt({model:y,modelInfo:l,models:c}),v=b?.supported?.structuredOutputs??!0,w=b?.supported?.structuredOutputModes??(v?["native","function"]:["function"]);return{functions:!0,streaming:!0,hasThinkingBudget:b?.supported?.thinkingBudget??!1,hasShowThoughts:b?.supported?.showThoughts??!1,structuredOutputs:w.includes("native"),structuredOutputModes:w,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:A?["pcm16","pcm"]:["wav","mp3","aac","ogg"],maxDuration:9.5*60,output:{supported:A,formats:["pcm16"],sampleRate:24e3,voices:["Kore"]}},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:h.supportsContextCache(y),types:["persistent"]},thinking:b?.supported?.thinkingBudget??!1,multiTurn:!0,serviceTiers:b?.supported?.serviceTiers??(u||A?[]:["standard","flex","priority"])}},f=c?.map(y=>{let A=y,b=A?.config;if(!b)return y;let v={};b.maxTokens!==void 0&&(v.maxTokens=b.maxTokens),b.temperature!==void 0&&(v.temperature=b.temperature),b.topP!==void 0&&(v.topP=b.topP),b.topK!==void 0&&(v.topK=b.topK),b.presencePenalty!==void 0&&(v.presencePenalty=b.presencePenalty),b.frequencyPenalty!==void 0&&(v.frequencyPenalty=b.frequencyPenalty),b.stopSequences!==void 0&&(v.stopSequences=b.stopSequences),b.endSequences!==void 0&&(v.endSequences=b.endSequences),b.stream!==void 0&&(v.stream=b.stream),b.n!==void 0&&(v.n=b.n);let w={...A};Object.keys(v).length>0&&(w.modelConfig={...A.modelConfig??{},...v});let N=b.thinking?.thinkingTokenBudget;if(typeof N=="number"){let E=String(A.model??d.model);if(Tc(E))throw new Error(`Gemini 3 models (${E}) do not support numeric thinkingTokenBudget. Use a logical thinkingTokenBudget level instead.`);let I=d.thinkingTokenBudgetLevels,L=[["minimal",I?.minimal??200],["low",I?.low??800],["medium",I?.medium??5e3],["high",I?.high??1e4],["highest",I?.highest??24500]],$="minimal",U=Number.POSITIVE_INFINITY;for(let[S,T]of L){let F=Math.abs(N-T);F<U&&(U=F,$=S)}w.thinkingTokenBudget=$}return b.thinking?.includeThoughts!==void 0&&(w.showThoughts=!!b.thinking.includeThoughts),b.serviceTier!==void 0&&(w.serviceTier=b.serviceTier),b.thinkingLevelMapping&&(w.thinkingLevelMapping=b.thinkingLevelMapping),b.thinkingTokenBudgetLevels&&(w.thinkingTokenBudgetLevels=b.thinkingTokenBudgetLevels),w});f?h.setModels(f):c&&h.setModels(c),h.setServiceTierSupportFor(y=>x(y).serviceTiers??[]),super(h,{name:"GoogleGeminiAI",apiURL:p,headers:m,modelInfo:l,defaults:{model:d.model,embedModel:d.embedModel},options:a,credentialProvider:t,profile:"google-gemini",supportFor:x,models:f??c})}};var hp=class{constructor(e={}){this.config=e;this.config.id=this.config.id??qe()}metrics={latency:{chat:{mean:0,p95:0,p99:0,samples:[]},embed:{mean:0,p95:0,p99:0,samples:[]}},errors:{chat:{count:0,rate:0,total:0},embed:{count:0,rate:0,total:0}}};getLastUsedChatModel(){return this.config.modelInfo?.name??"mock-model"}getLastUsedEmbedModel(){return this.config.embedModelInfo?.name??"mock-embed-model"}getLastUsedModelConfig(){return this.config.modelInfo?{maxTokens:this.config.modelInfo.maxTokens,temperature:.7,stream:this.config.features?.streaming??!1}:void 0}getName(){return this.config.name??"mock-ai-service"}getId(){return this.config.id??"mock-ai-service-id"}getFeatures(e){let t=this.config.features?.media;return{functions:this.config.features?.functions??!1,streaming:this.config.features?.streaming??!1,structuredOutputs:this.config.features?.structuredOutputs,...this.config.features?.structuredOutputModes?{structuredOutputModes:this.config.features.structuredOutputModes}:{},...this.config.features?.serviceTiers?{serviceTiers:this.config.features.serviceTiers}:{},media:{images:{supported:t?.images?.supported??!1,formats:t?.images?.formats??[]},audio:{supported:t?.audio?.supported??!1,formats:t?.audio?.formats??[]},files:{supported:t?.files?.supported??!1,formats:t?.files?.formats??[],uploadMethod:t?.files?.uploadMethod??"none"},urls:{supported:t?.urls?.supported??!1,webSearch:t?.urls?.webSearch??!1,contextFetching:t?.urls?.contextFetching??!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}getModelList(){return this.config.models}getMetrics(){return this.metrics}getEstimatedCost(){return 0}async chat(e,t){if(this.config.latencyMs&&await new Promise(n=>setTimeout(n,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock chat error");return this.updateMetrics("chat"),typeof this.config.chatResponse=="function"?await this.config.chatResponse(e,t):this.config.chatResponse??{results:[{index:0,content:"Mock response",finishReason:"stop"}],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:10,completionTokens:5,totalTokens:15}}}}async embed(e,t){if(this.config.latencyMs&&await new Promise(n=>setTimeout(n,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock embed error");return this.updateMetrics("embed"),typeof this.config.embedResponse=="function"?this.config.embedResponse(e):this.config.embedResponse??{embeddings:[[.1,.2,.3]],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:5,completionTokens:0,totalTokens:5}}}}async transcribe(e,t){if(this.config.shouldError)throw new Error(this.config.errorMessage??"Mock transcribe error");return typeof this.config.transcribeResponse=="function"?this.config.transcribeResponse(e):this.config.transcribeResponse??{text:"Mock transcription"}}async speak(e,t){if(this.config.shouldError)throw new Error(this.config.errorMessage??"Mock speech error");return typeof this.config.speechResponse=="function"?this.config.speechResponse(e):this.config.speechResponse??{data:"bW9jay1hdWRpbw==",format:"mp3",mimeType:"audio/mpeg",transcript:e.text}}setOptions(e){this.config.options=e}getOptions(){return this.config.options??{}}getLogger(){return this.config.options?.logger??(e=>{console.log(e)})}updateMetrics(e){let t=this.config.latencyMs??0;this.metrics.latency[e].samples.push(t);let n=this.metrics.latency[e].samples;if(this.metrics.latency[e].mean=n.reduce((o,i)=>o+i,0)/n.length,n.length>0){let o=[...n].sort((a,c)=>a-c),i=Math.max(0,Math.floor(o.length*.95)-1);this.metrics.latency[e].p95=o[i]??t;let s=Math.max(0,Math.floor(o.length*.99)-1);this.metrics.latency[e].p99=o[s]??t}if(this.config.shouldError){this.metrics.errors[e].count++,this.metrics.errors[e].total++;let o=this.metrics.latency[e].samples.length;this.metrics.errors[e].rate=o>0?this.metrics.errors[e].count/o:0}}};var xp=class r{options;lastUsedService;services=new Map;constructor(e){if(e.length===0)throw new Error("No AI services provided.");for(let[t,n]of e.entries())if("key"in n){if(this.services.has(n.key))throw new Error(`Duplicate model key: ${n.key}`);let{service:i,description:s,isInternal:a}=n;this.services.set(n.key,{service:i,description:s,isInternal:a})}else{let i=n.getModelList();if(!i)throw new Error(`Service ${t} \`${n.getName()}\` has no model list.`);for(let s of i){if(this.services.has(s.key)){let a=this.services.get(s.key)?.service;throw new Error(`Service ${t} \`${n.getName()}\` has duplicate model key: ${s.key} as service ${a?.getName()}`)}if("model"in s&&typeof s.model)this.services.set(s.key,{description:s.description,service:n,model:s.model});else if("embedModel"in s&&s.embedModel)this.services.set(s.key,{description:s.description,service:n,embedModel:s.embedModel});else throw new Error(`Key ${s.key} in model list for service ${t} \`${n.getName()}\` is missing a model or embedModel property.`)}}}static create(e){return new r(e)}getLastUsedChatModel(){return this.lastUsedService?.getLastUsedChatModel()}getLastUsedEmbedModel(){return this.lastUsedService?.getLastUsedEmbedModel()}getLastUsedModelConfig(){return this.lastUsedService?.getLastUsedModelConfig()}async chat(e,t){let n=e.model;if(!n)throw new Error("Model key must be specified for multi-service");let o=this.services.get(n);if(!o)throw new Error(`No service found for model key: ${n}`);if(this.lastUsedService=o.service,!o.model){let{model:i,...s}=e;return await o.service.chat(s,t)}return await o.service.chat({model:n,...e},t)}async embed(e,t){let n=e.embedModel;if(!n)throw new Error("Embed model key must be specified for multi-service");let o=this.services.get(n);if(!o)throw new Error(`No service found for embed model key: ${n}`);if(this.lastUsedService=o.service,!o.model){let{embedModel:i,...s}=e;return await o.service.embed(s,t)}return await o.service.embed({embedModel:n,...e},t)}async transcribe(e,t){let n=e.model;if(!n){let i=Array.from(this.services.values())[0]?.service;if(!i)throw new Error("No AI services provided.");return this.lastUsedService=i,await i.transcribe(e,t)}let o=this.services.get(n);if(!o)throw new Error(`No service found for transcription model key: ${n}`);return this.lastUsedService=o.service,await o.service.transcribe(e,t)}async speak(e,t){let n=e.model;if(!n){let i=Array.from(this.services.values())[0]?.service;if(!i)throw new Error("No AI services provided.");return this.lastUsedService=i,await i.speak(e,t)}let o=this.services.get(n);if(!o)throw new Error(`No service found for speech model key: ${n}`);return this.lastUsedService=o.service,await o.service.speak(e,t)}getId(){return`MultiServiceRouter:${Array.from(this.services.values()).map(e=>e.service.getId()).join(",")}`}getName(){return"MultiServiceRouter"}getModelList(){return Array.from(this.services).filter(([,e])=>!e.isInternal).map(([e,t])=>{if(t.model)return{key:e,description:t.description,model:t.model};if(t.embedModel)return{key:e,description:t.description,embedModel:t.embedModel};throw new Error(`Service ${e} has no model or embedModel`)})}getFeatures(e){if(e){let t=this.services.get(e);if(t)return t.service.getFeatures(e)}return{functions:!1,streaming:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}getMetrics(){let e=this.lastUsedService;if(!e){let t=this.services.values().next().value;t&&(e="service"in t?t.service:t)}if(!e)throw new Error("No service available to get metrics.");return e.getMetrics()}getEstimatedCost(e){return this.lastUsedService?.getEstimatedCost(e)??0}setOptions(e){for(let t of this.services.values())t.service.setOptions(e);this.options=e}getOptions(){return this.options??{}}getLogger(){let e=this.lastUsedService;if(!e){let t=this.services.values().next().value;t&&(e=t.service)}if(!e)throw new Error("No service available to get logger.");return e.getLogger()}setServiceEntry(e,t){this.services.set(e,t)}};async function yp(r,e,t={}){if(typeof r=="string")return[{type:"text",text:r}];if(!Array.isArray(r))return[{type:"text",text:String(r)}];let n=e.getFeatures(),o=[];for(let i of r)try{switch(i.type){case"text":o.push({type:"text",text:i.text});break;case"image":if(n.media.images.supported)o.push(i);else if(i.altText)o.push({type:"text",text:i.altText});else if(t.imageToText)try{let s=await t.imageToText(i.image);o.push({type:"text",text:s})}catch(s){throw new on(s,"image","vision analysis")}else switch(t.fallbackBehavior){case"error":throw new xt("Images",e.getName(),!1);case"skip":continue;default:o.push({type:"text",text:"[Image content not supported by this provider]"})}break;case"audio":if(n.media.audio.supported)i.transcription?o.push({type:"text",text:i.transcription}):o.push({type:"text",text:"[Audio content]"});else if(i.transcription)o.push({type:"text",text:i.transcription});else if(t.audioToText)try{let s=await t.audioToText(i.data,i.format);o.push({type:"text",text:s})}catch(s){throw new on(s,"audio","transcription")}else switch(t.fallbackBehavior){case"error":throw new xt("Audio",e.getName(),!1);case"skip":continue;case"degrade":o.push({type:"text",text:"[Audio content not supported by this provider]"})}break;case"file":if(n.media.files.supported)i.extractedText?o.push({type:"text",text:i.extractedText}):o.push({type:"text",text:`[File: ${i.filename}]`});else if(i.extractedText)o.push({type:"text",text:i.extractedText});else if(t.fileToText)try{let s=await t.fileToText(i.data,i.mimeType);o.push({type:"text",text:s})}catch(s){throw new on(s,"file","text extraction")}else switch(t.fallbackBehavior){case"error":throw new xt("Files",e.getName(),!1);case"skip":continue;default:o.push({type:"text",text:`[File: ${i.filename} - content not accessible by this provider]`})}break;case"url":if(n.media.urls.supported)i.cachedContent?o.push({type:"text",text:i.cachedContent}):o.push({type:"text",text:`[Link: ${i.url}${i.title?` - ${i.title}`:""}]`});else if(i.cachedContent)o.push({type:"text",text:i.cachedContent});else if(t.urlToText)try{let s=await t.urlToText(i.url);o.push({type:"text",text:s})}catch(s){throw new on(s,"url","content fetching")}else switch(t.fallbackBehavior){case"error":throw new xt("URLs",e.getName(),!1);case"skip":continue;case"degrade":o.push({type:"text",text:`[Link: ${i.url}${i.title?` - ${i.title}`:""}]`})}break;default:typeof i=="object"&&i.text?o.push({type:"text",text:i.text}):o.push({type:"text",text:String(i)})}}catch(s){throw s instanceof xt||s instanceof on?s:new on(s,i.type||"unknown","content processing")}return o}function pI(r){let e=!1,t=!1,n=!1,o=!1;for(let i of r)if(i.role==="user"&&Array.isArray(i.content))for(let s of i.content)switch(s.type){case"image":e=!0;break;case"audio":t=!0;break;case"file":n=!0;break;case"url":o=!0;break}return{hasImages:e,hasAudio:t,hasFiles:n,hasUrls:o}}var Ap=class{providers;processingServices;config;constructor(e){this.providers=[e.providers.primary,...e.providers.alternatives],this.processingServices=e.processing,this.config=e.routing}async chat(e,t={}){let n=await this.selectProviderWithDegradation(e,t.routingOptions||{},t.serviceTier),o=await this.preprocessRequest(e,n.provider,t.processingOptions);try{return{response:await n.provider.chat(o,t),routing:n}}catch(i){if(i instanceof xt&&t.fallbackProviders?.length)return await this.tryFallbackProviders(e,t.fallbackProviders,t);throw i}}async preprocessRequest(e,t,n){let o={...n,fallbackBehavior:n?.fallbackBehavior||"degrade",imageToText:n?.imageToText||this.processingServices.imageToText,audioToText:n?.audioToText||this.processingServices.audioToText,fileToText:n?.fileToText||this.processingServices.fileToText,urlToText:n?.urlToText||this.processingServices.urlToText},i=[];for(let s of e.chatPrompt)if(s.role==="user"&&Array.isArray(s.content)){let a=await yp(s.content,t,o);a.every(l=>l.type==="text")&&a.length===1?i.push({...s,content:a[0].text}):i.push({...s,content:a.map(l=>l.type==="image"?l:{type:"text",text:l.text})})}else i.push(s);return{...e,chatPrompt:i}}async selectProviderWithDegradation(e,t,n){let o=mo(e),i=[],s=[],a=[];try{let c=n&&n!=="auto"?this.providers.filter(d=>d.getFeatures(e.model).serviceTiers?.includes(n)):this.providers;if(c.length===0)throw new Error(`No provider supports service tier ${n}`);let l=Ud(e,c,{requireExactMatch:t.requireExactMatch??this.config.capability.requireExactMatch,allowDegradation:t.allowDegradation??this.config.capability.allowDegradation}),u=l.getFeatures();return o.hasImages&&!u.media.images.supported&&(s.push("Images will be converted to text descriptions"),i.push("Image-to-text conversion")),o.hasAudio&&!u.media.audio.supported&&(s.push("Audio will be transcribed to text"),i.push("Audio-to-text transcription")),o.hasFiles&&!u.media.files.supported&&(s.push("File content will be extracted to text"),i.push("File-to-text extraction")),o.hasUrls&&!u.media.urls.supported&&(s.push("URL content will be pre-fetched"),i.push("URL content fetching")),o.requiresStreaming&&!u.streaming&&a.push("Streaming not supported - will use non-streaming mode"),o.requiresCaching&&!u.caching.supported&&a.push("Content caching not supported"),{provider:l,processingApplied:i,degradations:s,warnings:a}}catch(c){throw new Error(`Provider selection failed: ${c instanceof Error?c.message:"Unknown error"}`)}}async tryFallbackProviders(e,t,n){for(let o of t)if(!(n.serviceTier&&n.serviceTier!=="auto"&&!o.getFeatures(e.model).serviceTiers?.includes(n.serviceTier)))try{let i={provider:o,processingApplied:["Fallback provider selection"],degradations:["Using fallback provider due to primary provider failure"],warnings:[]},s=await this.preprocessRequest(e,o,{fallbackBehavior:"degrade"});return{response:await o.chat(s,n),routing:i}}catch{}throw new Error("All fallback providers failed")}async getRoutingRecommendation(e){return await this.selectProviderWithDegradation(e,{})}async validateRequest(e){let t=mo(e),n=[],o=[];try{let i=await this.selectProviderWithDegradation(e,{});return i.degradations.length>0&&(n.push(...i.degradations),o.push("Consider using a provider that natively supports all media types")),i.warnings.length>0&&n.push(...i.warnings),t.hasImages&&this.processingServices.imageToText===void 0&&(this.providers.some(a=>a.getFeatures().media.images.supported)||(n.push("No image processing service available and no providers support images"),o.push("Add imageToText processing service or use image-capable provider"))),t.hasAudio&&this.processingServices.audioToText===void 0&&(this.providers.some(a=>a.getFeatures().media.audio.supported)||(n.push("No audio processing service available and no providers support audio"),o.push("Add audioToText processing service or use audio-capable provider"))),{canHandle:n.length===0||i.degradations.length>0,issues:n,recommendations:o}}catch(i){return{canHandle:!1,issues:[`Cannot route request: ${i instanceof Error?i.message:"Unknown error"}`],recommendations:["Add more providers or processing services to handle this request"]}}}getRoutingStats(){let e={};for(let t of this.providers){let n=t.getFeatures(),o=t.getName();n.functions&&(e.Functions=e.Functions||[],e.Functions.push(o)),n.streaming&&(e.Streaming=e.Streaming||[],e.Streaming.push(o)),n.media.images.supported&&(e.Images=e.Images||[],e.Images.push(o)),n.media.audio.supported&&(e.Audio=e.Audio||[],e.Audio.push(o)),n.media.files.supported&&(e.Files=e.Files||[],e.Files.push(o)),n.media.urls.supported&&(e.URLs=e.URLs||[],e.URLs.push(o)),n.caching.supported&&(e.Caching=e.Caching||[],e.Caching.push(o))}return{totalProviders:this.providers.length,capabilityMatrix:e,recommendedProvider:this.providers[0]?.getName()||"None"}}};var Bb=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...Qe()}),mI=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...Rr()}),bp=class{constructor(e,t){this.config=e;this.engine=t}tokensUsed;engine;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,endSequences:e.endSequences,stream:e.stream,n:e.n}}createChatReq(e){let t=e.model,n=this.config.supportsFunctions??!1,o=e.chatPrompt.map(u=>{if(u.role==="function"){let m=u.content?.length?u.content.map(g=>g.type==="text"?g.text:g.type==="image"?g.altText??`[MCP image result: ${g.mimeType}]`:g.type==="audio"?g.transcription??`[MCP audio result: ${g.mimeType??"application/octet-stream"}]`:g.type==="url"?g.cachedContent??[g.title,g.description,g.url].filter(Boolean).join(`
1259
+ `)});return o},jb=[{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"}],Gb=()=>structuredClone({model:"gemini-2.5-flash",embedModel:"text-embedding-005",safetySettings:jb,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...Qe()}),dI=()=>structuredClone({model:"gemini-2.0-flash",embedModel:"text-embedding-005",safetySettings:jb,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...Rr()}),fp=class{constructor(e,t,n,o,i,s,a){this.config=e;this.vertexConfig=t;this.endpointId=n;this.apiKey=o;this.credentialProvider=i;this.options=s;this.vertexApiURLForModel=a;if(!this.isVertex&&this.config.autoTruncate)throw new Error("Auto truncate is not supported for GoogleGemini");mp({model:this.config.model,direct:this.config.thinking,thinkingLevelMapping:this.config.thinkingLevelMapping,thinkingTokenBudgetLevels:this.config.thinkingTokenBudgetLevels})}tokensUsed;models;serviceTierSupportFor;setModels(e){this.models=e}setServiceTierSupportFor(e){this.serviceTierSupportFor=e}resolveServiceTier(e,t,n=!1){let o=t.serviceTier??this.options?.serviceTier??this.config.serviceTier,i=Bt(o),s=n||this.isVertex;if(s&&i&&i!=="auto")throw new Error(`Gemini inference service tiers are not supported by ${n?"the Live API":"Vertex AI"}`);return pn({requested:o,supported:s?[]:this.serviceTierSupportFor?.(e)??["standard","flex","priority"],mapping:{auto:null,standard:"standard",flex:"flex",priority:"priority"},provider:"Google Gemini",model:e})}getEffectiveMappings(e){let t=this.models?.find(n=>n.model===e);return{thinkingLevelMapping:{...this.config.thinkingLevelMapping,...t?.thinkingLevelMapping??{}},thinkingTokenBudgetLevels:{...this.config.thinkingTokenBudgetLevels,...t?.thinkingTokenBudgetLevels??{}}}}getTokenUsage(){return this.tokensUsed}get isVertex(){return this.vertexConfig!==void 0}getVertexApiURL(e,t){return this.isVertex?this.vertexApiURLForModel?.(e,t):void 0}async transcribe(e,t){let n=e.model??"gemini-2.5-flash",o=typeof this.apiKey=="function"?await this.apiKey():this.apiKey,i=this.isVertex?`${this.getVertexApiURL(n,t?.beta)}/models/${n}:generateContent`:`https://generativelanguage.googleapis.com/v1beta/models/${n}:generateContent`,s={contents:[{role:"user",parts:[{inlineData:{mimeType:e.audio.mimeType??Gt(e.audio.format,e.audio.sampleRate),data:e.audio.data}},{text:e.prompt??"Generate a transcript of the speech in this audio."}]}]},a={"Content-Type":"application/json",...this.isVertex&&o?{Authorization:`Bearer ${o}`}:!this.isVertex&&o?{"x-goog-api-key":o}:{}},c=await(t?.fetch??globalThis.fetch)(i,{method:"POST",headers:this.credentialProvider?{...a,...await this.credentialProvider({profile:"google-gemini",operation:"transcribe",method:"POST",url:i})}:a,body:JSON.stringify(s),signal:t?.abortSignal});if(!c.ok)throw new Error(`Gemini transcription failed: ${c.status} ${c.statusText}`);return{text:(await c.json()).candidates?.[0]?.content?.parts?.map(d=>"text"in d?d.text:"").join("").trim()??""}}async speak(e,t){let n=e.model??"gemini-2.5-flash-preview-tts",o=typeof this.apiKey=="function"?await this.apiKey():this.apiKey,i=this.isVertex?`${this.getVertexApiURL(n,t?.beta)}/models/${n}:generateContent`:`https://generativelanguage.googleapis.com/v1beta/models/${n}:generateContent`,s=typeof e.voice=="object"?e.voice.id:e.voice??"Kore";return await wr({url:i,headers:{...this.isVertex&&o?{Authorization:`Bearer ${o}`}:!this.isVertex&&o?{"x-goog-api-key":o}:{},...this.credentialProvider?await this.credentialProvider({profile:"google-gemini",operation:"speak",method:"POST",url:i}):{}},body:{contents:[{role:"user",parts:[{text:e.text}]}],generationConfig:{responseModalities:["AUDIO"],speechConfig:{voiceConfig:{prebuiltVoiceConfig:{voiceName:s}}}}},format:e.format??"wav",transcript:e.text,fetch:t?.fetch,abortSignal:t?.abortSignal})}getVertexCacheContext(){if(!this.vertexConfig)return;let{projectId:e,region:t}=this.vertexConfig,o=`https://${wi(t)}/v1`,i=`projects/${e}/locations/${t}`;return{baseUrl:o,parent:i,modelResource:s=>`${i}/publishers/google/models/${s}`}}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}}hasProviderDeclaredTools(){return!!(this.options?.codeExecution||this.options?.googleSearchRetrieval||this.options?.googleSearch||this.options?.googleMaps||this.options?.urlContext)}buildToolState(e,t){let n=[];if(e.functions&&e.functions.length>0){let l=e.functions.map(u=>{let d={type:"object",properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]},p=u.parameters?Pc(u.parameters):void 0;return p===void 0||p&&typeof p=="object"&&Object.keys(p).length===0?p={...d}:p&&typeof p=="object"&&p.type==="object"&&(!("properties"in p)||!p.properties||Object.keys(p.properties).length===0)&&(p={...p,properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]}),{name:u.name,description:u.description,parameters:p}});n.push({function_declarations:l})}if(this.options?.codeExecution&&n.push({code_execution:{}}),this.options?.googleSearchRetrieval&&n.push({google_search_retrieval:{dynamic_retrieval_config:this.options.googleSearchRetrieval}}),this.options?.googleSearch&&n.push({google_search:{}}),this.options?.googleMaps){let l=this.options.googleMaps,u=l?.enableWidget!==void 0?{enableWidget:l.enableWidget}:{};n.push({google_maps:u})}this.options?.urlContext&&n.push({url_context:{}}),n.length===0&&(n=void 0);let o,i=Array.isArray(n)?n.some(l=>l&&Array.isArray(l.function_declarations)&&l.function_declarations.length>0):!1;if(e.functionCall)if(e.functionCall==="none")o={function_calling_config:{mode:"NONE"}};else if(e.functionCall==="auto")o={function_calling_config:{mode:"AUTO"}};else if(e.functionCall==="required")o={function_calling_config:{mode:"ANY"}};else{let l=e.functionCall.function?.name?{allowedFunctionNames:[e.functionCall.function.name]}:{};o={function_calling_config:{mode:"ANY"},...l}}else i&&(o={function_calling_config:{mode:"AUTO"}});this.options?.retrievalConfig&&(o={...o??{},retrievalConfig:{...this.options.retrievalConfig.latLng?{latLng:this.options.retrievalConfig.latLng}:{}}});let s=e.functions?.some(l=>l.cache)??!1,a=!!(n&&n.length>0)||!!o,c=!!t?.contextCache&&a;return{tools:n,toolConfig:o,cacheableTools:s||c}}createChatReq=async(e,t)=>{let n=e.model,o=e.modelConfig?.stream??this.config.stream,i=uc(this.config.audio,e.modelConfig?.audio),s=dc(n,this.config.audio,e.modelConfig?.audio),a=this.resolveServiceTier(n,t,s);if(!e.chatPrompt||e.chatPrompt.length===0)throw new Error("Chat prompt is empty");let c;if(s){if(this.isVertex)throw new Error("Gemini Live audio currently supports Google AI API-key WebSocket sessions only");c={name:"gemini-live-audio"}}else this.endpointId?c={name:o?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:c={name:o?`/models/${n}:streamGenerateContent?alt=sse`:`/models/${n}:generateContent`};!s&&this.isVertex&&(c.url=this.getVertexApiURL(n,t?.beta));let l=e.chatPrompt.filter(w=>w.role==="system").map(w=>w.content),u=l.length>0?{role:"user",parts:[{text:l.join(" ")}]}:void 0,d=[],p=e.chatPrompt.filter(w=>w.role!=="system");for(let w=0;w<p.length;w++){let N=p[w];switch(N.role){case"user":{let E=Array.isArray(N.content)?N.content.map((I,L)=>{switch(I.type){case"text":return{text:I.text};case"image":return{inlineData:{mimeType:I.mimeType,data:I.image}};case"audio":return{inlineData:{mimeType:I.mimeType??Gt(I.format,I.sampleRate),data:I.data}};case"file":return"fileUri"in I?{fileData:{mimeType:I.mimeType,fileUri:I.fileUri}}:{inlineData:{mimeType:I.mimeType,data:I.data}};default:throw new Error(`Chat prompt content type not supported (index: ${L})`)}}):[{text:N.content}];d.push({role:"user",parts:E});break}case"assistant":{let E=[],I=N.thoughtBlocks,L=N.functionCalls&&N.functionCalls.length>0,$=I?.[0],U=I?.map(F=>F.data).join("")??"",S=$?.signature;if(U&&E.push({...L?{}:{thought:!0},text:U,...S&&!L?{thought_signature:S}:{}}),N.functionCalls){let F=N.functionCalls.map((H,O)=>{let V;if(typeof H.function.params=="string"){let Y=H.function.params;if(Y.trim().length===0)V={};else try{V=JSON.parse(Y)}catch{throw new Error(`Failed to parse function params JSON: ${Y}`)}}else V=H.function.params;let W={functionCall:{name:H.function.name,args:V}};return S&&O===0&&(W.thought_signature=S),W});E.push(...F)}let T=N.audio?.transcript;if((N.content||T)&&E.push({text:N.content??T??""}),E.length===0)throw new Error("Assistant content is empty");d.push({role:"model",parts:E});break}case"function":{let E=[],I=N,L=w;for(;;){if(!("functionId"in I))throw new Error(`Chat prompt functionId is empty (index: ${L})`);if(E.push(...gp(p,L,I)),L+1<p.length&&p[L+1].role==="function")L++,I=p[L];else break}w=L,d.push({role:"user",parts:E});break}default:throw new Error(`Invalid role: ${JSON.stringify(N)} (index: ${w})`)}}let{tools:m,toolConfig:g}=this.buildToolState(e,t),h=this.getEffectiveMappings(n),x=mp({model:n,direct:this.config.thinking,logicalLevel:t?.thinkingTokenBudget,showThoughts:t?.showThoughts,...h}),f=e.modelConfig?.maxTokens??this.config.maxTokens;if(x.thinkingLevel&&f!==void 0)throw new Error("Cannot set maxTokens when using thinkingLevel with Gemini models. When thinking is enabled, the model manages output tokens automatically. Remove the maxTokens setting or disable thinking.");let y=$b(n),A={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,...y?{}:{temperature:e.modelConfig?.temperature??this.config.temperature},...!y&&e.modelConfig?.topP!==void 0?{topP:e.modelConfig.topP}:{},...y?{}:{topK:e.modelConfig?.topK??this.config.topK},frequencyPenalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,candidateCount:e.modelConfig?.n??this.config.n??1,stopSequences:e.modelConfig?.stopSequences??this.config.stopSequences,responseMimeType:"text/plain",...Object.keys(x).length>0?{thinkingConfig:x}:{}};if(!y&&Tc(n)&&(A.temperature===void 0||A.temperature<1)&&(A.temperature=1),s&&(e.responseFormat||this.config.responseFormat))throw new Error("Gemini Live audio models do not support structured response formats with audio output");if(e.responseFormat)if(e.responseFormat.type==="json_schema"&&e.responseFormat.schema){let w=e.responseFormat.schema.schema||e.responseFormat.schema;A.responseMimeType="application/json",A.responseJsonSchema=Pc(w)}else A.responseMimeType="application/json";else this.config.responseFormat&&this.config.responseFormat==="json_object"&&(A.responseMimeType="application/json");let b=this.config.safetySettings,v={contents:d,tools:m,toolConfig:g,systemInstruction:u,generationConfig:A,safetySettings:b,...a?{service_tier:a}:{}};if(s){let w=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;if(!w)throw new Error("GoogleGemini AI API key not set");c=Hd({model:n,request:v,apiKey:w,audio:i})}return[c,v]};createEmbedReq=async(e,t)=>{let n=e.embedModel;if(!n)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let o,i;return this.isVertex?(this.endpointId?o={name:`/${this.endpointId}:predict`}:o={name:`/models/${n}:predict`},o.url=this.getVertexApiURL(n,t?.beta),i={instances:e.texts.map(s=>({content:s,...this.config.embedType&&{taskType:this.config.embedType}})),parameters:{autoTruncate:this.config.autoTruncate,outputDimensionality:this.config.dimensions}}):(o={name:`/models/${n}:batchEmbedContents`},i={requests:e.texts.map(s=>({model:`models/${n}`,content:{parts:[{text:s}]},outputDimensionality:this.config.dimensions,...this.config.embedType&&{taskType:this.config.embedType}}))}),[o,i]};createChatResp=e=>{let t,n=e.candidates?.map(i=>{let s={index:0};switch(i.finishReason){case"MAX_TOKENS":s.finishReason="length";break;case"STOP":s.finishReason="stop";break;case"SAFETY":throw new He("Content was blocked due to safety settings",void 0,void 0);case"RECITATION":throw new He("Content was blocked due to recitation policy",void 0,void 0);case"MALFORMED_FUNCTION_CALL":throw new He("Function call was malformed and blocked",void 0,void 0);case"UNEXPECTED_TOOL_CALL":throw new He("Unexpected tool call",void 0,void 0);case"FINISH_REASON_UNSPECIFIED":throw new He("Finish reason unspecified",void 0,void 0);case"BLOCKLIST":throw new He("Content was blocked due to blocklist",void 0,void 0);case"PROHIBITED_CONTENT":throw new He("Content was blocked due to prohibited content",void 0,void 0);case"SPII":throw new He("Content was blocked due to SPII",void 0,void 0);case"OTHER":throw new He("Other finish reason",void 0,void 0)}if(!i.content||!i.content.parts)return s;for(let l of i.content.parts){if("text"in l){if("thought"in l&&l.thought||l.thought===!0){s.thought=l.text;let d=l.thoughtSignature||l.thought_signature;s.thoughtBlocks||(s.thoughtBlocks=[]),s.thoughtBlocks.push({data:l.text,encrypted:!1,...d?{signature:d}:{}})}else s.content=l.text;continue}if("functionCall"in l){let d=l.thoughtSignature||l.thought_signature;if(d)if(!s.thoughtBlocks||s.thoughtBlocks.length===0)s.thoughtBlocks=[{data:"",encrypted:!1,signature:d}];else{let p=s.thoughtBlocks[s.thoughtBlocks.length-1];p&&!p.signature&&(p.signature=d)}s.functionCalls=[...s.functionCalls??[],{id:qe(),type:"function",function:{name:l.functionCall.name,params:l.functionCall.args}}]}let u=zd(l);u&&(s.audio=u)}let a=i.citationMetadata?.citations;if(Array.isArray(a)&&a.length){let l=u=>u?`${u.year}-${String(u.month).padStart(2,"0")}-${String(u.day).padStart(2,"0")}`:void 0;s.citations=a.filter(u=>typeof u?.uri=="string").map(u=>({url:u.uri,title:u.title,license:u.license,publicationDate:l(u.publicationDate)}))}let c=i.groundingMetadata;if(c){if(Array.isArray(c.groundingChunks)){let l=c.groundingChunks.map(d=>d?.maps).filter(d=>d&&typeof d.uri=="string").map(d=>({url:d.uri,title:d.title}));l.length&&(s.citations=[...s.citations??[],...l]);let u=c.groundingChunks.map(d=>d?.retrievedContext).filter(d=>d&&(typeof d.uri=="string"||typeof d.media_id=="string")).map(d=>({url:d.uri??"",title:d.title,...typeof d.media_id=="string"?{mediaId:d.media_id}:{},...Array.isArray(d.page_numbers)?{pageNumbers:d.page_numbers}:{}}));u.length&&(s.citations=[...s.citations??[],...u])}typeof c.googleMapsWidgetContextToken=="string"&&(t=c.googleMapsWidgetContextToken)}return s});if(e.usageMetadata){let i=e.usageMetadata.cachedContentTokenCount??0,s=e.usageMetadata.serviceTier==="unspecified"?"standard":e.usageMetadata.serviceTier;this.tokensUsed={totalTokens:e.usageMetadata.totalTokenCount,promptTokens:e.usageMetadata.promptTokenCount-i,completionTokens:e.usageMetadata.candidatesTokenCount,thoughtsTokens:e.usageMetadata.thoughtsTokenCount,...i>0?{cacheReadTokens:i}:{},...s?{serviceTier:s}:{}}}let o={results:n,...e.responseId?{remoteId:e.responseId}:{},...e.modelVersion?{providerMetadata:{google:{modelVersion:e.modelVersion}}}:{}};return t&&(o.providerMetadata={...o.providerMetadata,google:{...o.providerMetadata?.google??{},mapsWidgetContextToken:t}}),o};createChatStreamResp=e=>this.createChatResp(e);createEmbedResp=e=>{let t;return this.isVertex?t=e.predictions.map(n=>n.embeddings.values):t=e.embeddings.map(n=>n.values),{embeddings:t}};supportsContextCache=e=>{let t=e;return ib.some(n=>t.includes(n)||n.includes(t))};buildCacheCreateOp=(e,t)=>{let n=e.model,o=t.contextCache?.ttlSeconds??3600,{tools:i,toolConfig:s,cacheableTools:a}=this.buildToolState(e,t),{systemInstruction:c,contents:l}=this.extractCacheableContent(e.chatPrompt);if(!c&&(!l||l.length===0)&&!a)return;let u=this.getVertexCacheContext(),d={model:u?u.modelResource(n):`models/${n}`,ttl:`${o}s`,displayName:`ax-cache-${Date.now()}`};c&&(d.systemInstruction=c),l&&l.length>0&&(d.contents=l),a&&(i&&i.length>0&&(d.tools=i),s&&(d.toolConfig=s));let p;return u?p=`/${u.parent}/cachedContents`:p="/cachedContents",{type:"create",apiConfig:{name:p,...u?{url:u.baseUrl}:{}},request:d,parseResponse:m=>{let g=m;if(g?.name)return{name:g.name,expiresAt:g.expireTime,tokenCount:g.usageMetadata?.totalTokenCount}}}};getContextCacheToolState=(e,t)=>{let{tools:n,toolConfig:o,cacheableTools:i}=this.buildToolState(e,t);if(!i)return;let s=e.functions?.map(({cache:c,...l})=>l);if(!!(s&&s.length>0)||!!e.functionCall)return{functions:s,functionCall:e.functionCall};if(n||o)return{functions:[{name:"__gemini_tool_state__",description:JSON.stringify({tools:n,toolConfig:o})}]}};buildCacheUpdateTTLOp=(e,t)=>{let n={ttl:`${t}s`},o=new URLSearchParams({updateMask:"ttl"}),i=`/${e}?${o.toString()}`,s=this.getVertexCacheContext();return{type:"update",apiConfig:{name:i,method:"PATCH",headers:{"Content-Type":"application/json"},...s?{url:s.baseUrl}:{}},request:n,parseResponse:a=>{let c=a;if(c?.name)return{name:c.name,expiresAt:c.expireTime,tokenCount:c.usageMetadata?.totalTokenCount}}}};buildCacheDeleteOp=e=>{let t=`/${e}`,n=this.getVertexCacheContext();return{type:"delete",apiConfig:{name:t,headers:{"Content-Type":"application/json"},...n?{url:n.baseUrl}:{}},request:{},parseResponse:()=>{}}};prepareCachedChatReq=async(e,t,n)=>{let o=e.model,i=this.resolveServiceTier(o,t),s=e.modelConfig?.stream??this.config.stream,{tools:a,toolConfig:c,cacheableTools:l}=this.buildToolState(e,t),{dynamicContents:u,dynamicSystemInstruction:d}=this.extractDynamicContent(e.chatPrompt),p;this.endpointId?p={name:s?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:p={name:s?`/models/${o}:streamGenerateContent?alt=sse`:`/models/${o}:generateContent`};let m=$b(o),g=this.getEffectiveMappings(o),h=mp({model:o,direct:this.config.thinking,logicalLevel:t.thinkingTokenBudget,showThoughts:t.showThoughts,...g}),x={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,...m?{}:{temperature:e.modelConfig?.temperature??this.config.temperature},...!m&&e.modelConfig?.topP!==void 0?{topP:e.modelConfig.topP}:{},...m?{}:{topK:e.modelConfig?.topK??this.config.topK},frequencyPenalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,candidateCount:e.modelConfig?.n??this.config.n??1,stopSequences:e.modelConfig?.stopSequences??this.config.stopSequences,responseMimeType:"text/plain",...Object.keys(h).length>0?{thinkingConfig:h}:{}};!m&&Tc(o)&&(x.temperature===void 0||x.temperature<1)&&(x.temperature=1);let f=this.config.safetySettings,y={contents:u,cachedContent:n,generationConfig:x,safetySettings:f,...i?{service_tier:i}:{}};return l||(a&&a.length>0&&(y.tools=a),c&&(y.toolConfig=c)),d&&(y.systemInstruction=d),{apiConfig:p,request:y}};extractCacheableContent(e){let t,n=[],o=-1;for(let i=e.length-1;i>=0;i--){let s=e[i];if("cache"in s&&s.cache){o=i;break}}for(let i=0;i<e.length;i++){let s=e[i];if(s.role==="system"){t={role:"user",parts:[{text:s.content}]};continue}if(o>=0&&i<=o)if(s.role==="user"){let a=[];if(typeof s.content=="string")a.push({text:s.content});else if(Array.isArray(s.content))for(let c of s.content)switch(c.type){case"text":a.push({text:c.text});break;case"image":a.push({inlineData:{mimeType:c.mimeType,data:c.image}});break;case"audio":a.push({inlineData:{mimeType:c.mimeType??Gt(c.format,c.sampleRate),data:c.data}});break;case"file":"fileUri"in c?a.push({fileData:{mimeType:c.mimeType,fileUri:c.fileUri}}):a.push({inlineData:{mimeType:c.mimeType,data:c.data}});break}a.length>0&&n.push({role:"user",parts:a})}else if(s.role==="assistant"){let a=[],c=s.thoughtBlocks,l=s.functionCalls&&s.functionCalls.length>0,u=c?.[0]?.signature,d=c?.map(p=>p.data).join("")??"";if(d&&a.push({...l?{}:{thought:!0},text:d,...u&&!l?{thought_signature:u}:{}}),s.functionCalls)for(let[p,m]of s.functionCalls.entries()){let g;if(typeof m.function.params=="string")try{g=JSON.parse(m.function.params)}catch{g={}}else g=m.function.params??{};let h={functionCall:{name:m.function.name,args:g}};u&&p===0&&(h.thought_signature=u),a.push(h)}s.content&&a.push({text:s.content}),a.length>0&&n.push({role:"model",parts:a})}else s.role==="function"&&n.push({role:"user",parts:gp(e,i,s)})}return{systemInstruction:t,contents:n}}extractDynamicContent(e){let n=[],o=-1;for(let i=e.length-1;i>=0;i--){let s=e[i];if("cache"in s&&s.cache){o=i;break}}for(let i=0;i<e.length;i++){let s=e[i];if(s.role!=="system"&&!(o>=0&&i<=o))if(s.role==="user"){let a=[];if(typeof s.content=="string")a.push({text:s.content});else if(Array.isArray(s.content)){for(let c of s.content)if(!("cache"in c&&c.cache))switch(c.type){case"text":a.push({text:c.text});break;case"image":a.push({inlineData:{mimeType:c.mimeType,data:c.image}});break;case"audio":a.push({inlineData:{mimeType:c.mimeType??Gt(c.format,c.sampleRate),data:c.data}});break;case"file":"fileUri"in c?a.push({fileData:{mimeType:c.mimeType,fileUri:c.fileUri}}):a.push({inlineData:{mimeType:c.mimeType,data:c.data}});break}}a.length>0&&n.push({role:"user",parts:a})}else if(s.role==="assistant"){let a=[],c=s.thoughtBlocks,l=s.functionCalls&&s.functionCalls.length>0,u=c?.[0]?.signature,d=c?.map(p=>p.data).join("")??"";if(d&&a.push({...l?{}:{thought:!0},text:d,...u&&!l?{thought_signature:u}:{}}),s.functionCalls)for(let[p,m]of s.functionCalls.entries()){let g;if(typeof m.function.params=="string")try{g=JSON.parse(m.function.params)}catch{g={}}else g=m.function.params??{};let h={functionCall:{name:m.function.name,args:g}};u&&p===0&&(h.thought_signature=u),a.push(h)}s.content&&a.push({text:s.content}),a.length>0&&n.push({role:"model",parts:a})}else s.role==="function"&&n.push({role:"user",parts:gp(e,i,s)})}return{dynamicContents:n,dynamicSystemInstruction:void 0}}},zi=class r extends It{static create(e){return new r(e)}constructor({apiKey:e,credentialProvider:t,projectId:n,region:o,endpointId:i,config:s,options:a,models:c,modelInfo:l}){let u=n!==void 0&&o!==void 0?{projectId:n,region:o}:void 0,d={...Gb(),...s};if(u&&d.serviceTier&&d.serviceTier!=="auto")throw new Error("Gemini inference service tiers are not supported by Vertex AI");let p,m,g;if(u){let{projectId:y,region:A}=u;if(!e&&!t)throw new Error("GoogleGemini Vertex API key or credential provider not set");if(e&&typeof e!="function")throw new Error("GoogleGemini Vertex API key must be a function for token-based authentication");let b;i?b="endpoints":b="publishers/google";let v=wi(A);g=(w,N)=>`https://${v}/${lI(w,N)}/projects/${y}/locations/${A}/${b}`,p=g(d.model),m=async()=>({...e?{Authorization:`Bearer ${typeof e=="function"?await e():e}`}:{}})}else{if(!e&&!t)throw new Error("GoogleGemini AI API key or credential provider not set");p="https://generativelanguage.googleapis.com/v1beta",m=async()=>({...e?{"x-goog-api-key":typeof e=="function"?await e():e}:{}})}let h=new fp(d,u,i,e,t,a,g);l=[...Ii,...l??[]];let x=y=>{let A=dc(y,{output:{enabled:!0}}),b=jt({model:y,modelInfo:l,models:c}),v=b?.supported?.structuredOutputs??!0,w=b?.supported?.structuredOutputModes??(v?["native","function"]:["function"]);return{functions:!0,streaming:!0,hasThinkingBudget:b?.supported?.thinkingBudget??!1,hasShowThoughts:b?.supported?.showThoughts??!1,structuredOutputs:w.includes("native"),structuredOutputModes:w,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:A?["pcm16","pcm"]:["wav","mp3","aac","ogg"],maxDuration:9.5*60,output:{supported:A,formats:["pcm16"],sampleRate:24e3,voices:["Kore"]}},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:h.supportsContextCache(y),types:["persistent"]},thinking:b?.supported?.thinkingBudget??!1,multiTurn:!0,serviceTiers:b?.supported?.serviceTiers??(u||A?[]:["standard","flex","priority"])}},f=c?.map(y=>{let A=y,b=A?.config;if(!b)return y;let v={};b.maxTokens!==void 0&&(v.maxTokens=b.maxTokens),b.temperature!==void 0&&(v.temperature=b.temperature),b.topP!==void 0&&(v.topP=b.topP),b.topK!==void 0&&(v.topK=b.topK),b.presencePenalty!==void 0&&(v.presencePenalty=b.presencePenalty),b.frequencyPenalty!==void 0&&(v.frequencyPenalty=b.frequencyPenalty),b.stopSequences!==void 0&&(v.stopSequences=b.stopSequences),b.endSequences!==void 0&&(v.endSequences=b.endSequences),b.stream!==void 0&&(v.stream=b.stream),b.n!==void 0&&(v.n=b.n);let w={...A};Object.keys(v).length>0&&(w.modelConfig={...A.modelConfig??{},...v});let N=b.thinking?.thinkingTokenBudget;if(typeof N=="number"){let E=String(A.model??d.model);if(Tc(E))throw new Error(`Gemini 3 models (${E}) do not support numeric thinkingTokenBudget. Use a logical thinkingTokenBudget level instead.`);let I=d.thinkingTokenBudgetLevels,L=[["minimal",I?.minimal??200],["low",I?.low??800],["medium",I?.medium??5e3],["high",I?.high??1e4],["highest",I?.highest??24500]],$="minimal",U=Number.POSITIVE_INFINITY;for(let[S,T]of L){let F=Math.abs(N-T);F<U&&(U=F,$=S)}w.thinkingTokenBudget=$}return b.thinking?.includeThoughts!==void 0&&(w.showThoughts=!!b.thinking.includeThoughts),b.serviceTier!==void 0&&(w.serviceTier=b.serviceTier),b.thinkingLevelMapping&&(w.thinkingLevelMapping=b.thinkingLevelMapping),b.thinkingTokenBudgetLevels&&(w.thinkingTokenBudgetLevels=b.thinkingTokenBudgetLevels),w});f?h.setModels(f):c&&h.setModels(c),h.setServiceTierSupportFor(y=>x(y).serviceTiers??[]),super(h,{name:"GoogleGeminiAI",apiURL:p,headers:m,modelInfo:l,defaults:{model:d.model,embedModel:d.embedModel},options:a,credentialProvider:t,profile:"google-gemini",supportFor:x,models:f??c})}};var hp=class{constructor(e={}){this.config=e;this.config.id=this.config.id??qe()}metrics={latency:{chat:{mean:0,p95:0,p99:0,samples:[]},embed:{mean:0,p95:0,p99:0,samples:[]}},errors:{chat:{count:0,rate:0,total:0},embed:{count:0,rate:0,total:0}}};getLastUsedChatModel(){return this.config.modelInfo?.name??"mock-model"}getLastUsedEmbedModel(){return this.config.embedModelInfo?.name??"mock-embed-model"}getLastUsedModelConfig(){return this.config.modelInfo?{maxTokens:this.config.modelInfo.maxTokens,temperature:.7,stream:this.config.features?.streaming??!1}:void 0}getName(){return this.config.name??"mock-ai-service"}getId(){return this.config.id??"mock-ai-service-id"}getFeatures(e){let t=this.config.features?.media;return{functions:this.config.features?.functions??!1,streaming:this.config.features?.streaming??!1,structuredOutputs:this.config.features?.structuredOutputs,...this.config.features?.structuredOutputModes?{structuredOutputModes:this.config.features.structuredOutputModes}:{},...this.config.features?.serviceTiers?{serviceTiers:this.config.features.serviceTiers}:{},media:{images:{supported:t?.images?.supported??!1,formats:t?.images?.formats??[]},audio:{supported:t?.audio?.supported??!1,formats:t?.audio?.formats??[]},files:{supported:t?.files?.supported??!1,formats:t?.files?.formats??[],uploadMethod:t?.files?.uploadMethod??"none"},urls:{supported:t?.urls?.supported??!1,webSearch:t?.urls?.webSearch??!1,contextFetching:t?.urls?.contextFetching??!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}getModelList(){return this.config.models}getMetrics(){return this.metrics}getEstimatedCost(){return 0}async chat(e,t){if(this.config.latencyMs&&await new Promise(n=>setTimeout(n,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock chat error");return this.updateMetrics("chat"),typeof this.config.chatResponse=="function"?await this.config.chatResponse(e,t):this.config.chatResponse??{results:[{index:0,content:"Mock response",finishReason:"stop"}],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:10,completionTokens:5,totalTokens:15}}}}async embed(e,t){if(this.config.latencyMs&&await new Promise(n=>setTimeout(n,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock embed error");return this.updateMetrics("embed"),typeof this.config.embedResponse=="function"?this.config.embedResponse(e):this.config.embedResponse??{embeddings:[[.1,.2,.3]],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:5,completionTokens:0,totalTokens:5}}}}async transcribe(e,t){if(this.config.shouldError)throw new Error(this.config.errorMessage??"Mock transcribe error");return typeof this.config.transcribeResponse=="function"?this.config.transcribeResponse(e):this.config.transcribeResponse??{text:"Mock transcription"}}async speak(e,t){if(this.config.shouldError)throw new Error(this.config.errorMessage??"Mock speech error");return typeof this.config.speechResponse=="function"?this.config.speechResponse(e):this.config.speechResponse??{data:"bW9jay1hdWRpbw==",format:"mp3",mimeType:"audio/mpeg",transcript:e.text}}setOptions(e){this.config.options=e}getOptions(){return this.config.options??{}}getLogger(){return this.config.options?.logger??(e=>{console.log(e)})}updateMetrics(e){let t=this.config.latencyMs??0;this.metrics.latency[e].samples.push(t);let n=this.metrics.latency[e].samples;if(this.metrics.latency[e].mean=n.reduce((o,i)=>o+i,0)/n.length,n.length>0){let o=[...n].sort((a,c)=>a-c),i=Math.max(0,Math.floor(o.length*.95)-1);this.metrics.latency[e].p95=o[i]??t;let s=Math.max(0,Math.floor(o.length*.99)-1);this.metrics.latency[e].p99=o[s]??t}if(this.config.shouldError){this.metrics.errors[e].count++,this.metrics.errors[e].total++;let o=this.metrics.latency[e].samples.length;this.metrics.errors[e].rate=o>0?this.metrics.errors[e].count/o:0}}};var xp=class r{options;lastUsedService;services=new Map;constructor(e){if(e.length===0)throw new Error("No AI services provided.");for(let[t,n]of e.entries())if("key"in n){if(this.services.has(n.key))throw new Error(`Duplicate model key: ${n.key}`);let{service:i,description:s,isInternal:a}=n;this.services.set(n.key,{service:i,description:s,isInternal:a})}else{let i=n.getModelList();if(!i)throw new Error(`Service ${t} \`${n.getName()}\` has no model list.`);for(let s of i){if(this.services.has(s.key)){let a=this.services.get(s.key)?.service;throw new Error(`Service ${t} \`${n.getName()}\` has duplicate model key: ${s.key} as service ${a?.getName()}`)}if("model"in s&&typeof s.model)this.services.set(s.key,{description:s.description,service:n,model:s.model});else if("embedModel"in s&&s.embedModel)this.services.set(s.key,{description:s.description,service:n,embedModel:s.embedModel});else throw new Error(`Key ${s.key} in model list for service ${t} \`${n.getName()}\` is missing a model or embedModel property.`)}}}static create(e){return new r(e)}getLastUsedChatModel(){return this.lastUsedService?.getLastUsedChatModel()}getLastUsedEmbedModel(){return this.lastUsedService?.getLastUsedEmbedModel()}getLastUsedModelConfig(){return this.lastUsedService?.getLastUsedModelConfig()}async chat(e,t){let n=e.model;if(!n)throw new Error("Model key must be specified for multi-service");let o=this.services.get(n);if(!o)throw new Error(`No service found for model key: ${n}`);if(this.lastUsedService=o.service,!o.model){let{model:i,...s}=e;return await o.service.chat(s,t)}return await o.service.chat({model:n,...e},t)}async embed(e,t){let n=e.embedModel;if(!n)throw new Error("Embed model key must be specified for multi-service");let o=this.services.get(n);if(!o)throw new Error(`No service found for embed model key: ${n}`);if(this.lastUsedService=o.service,!o.model){let{embedModel:i,...s}=e;return await o.service.embed(s,t)}return await o.service.embed({embedModel:n,...e},t)}async transcribe(e,t){let n=e.model;if(!n){let i=Array.from(this.services.values())[0]?.service;if(!i)throw new Error("No AI services provided.");return this.lastUsedService=i,await i.transcribe(e,t)}let o=this.services.get(n);if(!o)throw new Error(`No service found for transcription model key: ${n}`);return this.lastUsedService=o.service,await o.service.transcribe(e,t)}async speak(e,t){let n=e.model;if(!n){let i=Array.from(this.services.values())[0]?.service;if(!i)throw new Error("No AI services provided.");return this.lastUsedService=i,await i.speak(e,t)}let o=this.services.get(n);if(!o)throw new Error(`No service found for speech model key: ${n}`);return this.lastUsedService=o.service,await o.service.speak(e,t)}getId(){return`MultiServiceRouter:${Array.from(this.services.values()).map(e=>e.service.getId()).join(",")}`}getName(){return"MultiServiceRouter"}getModelList(){return Array.from(this.services).filter(([,e])=>!e.isInternal).map(([e,t])=>{if(t.model)return{key:e,description:t.description,model:t.model};if(t.embedModel)return{key:e,description:t.description,embedModel:t.embedModel};throw new Error(`Service ${e} has no model or embedModel`)})}getFeatures(e){if(e){let t=this.services.get(e);if(t)return t.service.getFeatures(e)}return{functions:!1,streaming:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}getMetrics(){let e=this.lastUsedService;if(!e){let t=this.services.values().next().value;t&&(e="service"in t?t.service:t)}if(!e)throw new Error("No service available to get metrics.");return e.getMetrics()}getEstimatedCost(e){return this.lastUsedService?.getEstimatedCost(e)??0}setOptions(e){for(let t of this.services.values())t.service.setOptions(e);this.options=e}getOptions(){return this.options??{}}getLogger(){let e=this.lastUsedService;if(!e){let t=this.services.values().next().value;t&&(e=t.service)}if(!e)throw new Error("No service available to get logger.");return e.getLogger()}setServiceEntry(e,t){this.services.set(e,t)}};async function yp(r,e,t={}){if(typeof r=="string")return[{type:"text",text:r}];if(!Array.isArray(r))return[{type:"text",text:String(r)}];let n=e.getFeatures(),o=[];for(let i of r)try{switch(i.type){case"text":o.push({type:"text",text:i.text});break;case"image":if(n.media.images.supported)o.push(i);else if(i.altText)o.push({type:"text",text:i.altText});else if(t.imageToText)try{let s=await t.imageToText(i.image);o.push({type:"text",text:s})}catch(s){throw new on(s,"image","vision analysis")}else switch(t.fallbackBehavior){case"error":throw new xt("Images",e.getName(),!1);case"skip":continue;default:o.push({type:"text",text:"[Image content not supported by this provider]"})}break;case"audio":if(n.media.audio.supported)i.transcription?o.push({type:"text",text:i.transcription}):o.push({type:"text",text:"[Audio content]"});else if(i.transcription)o.push({type:"text",text:i.transcription});else if(t.audioToText)try{let s=await t.audioToText(i.data,i.format);o.push({type:"text",text:s})}catch(s){throw new on(s,"audio","transcription")}else switch(t.fallbackBehavior){case"error":throw new xt("Audio",e.getName(),!1);case"skip":continue;case"degrade":o.push({type:"text",text:"[Audio content not supported by this provider]"})}break;case"file":if(n.media.files.supported)o.push(i);else if(i.extractedText)o.push({type:"text",text:i.extractedText});else if(t.fileToText)try{let s=await t.fileToText(i.data,i.mimeType);o.push({type:"text",text:s})}catch(s){throw new on(s,"file","text extraction")}else switch(t.fallbackBehavior){case"error":throw new xt("Files",e.getName(),!1);case"skip":continue;default:o.push({type:"text",text:`[File: ${i.filename} - content not accessible by this provider]`})}break;case"url":if(n.media.urls.supported)i.cachedContent?o.push({type:"text",text:i.cachedContent}):o.push({type:"text",text:`[Link: ${i.url}${i.title?` - ${i.title}`:""}]`});else if(i.cachedContent)o.push({type:"text",text:i.cachedContent});else if(t.urlToText)try{let s=await t.urlToText(i.url);o.push({type:"text",text:s})}catch(s){throw new on(s,"url","content fetching")}else switch(t.fallbackBehavior){case"error":throw new xt("URLs",e.getName(),!1);case"skip":continue;case"degrade":o.push({type:"text",text:`[Link: ${i.url}${i.title?` - ${i.title}`:""}]`})}break;default:typeof i=="object"&&i.text?o.push({type:"text",text:i.text}):o.push({type:"text",text:String(i)})}}catch(s){throw s instanceof xt||s instanceof on?s:new on(s,i.type||"unknown","content processing")}return o}function pI(r){let e=!1,t=!1,n=!1,o=!1;for(let i of r)if(i.role==="user"&&Array.isArray(i.content))for(let s of i.content)switch(s.type){case"image":e=!0;break;case"audio":t=!0;break;case"file":n=!0;break;case"url":o=!0;break}return{hasImages:e,hasAudio:t,hasFiles:n,hasUrls:o}}var Ap=class{providers;processingServices;config;constructor(e){this.providers=[e.providers.primary,...e.providers.alternatives],this.processingServices=e.processing,this.config=e.routing}async chat(e,t={}){let n=await this.selectProviderWithDegradation(e,t.routingOptions||{},t.serviceTier),o=await this.preprocessRequest(e,n.provider,t.processingOptions);try{return{response:await n.provider.chat(o,t),routing:n}}catch(i){if(i instanceof xt&&t.fallbackProviders?.length)return await this.tryFallbackProviders(e,t.fallbackProviders,t);throw i}}async preprocessRequest(e,t,n){let o={...n,fallbackBehavior:n?.fallbackBehavior||"degrade",imageToText:n?.imageToText||this.processingServices.imageToText,audioToText:n?.audioToText||this.processingServices.audioToText,fileToText:n?.fileToText||this.processingServices.fileToText,urlToText:n?.urlToText||this.processingServices.urlToText},i=[];for(let s of e.chatPrompt)if(s.role==="user"&&Array.isArray(s.content)){let a=await yp(s.content,t,o);a.every(l=>l.type==="text")&&a.length===1?i.push({...s,content:a[0].text}):i.push({...s,content:a.map(l=>l.type==="text"?{type:"text",text:l.text}:l)})}else i.push(s);return{...e,chatPrompt:i}}async selectProviderWithDegradation(e,t,n){let o=mo(e),i=[],s=[],a=[];try{let c=n&&n!=="auto"?this.providers.filter(d=>d.getFeatures(e.model).serviceTiers?.includes(n)):this.providers;if(c.length===0)throw new Error(`No provider supports service tier ${n}`);let l=Ud(e,c,{requireExactMatch:t.requireExactMatch??this.config.capability.requireExactMatch,allowDegradation:t.allowDegradation??this.config.capability.allowDegradation}),u=l.getFeatures();return o.hasImages&&!u.media.images.supported&&(s.push("Images will be converted to text descriptions"),i.push("Image-to-text conversion")),o.hasAudio&&!u.media.audio.supported&&(s.push("Audio will be transcribed to text"),i.push("Audio-to-text transcription")),o.hasFiles&&!u.media.files.supported&&(s.push("File content will be extracted to text"),i.push("File-to-text extraction")),o.hasUrls&&!u.media.urls.supported&&(s.push("URL content will be pre-fetched"),i.push("URL content fetching")),o.requiresStreaming&&!u.streaming&&a.push("Streaming not supported - will use non-streaming mode"),o.requiresCaching&&!u.caching.supported&&a.push("Content caching not supported"),{provider:l,processingApplied:i,degradations:s,warnings:a}}catch(c){throw new Error(`Provider selection failed: ${c instanceof Error?c.message:"Unknown error"}`)}}async tryFallbackProviders(e,t,n){for(let o of t)if(!(n.serviceTier&&n.serviceTier!=="auto"&&!o.getFeatures(e.model).serviceTiers?.includes(n.serviceTier)))try{let i={provider:o,processingApplied:["Fallback provider selection"],degradations:["Using fallback provider due to primary provider failure"],warnings:[]},s=await this.preprocessRequest(e,o,{fallbackBehavior:"degrade"});return{response:await o.chat(s,n),routing:i}}catch{}throw new Error("All fallback providers failed")}async getRoutingRecommendation(e){return await this.selectProviderWithDegradation(e,{})}async validateRequest(e){let t=mo(e),n=[],o=[];try{let i=await this.selectProviderWithDegradation(e,{});return i.degradations.length>0&&(n.push(...i.degradations),o.push("Consider using a provider that natively supports all media types")),i.warnings.length>0&&n.push(...i.warnings),t.hasImages&&this.processingServices.imageToText===void 0&&(this.providers.some(a=>a.getFeatures().media.images.supported)||(n.push("No image processing service available and no providers support images"),o.push("Add imageToText processing service or use image-capable provider"))),t.hasAudio&&this.processingServices.audioToText===void 0&&(this.providers.some(a=>a.getFeatures().media.audio.supported)||(n.push("No audio processing service available and no providers support audio"),o.push("Add audioToText processing service or use audio-capable provider"))),{canHandle:n.length===0||i.degradations.length>0,issues:n,recommendations:o}}catch(i){return{canHandle:!1,issues:[`Cannot route request: ${i instanceof Error?i.message:"Unknown error"}`],recommendations:["Add more providers or processing services to handle this request"]}}}getRoutingStats(){let e={};for(let t of this.providers){let n=t.getFeatures(),o=t.getName();n.functions&&(e.Functions=e.Functions||[],e.Functions.push(o)),n.streaming&&(e.Streaming=e.Streaming||[],e.Streaming.push(o)),n.media.images.supported&&(e.Images=e.Images||[],e.Images.push(o)),n.media.audio.supported&&(e.Audio=e.Audio||[],e.Audio.push(o)),n.media.files.supported&&(e.Files=e.Files||[],e.Files.push(o)),n.media.urls.supported&&(e.URLs=e.URLs||[],e.URLs.push(o)),n.caching.supported&&(e.Caching=e.Caching||[],e.Caching.push(o))}return{totalProviders:this.providers.length,capabilityMatrix:e,recommendedProvider:this.providers[0]?.getName()||"None"}}};var Bb=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...Qe()}),mI=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...Rr()}),bp=class{constructor(e,t){this.config=e;this.engine=t}tokensUsed;engine;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,endSequences:e.endSequences,stream:e.stream,n:e.n}}createChatReq(e){let t=e.model,n=this.config.supportsFunctions??!1,o=e.chatPrompt.map(u=>{if(u.role==="function"){let m=u.content?.length?u.content.map(g=>g.type==="text"?g.text:g.type==="image"?g.altText??`[MCP image result: ${g.mimeType}]`:g.type==="audio"?g.transcription??`[MCP audio result: ${g.mimeType??"application/octet-stream"}]`:g.type==="url"?g.cachedContent??[g.title,g.description,g.url].filter(Boolean).join(`
1260
1260
  `):g.extractedText??`[MCP file result: ${g.filename??"file"} (${g.mimeType})]`).join(`
1261
1261
  `):typeof u.result=="string"?u.result:JSON.stringify(u.result);return{role:"tool",tool_call_id:u.functionId,content:m}}let d="";typeof u.content=="string"?d=u.content:Array.isArray(u.content)&&(d=u.content.filter(m=>m.type==="text").map(m=>m.text).join(`
1262
1262
  `));let p={role:u.role,content:d};return u.role==="assistant"&&u.functionCalls?.length?{...p,tool_calls:u.functionCalls.map(m=>({id:m.id,type:"function",function:{name:m.function.name,arguments:typeof m.function.params=="string"?m.function.params:JSON.stringify(m.function.params||{})}}))}:p}),i=n?e.functions?.map(u=>({type:"function",function:{name:u.name,description:u.description,parameters:u.parameters||{type:"object",properties:{}}}})):void 0,s=n&&i?.length?e.functionCall===void 0?"auto":e.functionCall:void 0,a=this.createResponseFormat(e.responseFormat),c={name:"/chat/completions",localCall:async(u,d)=>{try{let p=await this.engine.chat.completions.create({...u,stream:d??!1});return d?this.toReadableStream(p):p}catch(p){let m=p instanceof Error?p.message:String(p);throw new Error(`WebLLM API error: ${m}`,{cause:p})}}},l={model:t,messages:o,...i?.length?{tools:i}:{},...s?{tool_choice:s}:{},...a?{response_format:a}:{},max_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,frequency_penalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,stop:e.modelConfig?.stopSequences??this.config.stopSequences,stream:e.modelConfig?.stream??this.config.stream,...e.modelConfig?.stream??this.config.stream?{stream_options:{include_usage:!0}}:{},n:e.modelConfig?.n??this.config.n,...this.config.logitBias!==void 0?{logit_bias:this.config.logitBias}:{},...this.config.logProbs!==void 0?{logprobs:this.config.logProbs}:{},...this.config.topLogprobs!==void 0?{top_logprobs:this.config.topLogprobs}:{}};return[c,l]}createResponseFormat(e){if(e?.type==="json_object")return{type:"json_object"};if(e?.type==="json_schema")return{type:"json_schema",json_schema:e.schema??e.json_schema}}toReadableStream(e){if(this.isReadableStream(e))return e;if(e&&typeof e[Symbol.asyncIterator]=="function")return new ReadableStream({async start(t){try{for await(let n of e)t.enqueue(n);t.close()}catch(n){t.error(n)}}});throw new Error("WebLLM streaming response is not async iterable")}isReadableStream(e){return e!==null&&typeof e=="object"&&typeof e.getReader=="function"}createEmbedReq=e=>{throw new Error("WebLLM does not support embeddings")};createChatResp=e=>(this.tokensUsed={promptTokens:e.usage?.prompt_tokens??0,completionTokens:e.usage?.completion_tokens??0,totalTokens:e.usage?.total_tokens??0},{results:e.choices.map((n,o)=>{let i="stop";switch(n.finish_reason){case"stop":i="stop";break;case"length":i="length";break;case"tool_calls":i="function_call";break;case"content_filter":i="content_filter";break;default:i="stop";break}let s=n.message.tool_calls?.map(a=>({id:a.id,type:"function",function:{name:a.function.name,params:a.function.arguments}}));return{index:o,id:e.id,content:n.message.content||"",functionCalls:s,finishReason:i,logprobs:n.logprobs?this.createLogprobs(n.logprobs):void 0}}),remoteId:e.id});createChatStreamResp=(e,t)=>{let n=t,o=e.choices[0];if(!o)throw new Error("No choice in WebLLM stream response");if(o.delta.content&&(n.content=(n.content||"")+o.delta.content),o.delta.tool_calls){n.toolCalls||(n.toolCalls=[]);for(let c of o.delta.tool_calls){let l=n.toolCalls[c.index];l?(l.id=c.id??l.id,l.type=c.type??l.type,l.function??={},l.function.name=c.function?.name??l.function.name,c.function?.arguments&&(l.function.arguments=(l.function?.arguments||"")+c.function.arguments)):n.toolCalls[c.index]={id:c.id,type:c.type,function:{name:c.function?.name,arguments:c.function?.arguments||""}}}}if(o.logprobs){let c=this.createLogprobs(o.logprobs);n.logprobs={content:[...n.logprobs?.content??[],...c.content??[]]}}e.usage&&(this.tokensUsed={promptTokens:e.usage.prompt_tokens,completionTokens:e.usage.completion_tokens,totalTokens:e.usage.total_tokens});let i;if(o.finish_reason)switch(o.finish_reason){case"stop":i="stop";break;case"length":i="length";break;case"tool_calls":i="function_call";break;case"content_filter":i="content_filter";break;default:i="stop";break}let s=n.toolCalls?.map(c=>({id:c.id||"",type:"function",function:{name:c.function?.name||"",params:c.function?.arguments||""}}));return{results:[{index:0,id:e.id,content:n.content||"",functionCalls:s,finishReason:i,logprobs:n.logprobs}],remoteId:e.id}};createLogprobs(e){return{content:e.content.map(t=>({token:t.token,logprob:t.logprob,topLogprobs:t.top_logprobs?.map(n=>({token:n.token,logprob:n.logprob}))}))}}createEmbedResp(e){throw new Error("WebLLM does not support embeddings")}},Hi=class extends It{constructor({engine:e,config:t,options:n,models:o}){if(!e)throw new Error("WebLLM engine instance is required");let i={...Bb(),...t},s=new bp(i,e);super(s,{name:"WebLLM",apiURL:void 0,headers:async()=>({}),modelInfo:qi,defaults:{model:i.model},supportFor:a=>({functions:i.supportsFunctions??!1,streaming:!0,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}),options:n,models:o})}};function gI(r){return Ic.create(r)}var Ic=class r{ai;static create(e){return new r(e)}constructor(e){let t=up(e.name);switch(t.transport){case"openai-chat":t.id==="openai"?this.ai=new Ni(e):this.ai=new Di(e);break;case"openai-responses":t.id==="openai-responses"?this.ai=new $i(e):this.ai=new ji(e);break;case"anthropic-messages":this.ai=new ki(e);break;case"gemini-generate-content":this.ai=new zi(e);break;case"webllm":this.ai=new Hi(e);break;default:throw new Error(`Unsupported AI transport: ${t.transport}`)}}getName(){return this.ai.getName()}getId(){return this.ai.getId()}getFeatures(e){return this.ai.getFeatures(e)}getModelList(){return this.ai.getModelList()}getLastUsedChatModel(){return this.ai.getLastUsedChatModel()}getLastUsedEmbedModel(){return this.ai.getLastUsedEmbedModel()}getLastUsedModelConfig(){return this.ai.getLastUsedModelConfig()}getMetrics(){return this.ai.getMetrics()}getEstimatedCost(e){return this.ai.getEstimatedCost(e)}async chat(e,t){return await this.ai.chat(e,t)}async embed(e,t){return await this.ai.embed(e,t)}async transcribe(e,t){return await this.ai.transcribe(e,t)}async speak(e,t){return await this.ai.speak(e,t)}setOptions(e){this.ai.setOptions(e)}getOptions(){return this.ai.getOptions()}getLogger(){return this.ai.getLogger()}};function qb(r,e){return r.filter(t=>!e.has(t))}function zb(r){let e={};for(let t of r)e[t]=(e[t]||0)+1;return e}function kr(r){let e=r.normalize("NFD");return e=e.replace(/\b(a|an|the)\b/g," "),e=e.split(/\s+/).join(" "),e=e.replace(/[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/g,""),e.toLowerCase()}function fI(r,e){return kr(r)===kr(e)?1:0}function hI(r,e){let t=kr(r).split(" "),n=kr(e).split(" "),o=zb(t),i=zb(n),s=0;for(let l in o){let u=o[l]??0,d=i[l]??0;s+=Math.min(u,d)}if(s===0)return 0;let a=s/t.length,c=s/n.length;return 2*a*c/(a+c)}function xI(r,e,t,n=!1){let o=kr(r).split(" "),i=kr(e).split(" "),s=kr(t).split(" "),a=new Set([...Sa,...o]);i=qb(i,a),s=qb(s,a);let c=0,l=c/i.length,u=c/s.length,d=2*l*u/(l+u);return n?u:d}var yI={emScore:fI,f1Score:hI,novelF1ScoreOptimized:xI};var Cp=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,o=0;for(let s=0;s<n;s++){let a=this.examples[s];if(!a)throw new Error("Invalid example");try{let c=await this.program.forward(this.ai,a,{maxRetries:1}),l=await e({prediction:c,example:a});o+=l}catch(c){console.warn(`Program evaluation failed for example ${s}: ${c instanceof Error?c.message:"Unknown error"}`)}}let i=n>0?o/n:0;this.ai.getOptions().debug&&console.log(`