@ax-llm/ax 24.0.4 → 24.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs +3 -1
- package/index.cjs.map +1 -1
- package/index.d.cts +13 -7
- package/index.d.ts +13 -7
- package/index.global.js +3 -1
- package/index.global.js.map +1 -1
- package/index.js +3 -1
- package/index.js.map +1 -1
- package/package.json +1 -1
- package/skills/ax-agent-context.md +1 -1
- package/skills/ax-agent-memory-skills.md +1 -1
- package/skills/ax-agent-observability.md +1 -1
- package/skills/ax-agent-optimize.md +1 -1
- package/skills/ax-agent-rlm.md +1 -1
- package/skills/ax-agent.md +1 -1
- package/skills/ax-ai.md +2 -1
- package/skills/ax-audio.md +1 -1
- package/skills/ax-flow.md +1 -1
- package/skills/ax-gen.md +1 -1
- package/skills/ax-gepa.md +1 -1
- package/skills/ax-llm.md +1 -1
- package/skills/ax-mcp.md +1 -1
- package/skills/ax-playbook.md +1 -1
- package/skills/ax-refine.md +1 -1
- package/skills/ax-signature.md +1 -1
package/index.d.cts
CHANGED
|
@@ -10356,6 +10356,13 @@ declare class AxBalancer<TServices extends readonly AxAIService<any, any, any>[]
|
|
|
10356
10356
|
getLogger(): AxLoggerFunction;
|
|
10357
10357
|
}
|
|
10358
10358
|
|
|
10359
|
+
type AxUserMessage = Extract<AxChatRequest['chatPrompt'][number], {
|
|
10360
|
+
role: 'user';
|
|
10361
|
+
}>;
|
|
10362
|
+
type AxUserContentItem = Exclude<AxUserMessage['content'], string>[number];
|
|
10363
|
+
type AxUserImageContent = Extract<AxUserContentItem, {
|
|
10364
|
+
type: 'image';
|
|
10365
|
+
}>;
|
|
10359
10366
|
/**
|
|
10360
10367
|
* Configuration options for content processing and fallback behavior
|
|
10361
10368
|
*/
|
|
@@ -10372,14 +10379,13 @@ interface ProcessingOptions {
|
|
|
10372
10379
|
urlToText?: (url: string) => Promise<string>;
|
|
10373
10380
|
}
|
|
10374
10381
|
/**
|
|
10375
|
-
* Represents
|
|
10382
|
+
* Represents content after provider-specific processing. Native image content
|
|
10383
|
+
* is retained when the provider advertises image support.
|
|
10376
10384
|
*/
|
|
10377
|
-
|
|
10378
|
-
/** Content type after processing (always 'text') */
|
|
10385
|
+
type ProcessedContent = {
|
|
10379
10386
|
type: 'text';
|
|
10380
|
-
/** The processed text content */
|
|
10381
10387
|
text: string;
|
|
10382
|
-
}
|
|
10388
|
+
} | AxUserImageContent;
|
|
10383
10389
|
/**
|
|
10384
10390
|
* Indicates what types of media content are present in a request
|
|
10385
10391
|
*/
|
|
@@ -10406,7 +10412,7 @@ interface MediaRequirements {
|
|
|
10406
10412
|
* @param content - The content to process (string, object, or array of content items)
|
|
10407
10413
|
* @param provider - The target AI service provider
|
|
10408
10414
|
* @param options - Processing options including fallback behavior and conversion services
|
|
10409
|
-
* @returns Promise resolving to
|
|
10415
|
+
* @returns Promise resolving to processed text and supported native media items
|
|
10410
10416
|
* @throws AxMediaNotSupportedError when fallbackBehavior is 'error' and content is unsupported
|
|
10411
10417
|
* @throws AxContentProcessingError when a conversion service fails
|
|
10412
10418
|
*
|
|
@@ -10423,7 +10429,7 @@ interface MediaRequirements {
|
|
|
10423
10429
|
* imageToText: async (data) => await visionService.describe(data)
|
|
10424
10430
|
* }
|
|
10425
10431
|
* );
|
|
10426
|
-
* // Result: [{ type: 'text', text: 'Analyze this:' }, { type: 'text', text: 'Chart showing sales data' }]
|
|
10432
|
+
* // Result for a text-only provider: [{ type: 'text', text: 'Analyze this:' }, { type: 'text', text: 'Chart showing sales data' }]
|
|
10427
10433
|
* ```
|
|
10428
10434
|
*/
|
|
10429
10435
|
declare function axProcessContentForProvider(content: any, provider: AxAIService, options?: ProcessingOptions): Promise<ProcessedContent[]>;
|
package/index.d.ts
CHANGED
|
@@ -10356,6 +10356,13 @@ declare class AxBalancer<TServices extends readonly AxAIService<any, any, any>[]
|
|
|
10356
10356
|
getLogger(): AxLoggerFunction;
|
|
10357
10357
|
}
|
|
10358
10358
|
|
|
10359
|
+
type AxUserMessage = Extract<AxChatRequest['chatPrompt'][number], {
|
|
10360
|
+
role: 'user';
|
|
10361
|
+
}>;
|
|
10362
|
+
type AxUserContentItem = Exclude<AxUserMessage['content'], string>[number];
|
|
10363
|
+
type AxUserImageContent = Extract<AxUserContentItem, {
|
|
10364
|
+
type: 'image';
|
|
10365
|
+
}>;
|
|
10359
10366
|
/**
|
|
10360
10367
|
* Configuration options for content processing and fallback behavior
|
|
10361
10368
|
*/
|
|
@@ -10372,14 +10379,13 @@ interface ProcessingOptions {
|
|
|
10372
10379
|
urlToText?: (url: string) => Promise<string>;
|
|
10373
10380
|
}
|
|
10374
10381
|
/**
|
|
10375
|
-
* Represents
|
|
10382
|
+
* Represents content after provider-specific processing. Native image content
|
|
10383
|
+
* is retained when the provider advertises image support.
|
|
10376
10384
|
*/
|
|
10377
|
-
|
|
10378
|
-
/** Content type after processing (always 'text') */
|
|
10385
|
+
type ProcessedContent = {
|
|
10379
10386
|
type: 'text';
|
|
10380
|
-
/** The processed text content */
|
|
10381
10387
|
text: string;
|
|
10382
|
-
}
|
|
10388
|
+
} | AxUserImageContent;
|
|
10383
10389
|
/**
|
|
10384
10390
|
* Indicates what types of media content are present in a request
|
|
10385
10391
|
*/
|
|
@@ -10406,7 +10412,7 @@ interface MediaRequirements {
|
|
|
10406
10412
|
* @param content - The content to process (string, object, or array of content items)
|
|
10407
10413
|
* @param provider - The target AI service provider
|
|
10408
10414
|
* @param options - Processing options including fallback behavior and conversion services
|
|
10409
|
-
* @returns Promise resolving to
|
|
10415
|
+
* @returns Promise resolving to processed text and supported native media items
|
|
10410
10416
|
* @throws AxMediaNotSupportedError when fallbackBehavior is 'error' and content is unsupported
|
|
10411
10417
|
* @throws AxContentProcessingError when a conversion service fails
|
|
10412
10418
|
*
|
|
@@ -10423,7 +10429,7 @@ interface MediaRequirements {
|
|
|
10423
10429
|
* imageToText: async (data) => await visionService.describe(data)
|
|
10424
10430
|
* }
|
|
10425
10431
|
* );
|
|
10426
|
-
* // Result: [{ type: 'text', text: 'Analyze this:' }, { type: 'text', text: 'Chart showing sales data' }]
|
|
10432
|
+
* // Result for a text-only provider: [{ type: 'text', text: 'Analyze this:' }, { type: 'text', text: 'Chart showing sales data' }]
|
|
10427
10433
|
* ```
|
|
10428
10434
|
*/
|
|
10429
10435
|
declare function axProcessContentForProvider(content: any, provider: AxAIService, options?: ProcessingOptions): Promise<ProcessedContent[]>;
|
package/index.global.js
CHANGED
|
@@ -1263,7 +1263,9 @@ ${e}
|
|
|
1263
1263
|
`);throw new Ve(r,void 0,e)}return n.filter(r=>r.type==="output_text").map(r=>r.text).join(`
|
|
1264
1264
|
`)};function pl(n){let e=[];for(let t of n??[])if(t?.type==="output_text"&&Array.isArray(t.annotations))for(let r of t.annotations)r&&r.type==="url_citation"&&typeof r.url=="string"&&e.push({url:r.url,title:r.title,description:r.description});return e.length?e:void 0}var Ls=()=>({model:"gpt-4o",embedModel:"text-embedding-ada-002",temperature:.7,topP:1,stream:!0}),GC=()=>({...Ls(),model:"gpt-4o",temperature:.5}),BC=()=>({...Ls(),model:"gpt-4o",temperature:.9}),zr=class extends wt{constructor({apiKey:e,credentialProvider:t,credentialProfile:r,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 ml(o,i?.streamingUsage??!0,l),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 b=h?.thinking?.thinkingTokenBudget;if(typeof b=="number"){let A=[["minimal",200],["low",800],["medium",5e3],["high",1e4],["highest",24500]],v="minimal",w=Number.POSITIVE_INFINITY;for(let[N,k]of A){let E=Math.abs(b-k);E<w&&(w=E,v=N)}y.thinkingTokenBudget=v}return h?.thinking?.includeThoughts!==void 0&&(y.showThoughts=!!h.thinking.includeThoughts),y});super(d,{name:"OpenAI",apiURL:s||"https://api.openai.com/v1",headers:async()=>e?{Authorization:`Bearer ${e}`}:{},profile:r,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")}},Qo=class extends zr{constructor({apiKey:e,credentialProvider:t,config:r,options:o,models:i,modelInfo:s}){if((!e||e==="")&&!t)throw new Error("OpenAI API key not set");s=[...Ko,...s??[]];let a=c=>{let l=Bt({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}};super({apiKey:e,credentialProvider:t,credentialProfile:"openai-responses",config:{...Ls(),...r},options:o,modelInfo:s,models:i,supportFor:a})}};var gl={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"]},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"},"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"]},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"]},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"},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"]},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-query",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"]},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"],reviewedAt:"2026-08-17"},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"]},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"]},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"],reviewedAt:"2026-08-17"},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"]},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:"high",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"]},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"]},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"},modelRules:[],sources:["https://docs.mistral.ai/api/"],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"]},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"]},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"},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"]}}],sources:["https://docs.x.ai/developers/model-capabilities/text/reasoning","https://docs.x.ai/developers/models/grok-4.5"],reviewedAt:"2026-08-18"},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"]},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"]},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"]},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"],reviewedAt:"2026-08-18"},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"]},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"]},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"},"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"]},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"]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.aws.amazon.com/bedrock/latest/userguide/inference-chat-completions-mantle.html"],reviewedAt:"2026-08-17"},"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"]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://learn.microsoft.com/en-us/rest/api/microsoft-foundry/azureopenai/chat"],reviewedAt:"2026-08-17"},"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"]},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"]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.databricks.com/aws/en/machine-learning/model-serving/query-chat-models"],reviewedAt:"2026-08-17"},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"]},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"]},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"],reviewedAt:"2026-08-18"},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"]},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"],reviewedAt:"2026-08-18"},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"]},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"],reviewedAt:"2026-08-18"},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"]},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"]},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"]},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"]},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"]},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"]},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"]},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"]},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"]},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"]},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"]},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"]},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"]},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"]},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"]},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"]},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"]},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"]},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"]},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"]},operations:{chat:{path:"/chat/completions",dialect:"openai-chat"}},modelRules:[],sources:["https://docs.baseten.co/development/model/deployment/inference"],reviewedAt:"2026-08-17"}},fl={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"},Ns=["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 Xo=(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))(Xo||{}),vm=(e=>(e.GrokEmbedSmall="grok-embed-small",e))(vm||{});var qC=()=>({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}}),Rm=()=>structuredClone({model:"grok-4.6",...Qe()}),zC=()=>structuredClone({...Rm(),model:"grok-4.6"}),VC=()=>structuredClone({...Qe(),model:"grok-voice-think-fast-1.0",audio:qC(),stream:!1}),wm=n=>n==="grok-voice-think-fast-1.0"||n==="grok-voice-fast-1.0"||n.startsWith("grok-voice-"),$s=(n,e)=>bt(bt(qC(),n),e),hl=(n,e,t)=>wm(n)&&qt($s(e,t)),xl=n=>Jo({...n,apiName:"grok-realtime-audio",providerName:"Grok Realtime",wsURL:e=>`wss://api.x.ai/v1/realtime?model=${encodeURIComponent(e)}`,createSessionUpdate:({request:e,audio:t})=>{let r=e.messages.filter(s=>s.role==="system").map(s=>s.content).join(`
|
|
1265
1265
|
`),o=t.input,i=t.output;return{type:"session.update",session:{voice:typeof i?.voice=="object"?i.voice.id:i?.voice??"eve",...r?{instructions:r}:{},turn_detection:null,audio:{input:{format:{type:"audio/pcm",rate:o?.sampleRate??24e3}},output:{format:{type:"audio/pcm",rate:i?.sampleRate??24e3}}}}}}});var HC=gl,JI=fl,Us=()=>Ns.map(n=>{let e=HC[n];return{id:n,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}}),yl=n=>{let e=Al(n);return Us().find(t=>t.id===e.id)},Tm=n=>{let e=JI[n.toLowerCase()];if(!e)throw new Error(`Unknown AI profile "${n}". Use one of: ${Ns.join(", ")}`);return e},Al=n=>HC[Tm(n)],YI=(n,e)=>(e.match.exact?.includes(n)??!1)||(e.match.prefix?.some(t=>n.startsWith(t))??!1)||(e.match.contains?.some(t=>n.toLowerCase().includes(t.toLowerCase()))??!1),WC=(n,e)=>n.modelRules.find(t=>YI(e,t)),QI=n=>({images:{supported:n.images??!1,formats:n.images?["image/jpeg","image/png"]:[]},audio:{supported:n.audio??!1,formats:n.audio?["wav","mp3","pcm16"]:[],output:{supported:n.audioOutput??!1,formats:n.audioOutput?["wav","mp3","pcm16"]:[]}},files:n.files?{supported:!0,formats:["application/pdf","text/plain"],uploadMethod:n.files.uploadMethod}:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:n.webSearch??!1,contextFetching:!1}}),Ds=(n,e)=>{let t=Al(n),r=WC(t,e),o={...t.capabilities,...r?.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:QI(o),caching:o.caching?{supported:!0,types:[...o.caching.types],cacheBreakpoints:o.caching.cacheBreakpoints}:{supported:!1,types:[]},thinking:o.thinking,multiTurn:o.multiTurn}},XI=n=>{let e=n.modelRules.flatMap(i=>i.response?.reasoningFields??[]),t=n.modelRules.flatMap(i=>i.response?.reasoningDetailsFields??[]),r=n.modelRules.find(i=>i.replay?.assistantReasoningField)?.replay?.assistantReasoningField,o=n.modelRules.find(i=>i.replay?.assistantReasoningDetailsField)?.replay?.assistantReasoningDetailsField;return!r&&!o&&e.length===0&&t.length===0?"none":{...r||e[0]?{assistantField:r??e[0]}:{},responseFields:[...new Set(e)],...o?{assistantDetailsField:o}:{},responseDetailsFields:[...new Set(t)]}},ZI=n=>{let e=n.messages;if(Array.isArray(e))for(let t of e){if(!t||typeof t!="object")continue;let r=t.content;if(Array.isArray(r))for(let o of r){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})}}},Sm=n=>Array.isArray(n)?n.map(Sm):!n||typeof n!="object"?n:Object.fromEntries(Object.entries(n).map(([e,t])=>[e.replace(/[A-Z]/g,r=>`_${r.toLowerCase()}`),Sm(t)])),km=(n,e,t)=>{if(!e)return;let o=t.thinkingTokenBudget??(typeof n.reasoning_effort=="string"?n.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 n.reasoning_effort:s!==void 0&&(n.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=n,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"?n.thinking={type:a?"enabled":"disabled"}:e.reasoning==="openrouter"&&(c&&typeof n.reasoning_effort=="string"&&(n.reasoning={effort:n.reasoning_effort}),delete n.reasoning_effort),e.toolChoice==="unforced"){let l=n.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 n.tools,delete n.tool_choice}if(a)for(let l of e.dropWhenThinking??[])delete n[l];for(let l of e.dropFields??[])delete n[l];for(let[l,u]of Object.entries(e.copyFields??{}))n[l]!==void 0&&(n[u]=n[l]);for(let[l,u]of Object.entries(e.renameFields??{}))n[l]!==void 0&&(n[u]=n[l],delete n[l]);for(let[l,u]of Object.entries(e.enumMaps??{})){let d=n[l];typeof d=="string"&&d in u&&(n[l]=u[d])}for(let[l,u]of Object.entries(e.constructObjects??{})){let d=Object.fromEntries(Object.entries(u).filter(([,p])=>n[p]!==void 0).map(([p,m])=>[p,n[m]]));Object.keys(d).length>0&&(n[l]=d)}if(e.imageURLShape==="object"&&ZI(n),e.optionDialect==="search-parameters"){let l=t.searchParameters??t.search_parameters;l!==void 0&&(n.search_parameters=Sm(l))}},Pm=(n,e,t)=>{let r=t?.find(s=>s.name===e||s.aliases?.includes(e))?.supported;if(!r)return n;let o=r.thinkingBudget!==void 0||r.showThoughts!==void 0,i=n.structuredOutputModes;if(r.structuredOutputModes!==void 0)i=[...r.structuredOutputModes];else if(r.structuredOutputs!==void 0){let s=(i??[]).filter(a=>a!=="native");i=r.structuredOutputs?["native",...s]:s}return{...n,hasThinkingBudget:r.thinkingBudget??n.hasThinkingBudget??!1,hasShowThoughts:r.showThoughts??n.hasShowThoughts??!1,structuredOutputs:i?i.includes("native"):r.structuredOutputs??n.structuredOutputs??!1,...i?{structuredOutputModes:i}:{},thinking:o?!!(r.thinkingBudget||r.showThoughts):n.thinking}},eM=(n,e,t,r,o)=>{let i={...e},s=String(e.model),a=WC(n,s),c=Pm(Im(n,Ds(n.id,s),o),s,r);if(t.thinkingTokenBudget&&t.thinkingTokenBudget!=="none"&&!c.thinking)throw new Error(`Thinking is not verified for profile ${n.id} and model ${s}; add an exact modelInfo override to opt in`);let l=i.response_format;if(l?.type==="json_schema"&&!c.structuredOutputs)throw new Error(`Structured output is not verified for profile ${n.id} and model ${s}: native JSON Schema is unsupported`);if(l?.type==="json_object"&&!c.structuredOutputModes?.includes("json_object"))throw new Error(`Structured output is not verified for profile ${n.id} and model ${s}: JSON object mode is unsupported`);return km(i,n.request,t),km(i,a?.request,t),i},KC=(n,e)=>{if(typeof e.apiURL=="string"&&e.apiURL.length>0)return e.apiURL;if(n.endpoint){let t={...n.endpoint.defaults};for(let[a,c]of Object.entries(n.endpoint.fields??{}))for(let l of c)e[l]!==void 0&&e[l]!==""&&(t[a]=e[l]);for(let a of n.endpoint.required)if(t[a]===void 0||t[a]==="")throw new Error(`${n.name} endpoint field ${a} is required`);let r=String(t[n.endpoint.hostField]),o=r.includes("://")?r.replace(/\/$/,""):`${n.endpoint.scheme??"https"}://${r}${n.endpoint.hostSuffix}`,i=n.endpoint.path;for(let[a,c]of Object.entries(t)){let l=String(c);n.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(n.endpoint.apiVersionField){let a=n.endpoint.apiVersionField,c=n.endpoint.normalizers?.[a],l=t[a];c&&l!==void 0&&l!==""&&s.searchParams.set(c,String(l))}return s.toString().replace(/\/$/,"")}if(n.baseURL)return n.baseURL;throw new Error(`${n.name} requires apiURL`)},Im=(n,e,t)=>{let r=n.capabilityGates?.structuredOutputs;if(!r)return e;let s=(String(t[r.option]??"").match(/\d{4}-\d{2}-\d{2}/)?.[0]??"")>=r.min,a=(e.structuredOutputModes??[]).filter(c=>c!=="native");return{...e,structuredOutputs:s,structuredOutputModes:s?["native",...a]:a}},JC=(n,e,t)=>{if(n.auth.required&&!e&&!t)throw new Error(`${n.name} API key not set`);return e??""},tM=n=>n?.map(e=>{let t=e;return t.config?{...e,modelConfig:{...t.config,...t.modelConfig}}:e}),YC=(n,e)=>!e||n.auth.type==="none"?{}:n.auth.type==="api-key-header"?{[n.auth.header??"api-key"]:e}:n.auth.type==="x-api-key"?{"x-api-key":e}:{Authorization:`Bearer ${e}`},Zo=class extends qr{profileSpec;profileApiURL;constructor(e){let t=Al(e.name);if(t.transport!=="openai-chat")throw new Error(`${t.id} is not an OpenAI Chat profile`);let r=JC(t,e.apiKey,e.credentialProvider),o=KC(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:hl,resolveAudioConfig:$s,createApi:xl}:void 0;super({apiKey:r||"local-no-key",credentialProvider:e.credentialProvider,credentialProfile:t.id,apiURL:o,config:i,options:e.options,modelInfo:e.modelInfo??[],models:tM(e.models),supportFor:c=>Pm(Im(t,Ds(t.id,c),e),c,e.modelInfo),reasoningContentMode:XI(t),realtime:a,chatReqUpdater:(c,l)=>eM(t,c,{...e.options??{},...l},e.modelInfo,e)}),this.setName(t.name),this.setHeaders(async()=>YC(t,r)),this.profileSpec=t,this.profileApiURL=o}async transcribe(e,t){let r=this.profileSpec.operations.transcribe;if(!r)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=r.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 zo({url:`${this.profileApiURL.replace(/\/$/,"")}${r.path}`,headers:await this.buildHeaders({},{operation:"transcribe",method:"POST",url:`${this.profileApiURL.replace(/\/$/,"")}${r.path}`}),audio:e.audio,fields:s,fetch:t?.fetch??o.fetch,abortSignal:t?.abortSignal??o.abortSignal})}async speak(e,t){let r=this.profileSpec.operations.speak;if(!r)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(r.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 r.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 lr({url:`${this.profileApiURL.replace(/\/$/,"")}${r.path}`,headers:await this.buildHeaders({},{operation:"speak",method:"POST",url:`${this.profileApiURL.replace(/\/$/,"")}${r.path}`}),body:c,format:i,transcript:e.text,fetch:t?.fetch??o.fetch,abortSignal:t?.abortSignal??o.abortSignal})}},ei=class extends zr{constructor(e){let t=Al(e.name);if(t.transport!=="openai-responses")throw new Error(`${t.id} is not an OpenAI Responses profile`);let r=JC(t,e.apiKey,e.credentialProvider),o=KC(t,e),i={model:t.defaults.model,...t.defaults.embedModel?{embedModel:t.defaults.embedModel}:{},stream:!0,...e.config};super({apiKey:r||"local-no-key",credentialProvider:e.credentialProvider,credentialProfile:t.id,apiURL:o,config:i,options:e.options,modelInfo:e.modelInfo??[],models:e.models,supportFor:s=>Pm(Im(t,Ds(t.id,s),e),s,e.modelInfo),responsesReqUpdater:s=>{let a={...s};if(km(a,t.request,e.options??{}),t.request?.reasoningObjectFields&&a.reasoning){let c=a.reasoning;a.reasoning=Object.fromEntries(t.request.reasoningObjectFields.filter(l=>c[l]!==void 0).map(l=>[l,c[l]]))}return a}}),this.setName(t.name),this.setHeaders(async()=>YC(t,r))}};var js=(r=>(r.RekaCore="reka-core",r.RekaFlash="reka-flash",r.RekaEdge="reka-edge",r))(js||{});var bl=[{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 ti=(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))(ti||{});var ni=[{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 Cl=[{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 nM={openai:{displayName:"OpenAI",defaultModel:"gpt-5-mini",defaultEmbedModel:"text-embedding-3-small",isDynamic:!1,modelInfo:Wo},"openai-responses":{displayName:"OpenAI Responses",defaultModel:"gpt-4o",defaultEmbedModel:"text-embedding-ada-002",isDynamic:!1,modelInfo:Ko},"azure-openai":{displayName:"Azure OpenAI",isDynamic:!0,modelInfo:[]},anthropic:{displayName:"Anthropic",defaultModel:"claude-3-7-sonnet-latest",isDynamic:!1,modelInfo:Nr},"google-gemini":{displayName:"Google Gemini",defaultModel:"gemini-2.5-flash",defaultEmbedModel:"gemini-embedding-2",isDynamic:!1,modelInfo:Ho},cohere:{displayName:"Cohere",defaultModel:"command-r-plus",isDynamic:!1,modelInfo:el},deepseek:{displayName:"DeepSeek",defaultModel:"deepseek-v4-flash",isDynamic:!1,modelInfo:Ps},"deepseek-responses":{displayName:"DeepSeek Responses",defaultModel:"deepseek-v4-flash",isDynamic:!1,modelInfo:Ps},mistral:{displayName:"Mistral AI",defaultModel:"mistral-small-latest",isDynamic:!1,modelInfo:rl},reka:{displayName:"Reka",defaultModel:"reka-core",isDynamic:!1,modelInfo:bl},grok:{displayName:"xAI Grok",defaultModel:"grok-4.6",isDynamic:!1,modelInfo:Cl},webllm:{displayName:"WebLLM",defaultModel:"Llama-3.2-3B-Instruct-q4f32_1-MLC",isDynamic:!1,modelInfo:ni}},rM={...Object.fromEntries(Us().map(n=>[n.id,{displayName:n.name,isDynamic:!0,modelInfo:[]}])),...nM},oM=n=>{let e={...n};return n.aliases&&(e.aliases=[...n.aliases]),n.supported&&(e.supported={...n.supported}),n.notSupported&&(e.notSupported={...n.notSupported}),n.audio&&(e.audio={...n.audio}),e},iM=n=>{let e=QC(n),t=n.name.toLowerCase();return{thinkingBudget:n.supported?.thinkingBudget??!1,showThoughts:n.supported?.showThoughts??!1,structuredOutputs:n.supported?.structuredOutputs??!1,temperature:!(n.notSupported?.temperature??!1),topP:!(n.notSupported?.topP??!1),audioInput:n.audio?.input??e==="audio",audioOutput:n.audio?.output??(e==="audio"&&!t.includes("whisper")&&!t.includes("transcription"))}},QC=n=>{let e=n.name.toLowerCase();return n.audio?.input||n.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"},sM=n=>{if(n===void 0)return;let e=Array.isArray(n)?n:[n];if(!e.includes("all"))return new Set(e)},aM=(n,e)=>!e||e.has(n.type)?!0:e.has("text")&&n.type==="code",vl=n=>{let e=typeof n?.promptTokenCostPer1M=="number",t=typeof n?.completionTokenCostPer1M=="number";return!e&&!t?Number.POSITIVE_INFINITY:(n?.promptTokenCostPer1M??0)+(n?.completionTokenCostPer1M??0)},cM=(n,e)=>{let t=vl(n)-vl(e);return t!==0?t:n.name.localeCompare(e.name)},lM=(n,e,t,r)=>{let o=oM(r),i=[e,t].filter(s=>s!==void 0);return{...o,provider:n,type:QC(r),isDefault:i.some(s=>r.name===s||(r.aliases?.includes(s)??!1)),capabilities:iM(r)}},XC=n=>{let e=sM(n?.type);return Object.entries(rM).map(([t,{displayName:r,defaultModel:o,defaultEmbedModel:i,isDynamic:s,modelInfo:a}])=>{let c=a.map(l=>lM(t,o,i,l)).filter(l=>aM(l,e)).sort(cM);return{name:t,displayName:r,...o!==void 0?{defaultModel:o}:void 0,...i!==void 0?{defaultEmbedModel:i}:void 0,isDynamic:s,models:c}}).sort((t,r)=>{let o=vl(t.models[0])-vl(r.models[0]);return o!==0?o:t.displayName.localeCompare(r.displayName)})};var uM=n=>`wss://generativelanguage.googleapis.com/ws/google.ai.generativelanguage.${n.live?.enableAffectiveDialog||n.live?.proactiveAudio?"v1alpha":"v1beta"}.GenerativeService.BidiGenerateContent`,Em=()=>structuredClone({...Qe(),model:"gemini-2.5-flash-native-audio-preview-12-2025",embedModel:"text-embedding-005",audio:Ss(),stream:!1}),Om=n=>n==="gemini-2.5-flash-native-audio-preview-12-2025"||n==="gemini-3.1-flash-live-preview"||n.includes("native-audio")||n.includes("-live-")||n.startsWith("gemini-live-"),Bs=(n,e)=>{let t=bt(n,e);return qt(t)?bt(Ss(),t):t},qs=(n,e,t)=>Om(n)&&qt(Bs(e,t)),Fm=n=>{if(!("inlineData"in n))return;let{mimeType:e}=n.inlineData;if(!e.startsWith("audio/"))return;let t=At(e);if(t!=="pcm"&&t!=="pcm16")throw new Error(`Gemini Live audio output requires PCM audio input, received ${e}`)},Rl=n=>{if(!("inlineData"in n)||!n.inlineData.mimeType.startsWith("audio/"))return;let e=At(n.inlineData.mimeType),t=n.inlineData.mimeType.match(/rate=(\d+)/)?.[1];return{data:n.inlineData.data,mimeType:n.inlineData.mimeType,format:e==="pcm"?"pcm16":e,sampleRate:t?Number.parseInt(t,10):void 0,channels:1,isDelta:n.isDelta===!0}},wl=n=>({name:"gemini-live-audio",localCall:async(e,t)=>t?hM(n):await ZC(n)}),dM=({model:n,request:e,audio:t})=>{let r=t.output,o=t.live,i=typeof r?.voice=="string"?{voiceConfig:{prebuiltVoiceConfig:{voiceName:r.voice}}}:void 0,s={temperature:e.generationConfig.temperature,topP:e.generationConfig.topP,topK:e.generationConfig.topK,frequencyPenalty:e.generationConfig.frequencyPenalty,maxOutputTokens:e.generationConfig.maxOutputTokens,thinkingConfig:e.generationConfig.thinkingConfig,responseModalities:["AUDIO"],...i?{speechConfig:i}:{}};return{setup:{model:`models/${n}`,generationConfig:s,...e.systemInstruction?{systemInstruction:e.systemInstruction}:{},...e.tools?{tools:e.tools}:{},...e.toolConfig?{toolConfig:e.toolConfig}:{},...r?.includeTranscript!==!1?{outputAudioTranscription:{}}:{},...o?.enableAffectiveDialog?{enableAffectiveDialog:!0}:{},...o?.proactiveAudio?{proactivity:{proactiveAudio:!0}}:{}}}},pM=n=>{let e=[],t=[];for(let r of n){let o=[];for(let i of r.parts){if("inlineData"in i&&i.inlineData.mimeType.startsWith("audio/")){Fm(i),t.push({data:i.inlineData.data,mimeType:i.inlineData.mimeType});continue}o.push(i)}o.length>0&&e.push({...r,parts:o})}return{clientContents:e,audioParts:t}},mM=(n,e)=>{let{clientContents:t,audioParts:r}=pM(e.contents);t.length>0&&n.send(JSON.stringify({clientContent:{turns:t,turnComplete:r.length===0}}));for(let o of r)n.send(JSON.stringify({realtimeInput:{audio:o}}));r.length>0&&n.send(JSON.stringify({realtimeInput:{audioStreamEnd:!0}}))},Gs=({audioData:n,audio:e,transcript:t,text:r,functionCalls:o,usageMetadata:i,isDelta:s})=>{let a=e.output,c=a?.mimeType??Lt(a?.format,a?.sampleRate,"audio/pcm;rate=24000"),l=[];(r||t)&&l.push({text:r??t??""}),n&&l.push({inlineData:{mimeType:c,data:n},...s?{isDelta:!0}:{}});for(let u of o??[])l.push({functionCall:{name:u.name,args:u.args}});return{candidates:[{content:{role:"model",parts:l},finishReason:"STOP",citationMetadata:{citations:[]}}],usageMetadata:i??{promptTokenCount:0,candidatesTokenCount:0,totalTokenCount:0,thoughtsTokenCount:0}}},gM=n=>{if(n)return{promptTokenCount:n.promptTokenCount??0,candidatesTokenCount:n.candidatesTokenCount??n.responseTokenCount??0,totalTokenCount:n.totalTokenCount??0,thoughtsTokenCount:n.thoughtsTokenCount??0,cachedContentTokenCount:n.cachedContentTokenCount}},fM=n=>{let e=n?.data??n;return typeof e=="string"?JSON.parse(e):e},Mm=(n,e,t)=>{if(n.addEventListener){n.addEventListener(e,t);return}n[`on${e}`]=t},ZC=async(n,e)=>{let t=globalThis.WebSocket;if(!t)throw new Error("Gemini Live audio requires globalThis.WebSocket");let r=n.audio,o=r.live?.turnTimeoutMs??3e4,i={audioChunks:[],textChunks:[],outputTranscripts:[],functionCalls:[]},s=new t(`${uM(r)}?key=${encodeURIComponent(n.apiKey)}`);return await new Promise((a,c)=>{let l=!1,u=m=>{if(!l){l=!0,clearTimeout(p);try{s.close()}catch{}a(m)}},d=m=>{if(!l){l=!0,clearTimeout(p);try{s.close()}catch{}c(m instanceof Error?m:new Error(String(m)))}},p=setTimeout(()=>{d(new Error(`Gemini Live audio turn timed out after ${o}ms`))},o);Mm(s,"open",()=>{s.send(JSON.stringify(dM(n)))}),Mm(s,"error",m=>{d(m?.error??m?.message??"Gemini Live WebSocket error")}),Mm(s,"message",m=>{try{let g=fM(m);if(g.setupComplete){mM(s,n.request);return}g.usageMetadata&&(i.usageMetadata=gM(g.usageMetadata));let h=g.toolCall?.functionCalls;if(Array.isArray(h)){for(let b of h)if(typeof b?.name=="string"){let A={name:b.name,args:b.args??{}};i.functionCalls.push(A),e?.(Gs({audio:r,functionCalls:[A],isDelta:!0}))}}let x=g.serverContent;if(!x)return;let f=x.outputTranscription?.text;typeof f=="string"&&(i.outputTranscripts.push(f),e?.(Gs({audio:r,transcript:f,isDelta:!0})));let y=x.modelTurn?.parts;if(Array.isArray(y))for(let b of y){if(typeof b.text=="string"){i.textChunks.push(b.text),e?.(Gs({audio:r,text:b.text,isDelta:!0}));continue}let A=b.inlineData??b.inline_data,v=A?.mimeType??A?.mime_type;A?.data&&typeof v=="string"&&v.startsWith("audio/")&&(i.audioChunks.push(A.data),e?.(Gs({audio:r,audioData:A.data,isDelta:!0})))}if(x.turnComplete){let b=i.outputTranscripts.join(""),A=i.textChunks.join("");u(Gs({audio:r,audioData:$r(i.audioChunks),transcript:b||void 0,text:A||void 0,functionCalls:i.functionCalls,usageMetadata:i.usageMetadata}))}}catch(g){d(g)}})})},hM=n=>new ReadableStream({start(e){ZC(n,t=>e.enqueue(t)).then(t=>{e.enqueue(t),e.close()}).catch(t=>e.error(t))}});var Vr=n=>n.includes("gemini-3"),ev=n=>n.includes("gemini-3")&&n.includes("pro"),tv=n=>n==="gemini-3.7-flash"||n==="gemini-3.6-flash"||n==="gemini-3.5-flash-lite",xM=(n,e)=>e?"v1beta1":"v1",Sl=n=>{if(!n||typeof n!="object")return n;let e={...n},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(([r,o])=>[r,Sl(o)]))),e.items&&(e.items=Sl(e.items)),e},yM=(n,e,t)=>{for(let r=e-1;r>=0;r--){let o=n[r];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},_m=(n,e,t)=>{let r=t.protocolResult?.value,o=[{functionResponse:{name:yM(n,e,t.functionId),response:{result:t.result,...r?.structuredContent?{structuredContent:r.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(`
|
|
1266
|
-
`)});return o},nv=[{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"}],Nm=()=>structuredClone({model:"gemini-2.5-flash",embedModel:"text-embedding-005",safetySettings:nv,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...Qe()}),rv=()=>structuredClone({model:"gemini-2.0-flash",embedModel:"text-embedding-005",safetySettings:nv,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...ar()}),Lm=class{constructor(e,t,r,o,i,s,a){this.config=e;this.vertexConfig=t;this.endpointId=r;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");let c=this.config.model;if(Vr(c)){if(this.config.thinking?.thinkingTokenBudget!==void 0&&typeof this.config.thinking.thinkingTokenBudget=="number")throw new Error(`Gemini 3 models (${c}) do not support numeric thinkingTokenBudget. Use thinkingLevel ('low', 'medium', 'high') instead, or pass thinkingTokenBudget as a string level via options.`);if(ev(c)&&this.config.thinking?.thinkingLevel){let l=this.config.thinking.thinkingLevel;if(l!=="low"&&l!=="high")throw new Error(`Gemini 3 Pro (${c}) only supports thinkingLevel 'low' or 'high', got '${l}'. Use 'low' for less thinking or 'high' for more thinking.`)}}}tokensUsed;models;setModels(e){this.models=e}getEffectiveMappings(e){let t=this.models?.find(r=>r.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 r=e.model??"gemini-2.5-flash",o=typeof this.apiKey=="function"?await this.apiKey():this.apiKey,i=this.isVertex?`${this.getVertexApiURL(r,t?.beta)}/models/${r}:generateContent`:`https://generativelanguage.googleapis.com/v1beta/models/${r}:generateContent${o?`?key=${o}`:""}`,s={contents:[{role:"user",parts:[{inlineData:{mimeType:e.audio.mimeType??Lt(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}`}:{}},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 r=e.model??"gemini-2.5-flash-preview-tts",o=typeof this.apiKey=="function"?await this.apiKey():this.apiKey,i=this.isVertex?`${this.getVertexApiURL(r,t?.beta)}/models/${r}:generateContent`:`https://generativelanguage.googleapis.com/v1beta/models/${r}:generateContent${o?`?key=${o}`:""}`,s=typeof e.voice=="object"?e.voice.id:e.voice??"Kore";return await lr({url:i,headers:{...this.isVertex&&o?{Authorization:`Bearer ${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://${Rs(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 r=[];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?Sl(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}});r.push({function_declarations:l})}if(this.options?.codeExecution&&r.push({code_execution:{}}),this.options?.googleSearchRetrieval&&r.push({google_search_retrieval:{dynamic_retrieval_config:this.options.googleSearchRetrieval}}),this.options?.googleSearch&&r.push({google_search:{}}),this.options?.googleMaps){let l=this.options.googleMaps,u=l?.enableWidget!==void 0?{enableWidget:l.enableWidget}:{};r.push({google_maps:u})}this.options?.urlContext&&r.push({url_context:{}}),r.length===0&&(r=void 0);let o,i=Array.isArray(r)?r.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=!!(r&&r.length>0)||!!o,c=!!t?.contextCache&&a;return{tools:r,toolConfig:o,cacheableTools:s||c}}createChatReq=async(e,t)=>{let r=e.model,o=e.modelConfig?.stream??this.config.stream,i=Bs(this.config.audio,e.modelConfig?.audio),s=qs(r,this.config.audio,e.modelConfig?.audio);if(!e.chatPrompt||e.chatPrompt.length===0)throw new Error("Chat prompt is empty");let a;if(s){if(this.isVertex)throw new Error("Gemini Live audio currently supports Google AI API-key WebSocket sessions only");a={name:"gemini-live-audio"}}else this.endpointId?a={name:o?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:a={name:o?`/models/${r}:streamGenerateContent?alt=sse`:`/models/${r}:generateContent`};if(!s&&this.isVertex&&(a.url=this.getVertexApiURL(r,t?.beta)),!s&&!this.isVertex){let A=o?"&":"?",v=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;v&&(a.name+=`${A}key=${v}`)}let c=e.chatPrompt.filter(A=>A.role==="system").map(A=>A.content),l=c.length>0?{role:"user",parts:[{text:c.join(" ")}]}:void 0,u=[],d=e.chatPrompt.filter(A=>A.role!=="system");for(let A=0;A<d.length;A++){let v=d[A];switch(v.role){case"user":{let w=Array.isArray(v.content)?v.content.map((N,k)=>{switch(N.type){case"text":return{text:N.text};case"image":return{inlineData:{mimeType:N.mimeType,data:N.image}};case"audio":return{inlineData:{mimeType:N.mimeType??Lt(N.format,N.sampleRate),data:N.data}};case"file":return"fileUri"in N?{fileData:{mimeType:N.mimeType,fileUri:N.fileUri}}:{inlineData:{mimeType:N.mimeType,data:N.data}};default:throw new Error(`Chat prompt content type not supported (index: ${k})`)}}):[{text:v.content}];u.push({role:"user",parts:w});break}case"assistant":{let w=[],N=v.thoughtBlocks,k=v.functionCalls&&v.functionCalls.length>0,E=N?.[0],F=N?.map(P=>P.data).join("")??"",L=E?.signature;if(F&&w.push({...k?{}:{thought:!0},text:F,...L&&!k?{thought_signature:L}:{}}),v.functionCalls){let P=v.functionCalls.map(($,S)=>{let z;if(typeof $.function.params=="string"){let H=$.function.params;if(H.trim().length===0)z={};else try{z=JSON.parse(H)}catch{throw new Error(`Failed to parse function params JSON: ${H}`)}}else z=$.function.params;let _={functionCall:{name:$.function.name,args:z}};return L&&S===0&&(_.thought_signature=L),_});w.push(...P)}let D=v.audio?.transcript;if((v.content||D)&&w.push({text:v.content??D??""}),w.length===0)throw new Error("Assistant content is empty");u.push({role:"model",parts:w});break}case"function":{let w=[],N=v,k=A;for(;;){if(!("functionId"in N))throw new Error(`Chat prompt functionId is empty (index: ${k})`);if(w.push(..._m(d,k,N)),k+1<d.length&&d[k+1].role==="function")k++,N=d[k];else break}A=k,u.push({role:"user",parts:w});break}default:throw new Error(`Invalid role: ${JSON.stringify(v)} (index: ${A})`)}}let{tools:p,toolConfig:m}=this.buildToolState(e,t),g={};if(this.config.thinking?.includeThoughts&&(g.includeThoughts=!0),this.config.thinking?.thinkingTokenBudget&&(g.thinkingBudget=this.config.thinking.thinkingTokenBudget),this.config.thinking?.thinkingLevel&&Vr(r)&&(g.thinkingLevel=this.config.thinking.thinkingLevel),t?.thinkingTokenBudget){let A=this.getEffectiveMappings(r),v=A.thinkingTokenBudgetLevels;if(Vr(r)){let N=ev(r),k=A.thinkingLevelMapping;if(t.thinkingTokenBudget==="none")g.thinkingLevel=k?.minimal??"minimal";else{let E=t.thinkingTokenBudget,F=k?.[E];F||(F=E==="highest"?"high":E),g.thinkingLevel=F}if(N&&g.thinkingLevel){let E=g.thinkingLevel;E!=="low"&&E!=="high"&&(g.thinkingLevel=E==="minimal"?"low":"high")}}else switch(t.thinkingTokenBudget){case"none":g.thinkingBudget=0,g.includeThoughts=!1,delete g.thinkingLevel;break;case"minimal":g.thinkingBudget=v?.minimal??200;break;case"low":g.thinkingBudget=v?.low??800;break;case"medium":g.thinkingBudget=v?.medium??5e3;break;case"high":g.thinkingBudget=v?.high??1e4;break;case"highest":g.thinkingBudget=v?.highest??24500;break}}g.thinkingLevel&&delete g.thinkingBudget,Vr(r)||delete g.thinkingLevel,Vr(r)&&delete g.thinkingBudget;let h=e.modelConfig?.maxTokens??this.config.maxTokens;if(g.thinkingLevel&&h!==void 0)throw new Error("Cannot set maxTokens when using thinkingLevel with Gemini models. When thinking is enabled, the model manages output tokens automatically. Remove the maxTokens setting or disable thinking.");t?.showThoughts!==void 0&&t?.thinkingTokenBudget!=="none"&&(g.includeThoughts=t.showThoughts);let x=tv(r),f={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,...x?{}:{temperature:e.modelConfig?.temperature??this.config.temperature},...!x&&e.modelConfig?.topP!==void 0?{topP:e.modelConfig.topP}:{},...x?{}:{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(g).length>0?{thinkingConfig:g}:{}};if(!x&&Vr(r)&&(f.temperature===void 0||f.temperature<1)&&(f.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 A=e.responseFormat.schema.schema||e.responseFormat.schema;f.responseMimeType="application/json",f.responseJsonSchema=Sl(A)}else f.responseMimeType="application/json";else this.config.responseFormat&&this.config.responseFormat==="json_object"&&(f.responseMimeType="application/json");let y=this.config.safetySettings,b={contents:u,tools:p,toolConfig:m,systemInstruction:l,generationConfig:f,safetySettings:y};if(s){let A=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;if(!A)throw new Error("GoogleGemini AI API key not set");a=wl({model:r,request:b,apiKey:A,audio:i})}return[a,b]};createEmbedReq=async(e,t)=>{let r=e.embedModel;if(!r)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let o,i;if(this.isVertex)this.endpointId?o={name:`/${this.endpointId}:predict`}:o={name:`/models/${r}:predict`},o.url=this.getVertexApiURL(r,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}};else{let s=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;o={name:`/models/${r}:batchEmbedContents${s?`?key=${s}`:""}`},i={requests:e.texts.map(a=>({model:`models/${r}`,content:{parts:[{text:a}]},outputDimensionality:this.config.dimensions,...this.config.embedType&&{taskType:this.config.embedType}}))}}return[o,i]};createChatResp=e=>{let t,r=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 Ve("Content was blocked due to safety settings",void 0,void 0);case"RECITATION":throw new Ve("Content was blocked due to recitation policy",void 0,void 0);case"MALFORMED_FUNCTION_CALL":throw new Ve("Function call was malformed and blocked",void 0,void 0);case"UNEXPECTED_TOOL_CALL":throw new Ve("Unexpected tool call",void 0,void 0);case"FINISH_REASON_UNSPECIFIED":throw new Ve("Finish reason unspecified",void 0,void 0);case"BLOCKLIST":throw new Ve("Content was blocked due to blocklist",void 0,void 0);case"PROHIBITED_CONTENT":throw new Ve("Content was blocked due to prohibited content",void 0,void 0);case"SPII":throw new Ve("Content was blocked due to SPII",void 0,void 0);case"OTHER":throw new Ve("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:ze(),type:"function",function:{name:l.functionCall.name,params:l.functionCall.args}}]}let u=Rl(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;this.tokensUsed={totalTokens:e.usageMetadata.totalTokenCount,promptTokens:e.usageMetadata.promptTokenCount-i,completionTokens:e.usageMetadata.candidatesTokenCount,thoughtsTokens:e.usageMetadata.thoughtsTokenCount,...i>0?{cacheReadTokens:i}:{}}}let o={results:r,...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(r=>r.embeddings.values):t=e.embeddings.map(r=>r.values),{embeddings:t}};supportsContextCache=e=>{let t=e;return wC.some(r=>t.includes(r)||r.includes(t))};buildCacheCreateOp=(e,t)=>{let r=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(r):`models/${r}`,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;if(u)p=`/${u.parent}/cachedContents`;else{let m=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;p=`/cachedContents${m?`?key=${m}`:""}`}return{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:r,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(r||o)return{functions:[{name:"__gemini_tool_state__",description:JSON.stringify({tools:r,toolConfig:o})}]}};buildCacheUpdateTTLOp=(e,t)=>{let r={ttl:`${t}s`},o=new URLSearchParams({updateMask:"ttl"});if(!this.isVertex&&this.apiKey){let a=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;o.set("key",a)}let 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:r,parseResponse:a=>{let c=a;if(c?.name)return{name:c.name,expiresAt:c.expireTime,tokenCount:c.usageMetadata?.totalTokenCount}}}};buildCacheDeleteOp=e=>{let t=`/${e}`;if(!this.isVertex&&this.apiKey){let o=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;t+=`?key=${o}`}let r=this.getVertexCacheContext();return{type:"delete",apiConfig:{name:t,headers:{"Content-Type":"application/json"},...r?{url:r.baseUrl}:{}},request:{},parseResponse:()=>{}}};prepareCachedChatReq=async(e,t,r)=>{let o=e.model,i=e.modelConfig?.stream??this.config.stream,{tools:s,toolConfig:a,cacheableTools:c}=this.buildToolState(e,t),{dynamicContents:l,dynamicSystemInstruction:u}=this.extractDynamicContent(e.chatPrompt),d;if(this.endpointId?d={name:i?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:d={name:i?`/models/${o}:streamGenerateContent?alt=sse`:`/models/${o}:generateContent`},!this.isVertex){let x=i?"&":"?",f=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;f&&(d.name+=`${x}key=${f}`)}let p=tv(o),m={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,...p?{}:{temperature:e.modelConfig?.temperature??this.config.temperature},...!p&&e.modelConfig?.topP!==void 0?{topP:e.modelConfig.topP}:{},...p?{}:{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"};!p&&Vr(o)&&(m.temperature===void 0||m.temperature<1)&&(m.temperature=1);let g=this.config.safetySettings,h={contents:l,cachedContent:r,generationConfig:m,safetySettings:g};return c||(s&&s.length>0&&(h.tools=s),a&&(h.toolConfig=a)),u&&(h.systemInstruction=u),{apiConfig:d,request:h}};extractCacheableContent(e){let t,r=[],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??Lt(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&&r.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&&r.push({role:"model",parts:a})}else s.role==="function"&&r.push({role:"user",parts:_m(e,i,s)})}return{systemInstruction:t,contents:r}}extractDynamicContent(e){let r=[],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??Lt(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&&r.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&&r.push({role:"model",parts:a})}else s.role==="function"&&r.push({role:"user",parts:_m(e,i,s)})}return{dynamicContents:r,dynamicSystemInstruction:void 0}}},ri=class n extends wt{static create(e){return new n(e)}constructor({apiKey:e,credentialProvider:t,projectId:r,region:o,endpointId:i,config:s,options:a,models:c,modelInfo:l}){let u=r!==void 0&&o!==void 0?{projectId:r,region:o}:void 0,d={...Nm(),...s},p,m,g;if(u){let{projectId:y,region:b}=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 A;i?A="endpoints":A="publishers/google";let v=Rs(b);g=(w,N)=>`https://${v}/${xM(w,N)}/projects/${y}/locations/${b}/${A}`,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()=>({})}let h=new Lm(d,u,i,e,t,a,g);l=[...Ho,...l??[]];let x=y=>{let b=qs(y,{output:{enabled:!0}}),A=Bt({model:y,modelInfo:l,models:c}),v=A?.supported?.structuredOutputs??!0,w=A?.supported?.structuredOutputModes??(v?["native","function"]:["function"]);return{functions:!0,streaming:!0,hasThinkingBudget:A?.supported?.thinkingBudget??!1,hasShowThoughts:A?.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:b?["pcm16","pcm"]:["wav","mp3","aac","ogg"],maxDuration:9.5*60,output:{supported:b,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:A?.supported?.thinkingBudget??!1,multiTurn:!0}},f=c?.map(y=>{let b=y,A=b?.config;if(!A)return y;let v={};A.maxTokens!==void 0&&(v.maxTokens=A.maxTokens),A.temperature!==void 0&&(v.temperature=A.temperature),A.topP!==void 0&&(v.topP=A.topP),A.topK!==void 0&&(v.topK=A.topK),A.presencePenalty!==void 0&&(v.presencePenalty=A.presencePenalty),A.frequencyPenalty!==void 0&&(v.frequencyPenalty=A.frequencyPenalty),A.stopSequences!==void 0&&(v.stopSequences=A.stopSequences),A.endSequences!==void 0&&(v.endSequences=A.endSequences),A.stream!==void 0&&(v.stream=A.stream),A.n!==void 0&&(v.n=A.n);let w={...b};Object.keys(v).length>0&&(w.modelConfig={...b.modelConfig??{},...v});let N=A.thinking?.thinkingTokenBudget;if(typeof N=="number"){let k=d.thinkingTokenBudgetLevels,E=[["minimal",k?.minimal??200],["low",k?.low??800],["medium",k?.medium??5e3],["high",k?.high??1e4],["highest",k?.highest??24500]],F="minimal",L=Number.POSITIVE_INFINITY;for(let[D,P]of E){let $=Math.abs(N-P);$<L&&(L=$,F=D)}w.thinkingTokenBudget=F}return A.thinking?.includeThoughts!==void 0&&(w.showThoughts=!!A.thinking.includeThoughts),A.thinkingLevelMapping&&(w.thinkingLevelMapping=A.thinkingLevelMapping),A.thinkingTokenBudgetLevels&&(w.thinkingTokenBudgetLevels=A.thinkingTokenBudgetLevels),w});f?h.setModels(f):c&&h.setModels(c),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 kl=class{constructor(e={}){this.config=e;this.config.id=this.config.id??ze()}metrics={latency:{chat:{mean:0,p95:0,p99:0,samples:[]},embed:{mean:0,p95:0,p99:0,samples:[]}},errors:{chat:{count:0,rate:0,total:0},embed:{count:0,rate:0,total:0}}};getLastUsedChatModel(){return this.config.modelInfo?.name??"mock-model"}getLastUsedEmbedModel(){return this.config.embedModelInfo?.name??"mock-embed-model"}getLastUsedModelConfig(){return this.config.modelInfo?{maxTokens:this.config.modelInfo.maxTokens,temperature:.7,stream:this.config.features?.streaming??!1}:void 0}getName(){return this.config.name??"mock-ai-service"}getId(){return this.config.id??"mock-ai-service-id"}getFeatures(e){let t=this.config.features?.media;return{functions:this.config.features?.functions??!1,streaming:this.config.features?.streaming??!1,structuredOutputs:this.config.features?.structuredOutputs,...this.config.features?.structuredOutputModes?{structuredOutputModes:this.config.features.structuredOutputModes}:{},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(r=>setTimeout(r,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock chat error");return this.updateMetrics("chat"),typeof this.config.chatResponse=="function"?await this.config.chatResponse(e):this.config.chatResponse??{results:[{index:0,content:"Mock response",finishReason:"stop"}],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:10,completionTokens:5,totalTokens:15}}}}async embed(e,t){if(this.config.latencyMs&&await new Promise(r=>setTimeout(r,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 r=this.metrics.latency[e].samples;if(this.metrics.latency[e].mean=r.reduce((o,i)=>o+i,0)/r.length,r.length>0){let o=[...r].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 Tl=class n{options;lastUsedService;services=new Map;constructor(e){if(e.length===0)throw new Error("No AI services provided.");for(let[t,r]of e.entries())if("key"in r){if(this.services.has(r.key))throw new Error(`Duplicate model key: ${r.key}`);let{service:i,description:s,isInternal:a}=r;this.services.set(r.key,{service:i,description:s,isInternal:a})}else{let i=r.getModelList();if(!i)throw new Error(`Service ${t} \`${r.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} \`${r.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:r,model:s.model});else if("embedModel"in s&&s.embedModel)this.services.set(s.key,{description:s.description,service:r,embedModel:s.embedModel});else throw new Error(`Key ${s.key} in model list for service ${t} \`${r.getName()}\` is missing a model or embedModel property.`)}}}static create(e){return new n(e)}getLastUsedChatModel(){return this.lastUsedService?.getLastUsedChatModel()}getLastUsedEmbedModel(){return this.lastUsedService?.getLastUsedEmbedModel()}getLastUsedModelConfig(){return this.lastUsedService?.getLastUsedModelConfig()}async chat(e,t){let r=e.model;if(!r)throw new Error("Model key must be specified for multi-service");let o=this.services.get(r);if(!o)throw new Error(`No service found for model key: ${r}`);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:r,...e},t)}async embed(e,t){let r=e.embedModel;if(!r)throw new Error("Embed model key must be specified for multi-service");let o=this.services.get(r);if(!o)throw new Error(`No service found for embed model key: ${r}`);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:r,...e},t)}async transcribe(e,t){let r=e.model;if(!r){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(r);if(!o)throw new Error(`No service found for transcription model key: ${r}`);return this.lastUsedService=o.service,await o.service.transcribe(e,t)}async speak(e,t){let r=e.model;if(!r){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(r);if(!o)throw new Error(`No service found for speech model key: ${r}`);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 Pl(n,e,t={}){if(typeof n=="string")return[{type:"text",text:n}];if(!Array.isArray(n))return[{type:"text",text:String(n)}];let r=e.getFeatures(),o=[];for(let i of n)try{switch(i.type){case"text":o.push({type:"text",text:i.text});break;case"image":if(r.media.images.supported)i.altText?o.push({type:"text",text:`[Image: ${i.altText}]`}):o.push({type:"text",text:"[Image content]"});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 Qt(s,"image","vision analysis")}else switch(t.fallbackBehavior){case"error":throw new ht("Images",e.getName(),!1);case"skip":continue;default:o.push({type:"text",text:"[Image content not supported by this provider]"})}break;case"audio":if(r.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 Qt(s,"audio","transcription")}else switch(t.fallbackBehavior){case"error":throw new ht("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(r.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 Qt(s,"file","text extraction")}else switch(t.fallbackBehavior){case"error":throw new ht("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(r.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 Qt(s,"url","content fetching")}else switch(t.fallbackBehavior){case"error":throw new ht("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 ht||s instanceof Qt?s:new Qt(s,i.type||"unknown","content processing")}return o}function ov(n){let e=!1,t=!1,r=!1,o=!1;for(let i of n)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":r=!0;break;case"url":o=!0;break}return{hasImages:e,hasAudio:t,hasFiles:r,hasUrls:o}}var Il=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 r=await this.selectProviderWithDegradation(e,t.routingOptions||{}),o=await this.preprocessRequest(e,r.provider,t.processingOptions);try{return{response:await r.provider.chat(o,t),routing:r}}catch(i){if(i instanceof ht&&t.fallbackProviders?.length)return await this.tryFallbackProviders(e,t.fallbackProviders,t);throw i}}async preprocessRequest(e,t,r){let o={...r,fallbackBehavior:r?.fallbackBehavior||"degrade",imageToText:r?.imageToText||this.processingServices.imageToText,audioToText:r?.audioToText||this.processingServices.audioToText,fileToText:r?.fileToText||this.processingServices.fileToText,urlToText:r?.urlToText||this.processingServices.urlToText},i=[];for(let s of e.chatPrompt)if(s.role==="user"&&Array.isArray(s.content)){let a=await Pl(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=>({type:"text",text:l.text}))})}else i.push(s);return{...e,chatPrompt:i}}async selectProviderWithDegradation(e,t){let r=Dr(e),o=[],i=[],s=[];try{let a=Xc(e,this.providers,{requireExactMatch:t.requireExactMatch??this.config.capability.requireExactMatch,allowDegradation:t.allowDegradation??this.config.capability.allowDegradation}),c=a.getFeatures();return r.hasImages&&!c.media.images.supported&&(i.push("Images will be converted to text descriptions"),o.push("Image-to-text conversion")),r.hasAudio&&!c.media.audio.supported&&(i.push("Audio will be transcribed to text"),o.push("Audio-to-text transcription")),r.hasFiles&&!c.media.files.supported&&(i.push("File content will be extracted to text"),o.push("File-to-text extraction")),r.hasUrls&&!c.media.urls.supported&&(i.push("URL content will be pre-fetched"),o.push("URL content fetching")),r.requiresStreaming&&!c.streaming&&s.push("Streaming not supported - will use non-streaming mode"),r.requiresCaching&&!c.caching.supported&&s.push("Content caching not supported"),{provider:a,processingApplied:o,degradations:i,warnings:s}}catch(a){throw new Error(`Provider selection failed: ${a instanceof Error?a.message:"Unknown error"}`)}}async tryFallbackProviders(e,t,r){for(let o of t)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,r),routing:i}}catch{}throw new Error("All fallback providers failed")}async getRoutingRecommendation(e){return await this.selectProviderWithDegradation(e,{})}async validateRequest(e){let t=Dr(e),r=[],o=[];try{let i=await this.selectProviderWithDegradation(e,{});return i.degradations.length>0&&(r.push(...i.degradations),o.push("Consider using a provider that natively supports all media types")),i.warnings.length>0&&r.push(...i.warnings),t.hasImages&&this.processingServices.imageToText===void 0&&(this.providers.some(a=>a.getFeatures().media.images.supported)||(r.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)||(r.push("No audio processing service available and no providers support audio"),o.push("Add audioToText processing service or use audio-capable provider"))),{canHandle:r.length===0||i.degradations.length>0,issues:r,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 r=t.getFeatures(),o=t.getName();r.functions&&(e.Functions=e.Functions||[],e.Functions.push(o)),r.streaming&&(e.Streaming=e.Streaming||[],e.Streaming.push(o)),r.media.images.supported&&(e.Images=e.Images||[],e.Images.push(o)),r.media.audio.supported&&(e.Audio=e.Audio||[],e.Audio.push(o)),r.media.files.supported&&(e.Files=e.Files||[],e.Files.push(o)),r.media.urls.supported&&(e.URLs=e.URLs||[],e.URLs.push(o)),r.caching.supported&&(e.Caching=e.Caching||[],e.Caching.push(o))}return{totalProviders:this.providers.length,capabilityMatrix:e,recommendedProvider:this.providers[0]?.getName()||"None"}}};var Um=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...Qe()}),iv=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...ar()}),$m=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,r=this.config.supportsFunctions??!1,o=e.chatPrompt.map(u=>{if(u.role==="function")return{role:"tool",tool_call_id:u.functionId,content:typeof u.result=="string"?u.result:JSON.stringify(u.result)};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(`
|
|
1266
|
+
`)});return o},nv=[{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"}],Nm=()=>structuredClone({model:"gemini-2.5-flash",embedModel:"text-embedding-005",safetySettings:nv,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...Qe()}),rv=()=>structuredClone({model:"gemini-2.0-flash",embedModel:"text-embedding-005",safetySettings:nv,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...ar()}),Lm=class{constructor(e,t,r,o,i,s,a){this.config=e;this.vertexConfig=t;this.endpointId=r;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");let c=this.config.model;if(Vr(c)){if(this.config.thinking?.thinkingTokenBudget!==void 0&&typeof this.config.thinking.thinkingTokenBudget=="number")throw new Error(`Gemini 3 models (${c}) do not support numeric thinkingTokenBudget. Use thinkingLevel ('low', 'medium', 'high') instead, or pass thinkingTokenBudget as a string level via options.`);if(ev(c)&&this.config.thinking?.thinkingLevel){let l=this.config.thinking.thinkingLevel;if(l!=="low"&&l!=="high")throw new Error(`Gemini 3 Pro (${c}) only supports thinkingLevel 'low' or 'high', got '${l}'. Use 'low' for less thinking or 'high' for more thinking.`)}}}tokensUsed;models;setModels(e){this.models=e}getEffectiveMappings(e){let t=this.models?.find(r=>r.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 r=e.model??"gemini-2.5-flash",o=typeof this.apiKey=="function"?await this.apiKey():this.apiKey,i=this.isVertex?`${this.getVertexApiURL(r,t?.beta)}/models/${r}:generateContent`:`https://generativelanguage.googleapis.com/v1beta/models/${r}:generateContent${o?`?key=${o}`:""}`,s={contents:[{role:"user",parts:[{inlineData:{mimeType:e.audio.mimeType??Lt(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}`}:{}},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 r=e.model??"gemini-2.5-flash-preview-tts",o=typeof this.apiKey=="function"?await this.apiKey():this.apiKey,i=this.isVertex?`${this.getVertexApiURL(r,t?.beta)}/models/${r}:generateContent`:`https://generativelanguage.googleapis.com/v1beta/models/${r}:generateContent${o?`?key=${o}`:""}`,s=typeof e.voice=="object"?e.voice.id:e.voice??"Kore";return await lr({url:i,headers:{...this.isVertex&&o?{Authorization:`Bearer ${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://${Rs(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 r=[];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?Sl(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}});r.push({function_declarations:l})}if(this.options?.codeExecution&&r.push({code_execution:{}}),this.options?.googleSearchRetrieval&&r.push({google_search_retrieval:{dynamic_retrieval_config:this.options.googleSearchRetrieval}}),this.options?.googleSearch&&r.push({google_search:{}}),this.options?.googleMaps){let l=this.options.googleMaps,u=l?.enableWidget!==void 0?{enableWidget:l.enableWidget}:{};r.push({google_maps:u})}this.options?.urlContext&&r.push({url_context:{}}),r.length===0&&(r=void 0);let o,i=Array.isArray(r)?r.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=!!(r&&r.length>0)||!!o,c=!!t?.contextCache&&a;return{tools:r,toolConfig:o,cacheableTools:s||c}}createChatReq=async(e,t)=>{let r=e.model,o=e.modelConfig?.stream??this.config.stream,i=Bs(this.config.audio,e.modelConfig?.audio),s=qs(r,this.config.audio,e.modelConfig?.audio);if(!e.chatPrompt||e.chatPrompt.length===0)throw new Error("Chat prompt is empty");let a;if(s){if(this.isVertex)throw new Error("Gemini Live audio currently supports Google AI API-key WebSocket sessions only");a={name:"gemini-live-audio"}}else this.endpointId?a={name:o?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:a={name:o?`/models/${r}:streamGenerateContent?alt=sse`:`/models/${r}:generateContent`};if(!s&&this.isVertex&&(a.url=this.getVertexApiURL(r,t?.beta)),!s&&!this.isVertex){let A=o?"&":"?",v=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;v&&(a.name+=`${A}key=${v}`)}let c=e.chatPrompt.filter(A=>A.role==="system").map(A=>A.content),l=c.length>0?{role:"user",parts:[{text:c.join(" ")}]}:void 0,u=[],d=e.chatPrompt.filter(A=>A.role!=="system");for(let A=0;A<d.length;A++){let v=d[A];switch(v.role){case"user":{let w=Array.isArray(v.content)?v.content.map((N,k)=>{switch(N.type){case"text":return{text:N.text};case"image":return{inlineData:{mimeType:N.mimeType,data:N.image}};case"audio":return{inlineData:{mimeType:N.mimeType??Lt(N.format,N.sampleRate),data:N.data}};case"file":return"fileUri"in N?{fileData:{mimeType:N.mimeType,fileUri:N.fileUri}}:{inlineData:{mimeType:N.mimeType,data:N.data}};default:throw new Error(`Chat prompt content type not supported (index: ${k})`)}}):[{text:v.content}];u.push({role:"user",parts:w});break}case"assistant":{let w=[],N=v.thoughtBlocks,k=v.functionCalls&&v.functionCalls.length>0,E=N?.[0],F=N?.map(P=>P.data).join("")??"",L=E?.signature;if(F&&w.push({...k?{}:{thought:!0},text:F,...L&&!k?{thought_signature:L}:{}}),v.functionCalls){let P=v.functionCalls.map(($,S)=>{let z;if(typeof $.function.params=="string"){let H=$.function.params;if(H.trim().length===0)z={};else try{z=JSON.parse(H)}catch{throw new Error(`Failed to parse function params JSON: ${H}`)}}else z=$.function.params;let _={functionCall:{name:$.function.name,args:z}};return L&&S===0&&(_.thought_signature=L),_});w.push(...P)}let D=v.audio?.transcript;if((v.content||D)&&w.push({text:v.content??D??""}),w.length===0)throw new Error("Assistant content is empty");u.push({role:"model",parts:w});break}case"function":{let w=[],N=v,k=A;for(;;){if(!("functionId"in N))throw new Error(`Chat prompt functionId is empty (index: ${k})`);if(w.push(..._m(d,k,N)),k+1<d.length&&d[k+1].role==="function")k++,N=d[k];else break}A=k,u.push({role:"user",parts:w});break}default:throw new Error(`Invalid role: ${JSON.stringify(v)} (index: ${A})`)}}let{tools:p,toolConfig:m}=this.buildToolState(e,t),g={};if(this.config.thinking?.includeThoughts&&(g.includeThoughts=!0),this.config.thinking?.thinkingTokenBudget&&(g.thinkingBudget=this.config.thinking.thinkingTokenBudget),this.config.thinking?.thinkingLevel&&Vr(r)&&(g.thinkingLevel=this.config.thinking.thinkingLevel),t?.thinkingTokenBudget){let A=this.getEffectiveMappings(r),v=A.thinkingTokenBudgetLevels;if(Vr(r)){let N=ev(r),k=A.thinkingLevelMapping;if(t.thinkingTokenBudget==="none")g.thinkingLevel=k?.minimal??"minimal";else{let E=t.thinkingTokenBudget,F=k?.[E];F||(F=E==="highest"?"high":E),g.thinkingLevel=F}if(N&&g.thinkingLevel){let E=g.thinkingLevel;E!=="low"&&E!=="high"&&(g.thinkingLevel=E==="minimal"?"low":"high")}}else switch(t.thinkingTokenBudget){case"none":g.thinkingBudget=0,g.includeThoughts=!1,delete g.thinkingLevel;break;case"minimal":g.thinkingBudget=v?.minimal??200;break;case"low":g.thinkingBudget=v?.low??800;break;case"medium":g.thinkingBudget=v?.medium??5e3;break;case"high":g.thinkingBudget=v?.high??1e4;break;case"highest":g.thinkingBudget=v?.highest??24500;break}}g.thinkingLevel&&delete g.thinkingBudget,Vr(r)||delete g.thinkingLevel,Vr(r)&&delete g.thinkingBudget;let h=e.modelConfig?.maxTokens??this.config.maxTokens;if(g.thinkingLevel&&h!==void 0)throw new Error("Cannot set maxTokens when using thinkingLevel with Gemini models. When thinking is enabled, the model manages output tokens automatically. Remove the maxTokens setting or disable thinking.");t?.showThoughts!==void 0&&t?.thinkingTokenBudget!=="none"&&(g.includeThoughts=t.showThoughts);let x=tv(r),f={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,...x?{}:{temperature:e.modelConfig?.temperature??this.config.temperature},...!x&&e.modelConfig?.topP!==void 0?{topP:e.modelConfig.topP}:{},...x?{}:{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(g).length>0?{thinkingConfig:g}:{}};if(!x&&Vr(r)&&(f.temperature===void 0||f.temperature<1)&&(f.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 A=e.responseFormat.schema.schema||e.responseFormat.schema;f.responseMimeType="application/json",f.responseJsonSchema=Sl(A)}else f.responseMimeType="application/json";else this.config.responseFormat&&this.config.responseFormat==="json_object"&&(f.responseMimeType="application/json");let y=this.config.safetySettings,b={contents:u,tools:p,toolConfig:m,systemInstruction:l,generationConfig:f,safetySettings:y};if(s){let A=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;if(!A)throw new Error("GoogleGemini AI API key not set");a=wl({model:r,request:b,apiKey:A,audio:i})}return[a,b]};createEmbedReq=async(e,t)=>{let r=e.embedModel;if(!r)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let o,i;if(this.isVertex)this.endpointId?o={name:`/${this.endpointId}:predict`}:o={name:`/models/${r}:predict`},o.url=this.getVertexApiURL(r,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}};else{let s=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;o={name:`/models/${r}:batchEmbedContents${s?`?key=${s}`:""}`},i={requests:e.texts.map(a=>({model:`models/${r}`,content:{parts:[{text:a}]},outputDimensionality:this.config.dimensions,...this.config.embedType&&{taskType:this.config.embedType}}))}}return[o,i]};createChatResp=e=>{let t,r=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 Ve("Content was blocked due to safety settings",void 0,void 0);case"RECITATION":throw new Ve("Content was blocked due to recitation policy",void 0,void 0);case"MALFORMED_FUNCTION_CALL":throw new Ve("Function call was malformed and blocked",void 0,void 0);case"UNEXPECTED_TOOL_CALL":throw new Ve("Unexpected tool call",void 0,void 0);case"FINISH_REASON_UNSPECIFIED":throw new Ve("Finish reason unspecified",void 0,void 0);case"BLOCKLIST":throw new Ve("Content was blocked due to blocklist",void 0,void 0);case"PROHIBITED_CONTENT":throw new Ve("Content was blocked due to prohibited content",void 0,void 0);case"SPII":throw new Ve("Content was blocked due to SPII",void 0,void 0);case"OTHER":throw new Ve("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:ze(),type:"function",function:{name:l.functionCall.name,params:l.functionCall.args}}]}let u=Rl(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;this.tokensUsed={totalTokens:e.usageMetadata.totalTokenCount,promptTokens:e.usageMetadata.promptTokenCount-i,completionTokens:e.usageMetadata.candidatesTokenCount,thoughtsTokens:e.usageMetadata.thoughtsTokenCount,...i>0?{cacheReadTokens:i}:{}}}let o={results:r,...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(r=>r.embeddings.values):t=e.embeddings.map(r=>r.values),{embeddings:t}};supportsContextCache=e=>{let t=e;return wC.some(r=>t.includes(r)||r.includes(t))};buildCacheCreateOp=(e,t)=>{let r=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(r):`models/${r}`,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;if(u)p=`/${u.parent}/cachedContents`;else{let m=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;p=`/cachedContents${m?`?key=${m}`:""}`}return{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:r,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(r||o)return{functions:[{name:"__gemini_tool_state__",description:JSON.stringify({tools:r,toolConfig:o})}]}};buildCacheUpdateTTLOp=(e,t)=>{let r={ttl:`${t}s`},o=new URLSearchParams({updateMask:"ttl"});if(!this.isVertex&&this.apiKey){let a=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;o.set("key",a)}let 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:r,parseResponse:a=>{let c=a;if(c?.name)return{name:c.name,expiresAt:c.expireTime,tokenCount:c.usageMetadata?.totalTokenCount}}}};buildCacheDeleteOp=e=>{let t=`/${e}`;if(!this.isVertex&&this.apiKey){let o=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;t+=`?key=${o}`}let r=this.getVertexCacheContext();return{type:"delete",apiConfig:{name:t,headers:{"Content-Type":"application/json"},...r?{url:r.baseUrl}:{}},request:{},parseResponse:()=>{}}};prepareCachedChatReq=async(e,t,r)=>{let o=e.model,i=e.modelConfig?.stream??this.config.stream,{tools:s,toolConfig:a,cacheableTools:c}=this.buildToolState(e,t),{dynamicContents:l,dynamicSystemInstruction:u}=this.extractDynamicContent(e.chatPrompt),d;if(this.endpointId?d={name:i?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:d={name:i?`/models/${o}:streamGenerateContent?alt=sse`:`/models/${o}:generateContent`},!this.isVertex){let x=i?"&":"?",f=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;f&&(d.name+=`${x}key=${f}`)}let p=tv(o),m={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,...p?{}:{temperature:e.modelConfig?.temperature??this.config.temperature},...!p&&e.modelConfig?.topP!==void 0?{topP:e.modelConfig.topP}:{},...p?{}:{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"};!p&&Vr(o)&&(m.temperature===void 0||m.temperature<1)&&(m.temperature=1);let g=this.config.safetySettings,h={contents:l,cachedContent:r,generationConfig:m,safetySettings:g};return c||(s&&s.length>0&&(h.tools=s),a&&(h.toolConfig=a)),u&&(h.systemInstruction=u),{apiConfig:d,request:h}};extractCacheableContent(e){let t,r=[],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??Lt(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&&r.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&&r.push({role:"model",parts:a})}else s.role==="function"&&r.push({role:"user",parts:_m(e,i,s)})}return{systemInstruction:t,contents:r}}extractDynamicContent(e){let r=[],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??Lt(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&&r.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&&r.push({role:"model",parts:a})}else s.role==="function"&&r.push({role:"user",parts:_m(e,i,s)})}return{dynamicContents:r,dynamicSystemInstruction:void 0}}},ri=class n extends wt{static create(e){return new n(e)}constructor({apiKey:e,credentialProvider:t,projectId:r,region:o,endpointId:i,config:s,options:a,models:c,modelInfo:l}){let u=r!==void 0&&o!==void 0?{projectId:r,region:o}:void 0,d={...Nm(),...s},p,m,g;if(u){let{projectId:y,region:b}=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 A;i?A="endpoints":A="publishers/google";let v=Rs(b);g=(w,N)=>`https://${v}/${xM(w,N)}/projects/${y}/locations/${b}/${A}`,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()=>({})}let h=new Lm(d,u,i,e,t,a,g);l=[...Ho,...l??[]];let x=y=>{let b=qs(y,{output:{enabled:!0}}),A=Bt({model:y,modelInfo:l,models:c}),v=A?.supported?.structuredOutputs??!0,w=A?.supported?.structuredOutputModes??(v?["native","function"]:["function"]);return{functions:!0,streaming:!0,hasThinkingBudget:A?.supported?.thinkingBudget??!1,hasShowThoughts:A?.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:b?["pcm16","pcm"]:["wav","mp3","aac","ogg"],maxDuration:9.5*60,output:{supported:b,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:A?.supported?.thinkingBudget??!1,multiTurn:!0}},f=c?.map(y=>{let b=y,A=b?.config;if(!A)return y;let v={};A.maxTokens!==void 0&&(v.maxTokens=A.maxTokens),A.temperature!==void 0&&(v.temperature=A.temperature),A.topP!==void 0&&(v.topP=A.topP),A.topK!==void 0&&(v.topK=A.topK),A.presencePenalty!==void 0&&(v.presencePenalty=A.presencePenalty),A.frequencyPenalty!==void 0&&(v.frequencyPenalty=A.frequencyPenalty),A.stopSequences!==void 0&&(v.stopSequences=A.stopSequences),A.endSequences!==void 0&&(v.endSequences=A.endSequences),A.stream!==void 0&&(v.stream=A.stream),A.n!==void 0&&(v.n=A.n);let w={...b};Object.keys(v).length>0&&(w.modelConfig={...b.modelConfig??{},...v});let N=A.thinking?.thinkingTokenBudget;if(typeof N=="number"){let k=d.thinkingTokenBudgetLevels,E=[["minimal",k?.minimal??200],["low",k?.low??800],["medium",k?.medium??5e3],["high",k?.high??1e4],["highest",k?.highest??24500]],F="minimal",L=Number.POSITIVE_INFINITY;for(let[D,P]of E){let $=Math.abs(N-P);$<L&&(L=$,F=D)}w.thinkingTokenBudget=F}return A.thinking?.includeThoughts!==void 0&&(w.showThoughts=!!A.thinking.includeThoughts),A.thinkingLevelMapping&&(w.thinkingLevelMapping=A.thinkingLevelMapping),A.thinkingTokenBudgetLevels&&(w.thinkingTokenBudgetLevels=A.thinkingTokenBudgetLevels),w});f?h.setModels(f):c&&h.setModels(c),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 kl=class{constructor(e={}){this.config=e;this.config.id=this.config.id??ze()}metrics={latency:{chat:{mean:0,p95:0,p99:0,samples:[]},embed:{mean:0,p95:0,p99:0,samples:[]}},errors:{chat:{count:0,rate:0,total:0},embed:{count:0,rate:0,total:0}}};getLastUsedChatModel(){return this.config.modelInfo?.name??"mock-model"}getLastUsedEmbedModel(){return this.config.embedModelInfo?.name??"mock-embed-model"}getLastUsedModelConfig(){return this.config.modelInfo?{maxTokens:this.config.modelInfo.maxTokens,temperature:.7,stream:this.config.features?.streaming??!1}:void 0}getName(){return this.config.name??"mock-ai-service"}getId(){return this.config.id??"mock-ai-service-id"}getFeatures(e){let t=this.config.features?.media;return{functions:this.config.features?.functions??!1,streaming:this.config.features?.streaming??!1,structuredOutputs:this.config.features?.structuredOutputs,...this.config.features?.structuredOutputModes?{structuredOutputModes:this.config.features.structuredOutputModes}:{},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(r=>setTimeout(r,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock chat error");return this.updateMetrics("chat"),typeof this.config.chatResponse=="function"?await this.config.chatResponse(e):this.config.chatResponse??{results:[{index:0,content:"Mock response",finishReason:"stop"}],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:10,completionTokens:5,totalTokens:15}}}}async embed(e,t){if(this.config.latencyMs&&await new Promise(r=>setTimeout(r,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 r=this.metrics.latency[e].samples;if(this.metrics.latency[e].mean=r.reduce((o,i)=>o+i,0)/r.length,r.length>0){let o=[...r].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 Tl=class n{options;lastUsedService;services=new Map;constructor(e){if(e.length===0)throw new Error("No AI services provided.");for(let[t,r]of e.entries())if("key"in r){if(this.services.has(r.key))throw new Error(`Duplicate model key: ${r.key}`);let{service:i,description:s,isInternal:a}=r;this.services.set(r.key,{service:i,description:s,isInternal:a})}else{let i=r.getModelList();if(!i)throw new Error(`Service ${t} \`${r.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} \`${r.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:r,model:s.model});else if("embedModel"in s&&s.embedModel)this.services.set(s.key,{description:s.description,service:r,embedModel:s.embedModel});else throw new Error(`Key ${s.key} in model list for service ${t} \`${r.getName()}\` is missing a model or embedModel property.`)}}}static create(e){return new n(e)}getLastUsedChatModel(){return this.lastUsedService?.getLastUsedChatModel()}getLastUsedEmbedModel(){return this.lastUsedService?.getLastUsedEmbedModel()}getLastUsedModelConfig(){return this.lastUsedService?.getLastUsedModelConfig()}async chat(e,t){let r=e.model;if(!r)throw new Error("Model key must be specified for multi-service");let o=this.services.get(r);if(!o)throw new Error(`No service found for model key: ${r}`);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:r,...e},t)}async embed(e,t){let r=e.embedModel;if(!r)throw new Error("Embed model key must be specified for multi-service");let o=this.services.get(r);if(!o)throw new Error(`No service found for embed model key: ${r}`);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:r,...e},t)}async transcribe(e,t){let r=e.model;if(!r){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(r);if(!o)throw new Error(`No service found for transcription model key: ${r}`);return this.lastUsedService=o.service,await o.service.transcribe(e,t)}async speak(e,t){let r=e.model;if(!r){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(r);if(!o)throw new Error(`No service found for speech model key: ${r}`);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 Pl(n,e,t={}){if(typeof n=="string")return[{type:"text",text:n}];if(!Array.isArray(n))return[{type:"text",text:String(n)}];let r=e.getFeatures(),o=[];for(let i of n)try{switch(i.type){case"text":o.push({type:"text",text:i.text});break;case"image":if(r.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 Qt(s,"image","vision analysis")}else switch(t.fallbackBehavior){case"error":throw new ht("Images",e.getName(),!1);case"skip":continue;default:o.push({type:"text",text:"[Image content not supported by this provider]"})}break;case"audio":if(r.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 Qt(s,"audio","transcription")}else switch(t.fallbackBehavior){case"error":throw new ht("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(r.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 Qt(s,"file","text extraction")}else switch(t.fallbackBehavior){case"error":throw new ht("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(r.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 Qt(s,"url","content fetching")}else switch(t.fallbackBehavior){case"error":throw new ht("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 ht||s instanceof Qt?s:new Qt(s,i.type||"unknown","content processing")}return o}function ov(n){let e=!1,t=!1,r=!1,o=!1;for(let i of n)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":r=!0;break;case"url":o=!0;break}return{hasImages:e,hasAudio:t,hasFiles:r,hasUrls:o}}var Il=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 r=await this.selectProviderWithDegradation(e,t.routingOptions||{}),o=await this.preprocessRequest(e,r.provider,t.processingOptions);try{return{response:await r.provider.chat(o,t),routing:r}}catch(i){if(i instanceof ht&&t.fallbackProviders?.length)return await this.tryFallbackProviders(e,t.fallbackProviders,t);throw i}}async preprocessRequest(e,t,r){let o={...r,fallbackBehavior:r?.fallbackBehavior||"degrade",imageToText:r?.imageToText||this.processingServices.imageToText,audioToText:r?.audioToText||this.processingServices.audioToText,fileToText:r?.fileToText||this.processingServices.fileToText,urlToText:r?.urlToText||this.processingServices.urlToText},i=[];for(let s of e.chatPrompt)if(s.role==="user"&&Array.isArray(s.content)){let a=await Pl(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){let r=Dr(e),o=[],i=[],s=[];try{let a=Xc(e,this.providers,{requireExactMatch:t.requireExactMatch??this.config.capability.requireExactMatch,allowDegradation:t.allowDegradation??this.config.capability.allowDegradation}),c=a.getFeatures();return r.hasImages&&!c.media.images.supported&&(i.push("Images will be converted to text descriptions"),o.push("Image-to-text conversion")),r.hasAudio&&!c.media.audio.supported&&(i.push("Audio will be transcribed to text"),o.push("Audio-to-text transcription")),r.hasFiles&&!c.media.files.supported&&(i.push("File content will be extracted to text"),o.push("File-to-text extraction")),r.hasUrls&&!c.media.urls.supported&&(i.push("URL content will be pre-fetched"),o.push("URL content fetching")),r.requiresStreaming&&!c.streaming&&s.push("Streaming not supported - will use non-streaming mode"),r.requiresCaching&&!c.caching.supported&&s.push("Content caching not supported"),{provider:a,processingApplied:o,degradations:i,warnings:s}}catch(a){throw new Error(`Provider selection failed: ${a instanceof Error?a.message:"Unknown error"}`)}}async tryFallbackProviders(e,t,r){for(let o of t)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,r),routing:i}}catch{}throw new Error("All fallback providers failed")}async getRoutingRecommendation(e){return await this.selectProviderWithDegradation(e,{})}async validateRequest(e){let t=Dr(e),r=[],o=[];try{let i=await this.selectProviderWithDegradation(e,{});return i.degradations.length>0&&(r.push(...i.degradations),o.push("Consider using a provider that natively supports all media types")),i.warnings.length>0&&r.push(...i.warnings),t.hasImages&&this.processingServices.imageToText===void 0&&(this.providers.some(a=>a.getFeatures().media.images.supported)||(r.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)||(r.push("No audio processing service available and no providers support audio"),o.push("Add audioToText processing service or use audio-capable provider"))),{canHandle:r.length===0||i.degradations.length>0,issues:r,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 r=t.getFeatures(),o=t.getName();r.functions&&(e.Functions=e.Functions||[],e.Functions.push(o)),r.streaming&&(e.Streaming=e.Streaming||[],e.Streaming.push(o)),r.media.images.supported&&(e.Images=e.Images||[],e.Images.push(o)),r.media.audio.supported&&(e.Audio=e.Audio||[],e.Audio.push(o)),r.media.files.supported&&(e.Files=e.Files||[],e.Files.push(o)),r.media.urls.supported&&(e.URLs=e.URLs||[],e.URLs.push(o)),r.caching.supported&&(e.Caching=e.Caching||[],e.Caching.push(o))}return{totalProviders:this.providers.length,capabilityMatrix:e,recommendedProvider:this.providers[0]?.getName()||"None"}}};var Um=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...Qe()}),iv=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...ar()}),$m=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,r=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(`
|
|
1267
|
+
`):g.extractedText??`[MCP file result: ${g.filename??"file"} (${g.mimeType})]`).join(`
|
|
1268
|
+
`):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(`
|
|
1267
1269
|
`));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=r?e.functions?.map(u=>({type:"function",function:{name:u.name,description:u.description,parameters:u.parameters||{type:"object",properties:{}}}})):void 0,s=r&&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 r of e)t.enqueue(r);t.close()}catch(r){t.error(r)}}});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((r,o)=>{let i="stop";switch(r.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=r.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:r.message.content||"",functionCalls:s,finishReason:i,logprobs:r.logprobs?this.createLogprobs(r.logprobs):void 0}}),remoteId:e.id});createChatStreamResp=(e,t)=>{let r=t,o=e.choices[0];if(!o)throw new Error("No choice in WebLLM stream response");if(o.delta.content&&(r.content=(r.content||"")+o.delta.content),o.delta.tool_calls){r.toolCalls||(r.toolCalls=[]);for(let c of o.delta.tool_calls){let l=r.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)):r.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);r.logprobs={content:[...r.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=r.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:r.content||"",functionCalls:s,finishReason:i,logprobs:r.logprobs}],remoteId:e.id}};createLogprobs(e){return{content:e.content.map(t=>({token:t.token,logprob:t.logprob,topLogprobs:t.top_logprobs?.map(r=>({token:r.token,logprob:r.logprob}))}))}}createEmbedResp(e){throw new Error("WebLLM does not support embeddings")}},oi=class extends wt{constructor({engine:e,config:t,options:r,models:o}){if(!e)throw new Error("WebLLM engine instance is required");let i={...Um(),...t},s=new $m(i,e);super(s,{name:"WebLLM",apiURL:void 0,headers:async()=>({}),modelInfo:ni,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:r,models:o})}};function sv(n){return zs.create(n)}var zs=class n{ai;static create(e){return new n(e)}constructor(e){let t=yl(e.name);switch(t.transport){case"openai-chat":t.id==="openai"?this.ai=new Yo(e):this.ai=new Zo(e);break;case"openai-responses":t.id==="openai-responses"?this.ai=new Qo(e):this.ai=new ei(e);break;case"anthropic-messages":this.ai=new qo(e);break;case"gemini-generate-content":this.ai=new ri(e);break;case"webllm":this.ai=new oi(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 av(n,e){return n.filter(t=>!e.has(t))}function cv(n){let e={};for(let t of n)e[t]=(e[t]||0)+1;return e}function Hr(n){let e=n.normalize("NFD");return e=e.replace(/\b(a|an|the)\b/g," "),e=e.split(/\s+/).join(" "),e=e.replace(/[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/g,""),e.toLowerCase()}function AM(n,e){return Hr(n)===Hr(e)?1:0}function bM(n,e){let t=Hr(n).split(" "),r=Hr(e).split(" "),o=cv(t),i=cv(r),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/r.length;return 2*a*c/(a+c)}function CM(n,e,t,r=!1){let o=Hr(n).split(" "),i=Hr(e).split(" "),s=Hr(t).split(" "),a=new Set([...bc,...o]);i=av(i,a),s=av(s,a);let c=0,l=c/i.length,u=c/s.length,d=2*l*u/(l+u);return r?u:d}var lv={emScore:AM,f1Score:bM,novelF1ScoreOptimized:CM};var Ml=class{ai;program;examples;constructor({ai:e,program:t,examples:r=[]}){if(r.length===0)throw new Error("No examples found");this.ai=e,this.program=t,this.examples=r}async run(e){let t=Date.now(),r=this.examples.length,o=0;for(let s=0;s<r;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=r>0?o/r:0;this.ai.getOptions().debug&&console.log(`
|
|
1268
1270
|
Performance: `,o,"/",r,"Average Score: ",i,`
|
|
1269
1271
|
`)}};var ii=class extends yn{maxRounds;maxDemos;maxExamples;batchSize;earlyStoppingPatience;costMonitoring;maxTokensPerGeneration;verboseMode;debugMode;qualityThreshold;traces=[];constructor(e){super(e);let t=e.options||{};this.maxRounds=t.maxRounds??3,this.maxDemos=t.maxDemos??4,this.maxExamples=t.maxExamples??16,this.batchSize=t.batchSize??1,this.earlyStoppingPatience=t.earlyStoppingPatience??0,this.costMonitoring=t.costMonitoring??!1,this.maxTokensPerGeneration=t.maxTokensPerGeneration??0,this.verboseMode=t.verboseMode??!0,this.debugMode=t.debugMode??!1,this.qualityThreshold=t.qualityThreshold??.5}async compileRound(e,t,r,o,i){let s=Date.now(),a=i?.maxDemos??this.maxDemos,c={modelConfig:{temperature:.7}};this.maxTokensPerGeneration>0&&(c.modelConfig.max_tokens=this.maxTokensPerGeneration);let l=RM([...t],this.maxExamples),u=this.traces.length,d=e.getId();for(let p=0;p<l.length;p+=this.batchSize){p>0&&(c.modelConfig.temperature=.7+.001*p);let m=l.slice(p,p+this.batchSize);for(let g of m){if(!g||typeof g!="object")continue;let h=t.filter(y=>y!==g);e.setDemos([{traces:h,programId:d}]);let x=this.getTeacherOrStudentAI();this.stats.totalCalls++;let f;try{let y={...c,maxRetries:1};f=await e.forward(x,g,y),this.costMonitoring&&(this.stats.estimatedTokenUsage+=JSON.stringify(g).length/4+JSON.stringify(f).length/4),await o({prediction:f,example:g})>=this.qualityThreshold&&(this.traces=[...this.traces,...e.getTraces()],this.stats.successfulDemos++)}catch(y){(this.verboseMode||this.debugMode)&&console.warn(`Student model failed during bootstrap: ${y instanceof Error?y.message:"Unknown error"}`),f={}}if(this.traces.length>=a)return}}if(this.earlyStoppingPatience>0){let m=this.traces.length-u;if(!this.stats.earlyStopping)this.stats.earlyStopping={bestScoreRound:m>0?r:0,patienceExhausted:!1,reason:"No improvement detected"};else if(m>0)this.stats.earlyStopping.bestScoreRound=r;else if(r-this.stats.earlyStopping.bestScoreRound>=this.earlyStoppingPatience){this.stats.earlyStopping.patienceExhausted=!0,this.stats.earlyStopped=!0,this.stats.earlyStopping.reason=`No improvement for ${this.earlyStoppingPatience} rounds`;return}}}async compile(e,t,r,o){this.validateExamples(t,!1);let i=o?.maxIterations??this.maxRounds;this.traces=[],this.reset();for(let c=0;c<i&&(await this.compileRound(e,t,c,r,o),!this.stats.earlyStopped);c++);if(this.traces.length===0)throw new Error("No demonstrations found. Either provide more examples or improve the existing ones.");let s=vM(this.traces),a=0;return this.traces.length>0&&(a=this.stats.successfulDemos/Math.max(1,this.stats.totalCalls)),await this.logOptimizationComplete("BootstrapFewShot",a,{maxRounds:this.maxRounds,maxDemos:this.maxDemos,batchSize:this.batchSize,successRate:a,demosGenerated:s.length,tracesCollected:this.traces.length},o),{demos:s,stats:this.stats,bestScore:a,finalConfiguration:{maxRounds:this.maxRounds,maxDemos:this.maxDemos,batchSize:this.batchSize,successRate:a}}}};function vM(n){let e=new Map;for(let r of n)if(e.has(r.programId)){let o=e.get(r.programId);o&&o.push(r.trace)}else e.set(r.programId,[r.trace]);let t=[];return e.forEach((r,o)=>{t.push({traces:r,programId:o})}),t}var RM=(n,e)=>{let t=[...n];for(let r=t.length-1;r>0;r--){let o=Math.floor(Math.random()*(r+1)),i=t[r],s=t[o];if(!i||!s)throw new Error("Invalid array elements");[t[r],t[o]]=[s,i]}return t.slice(0,e)};var wM=100,SM=8;async function uv(n,e,t,r){let{bootstrap:o,...i}=r,s=o??e.length<=SM,a={...i,bootstrap:!1,maxMetricCalls:i.maxMetricCalls??wM},c=[];if(s){let d=typeof s=="object"?s:void 0,p=new ii({...i,options:d}),m=async({prediction:h,example:x})=>{let f=await Fr(t,h,x);return Oo(f,a)};c=(await p.compile(n,e,m,a)).demos??[],c.length>0&&n.setDemos(c)}let l=await new ir(i).compile(n,e,t,a);if(c.length===0)return l;let u=l.optimizedProgram;return u&&(l.optimizedProgram=new tn({bestScore:u.bestScore,stats:u.stats,componentMap:u.componentMap,selectorState:u.selectorState,demos:c,examples:u.examples,modelConfig:u.modelConfig,optimizerType:u.optimizerType??"GEPA",optimizationTime:u.optimizationTime??0,totalRounds:u.totalRounds??0,converged:u.converged??!1,scoreHistory:u.scoreHistory,configurationHistory:u.configurationHistory,artifactFormatVersion:u.artifactFormatVersion,instructionSchema:u.instructionSchema})),l.demos=c,l.paretoFront=l.paretoFront.map(d=>({...d,demos:c})),l}var dv=0,pv=n=>(dv++,`${n}-${Date.now()}-${dv}`),Dm=(n,e)=>{if(!Number.isFinite(n)||n<1)throw new Error(`${e} must be a positive number`);return Math.floor(n)},si=n=>{try{return JSON.stringify(n,(e,t)=>typeof t=="bigint"?t.toString():typeof t=="function"?`[Function ${t.name||"anonymous"}]`:t)}catch{return String(n)}},mv=(n,e)=>({temperature:1,...n,...e}),gv=(n,e)=>{if(e==="serial")return"serial";if(e==="native-samples"){if(!(n instanceof Pe))throw new bn('strategy "native-samples" requires an AxGen program');return"native-samples"}return n instanceof Pe?"native-samples":"serial"},El=(n,e)=>n.getTraces().slice(e),Ol=(n,e)=>n.getChatLog().slice(e),Fl=n=>Array.isArray(n)?n:[...n.actor,...n.responder],bn=class extends Error{attempts;constructor(e,t=[]){super(e),this.name="AxRefineError",this.attempts=t}},_l=class{constructor(e){this.program=e}attempts=[];selectedAttempt;getAttempts(){return this.attempts}getSignature(){return this.program.getSignature()}getId(){return this.program.getId()}setId(e){this.program.setId(e)}getTraces(){return this.selectedAttempt?.traces??[]}namedProgramInstances(){return this.program.namedProgramInstances?.()??[]}setDemos(e,t){this.program.setDemos(e,t)}applyOptimization(e){this.program.applyOptimization(e)}getOptimizableComponents(){return this.program.getOptimizableComponents()}applyOptimizedComponents(e){this.program.applyOptimizedComponents(e)}getUsage(){return yr(this.attempts.flatMap(e=>e.usage))}getChatLog(){return this.selectedAttempt?.chatLog??[]}resetUsage(){this.attempts=[],this.selectedAttempt=void 0,this.program.resetUsage()}streamingForward(e,t,r){let o=new bn("bestOfN/refine wrappers do not support streamingForward(); use forward() so complete candidates can be scored.");return{async next(){throw o},async return(){return{done:!0,value:void 0}},async throw(i){throw i??o},[Symbol.asyncIterator](){return this},async[Symbol.asyncDispose](){}}}resetRun(){this.attempts=[],this.selectedAttempt=void 0}async emitAttempt(e,t){this.attempts.push(t),await e?.(t)}async runCandidateBatch({ai:e,input:t,forwardOptions:r,count:o,round:i,failCount:s,strategy:a,threshold:c}){return a==="native-samples"?this.runNativeSampleBatch({ai:e,input:t,forwardOptions:r,count:o,round:i,failCount:s,threshold:c}):this.runSerialBatch({ai:e,input:t,forwardOptions:r,count:o,round:i,failCount:s,threshold:c})}async runNativeSampleBatch({ai:e,input:t,forwardOptions:r,count:o,round:i,failCount:s,threshold:a}){let c=[],l=this.program.getTraces().length,u=this.program.getChatLog().length;this.program.resetUsage();let d=0,p=Number.NEGATIVE_INFINITY,m;try{m=await this.program.forward(e,t,{...r,mem:new On,sessionId:pv("ax-refine-native"),sampleCount:o,modelConfig:{...mv(this.modelConfig,r?.modelConfig),stream:!1},resultPicker:async y=>{if(y.type!=="fields")return 0;let b=a===void 0?void 0:{index:-1};for(let A of y.results){let v=A.sample,w={attempt:this.attempts.length+c.length+1,round:i,sampleIndex:A.index,strategy:"native-samples",input:t,prediction:v,traces:[],chatLog:[],usage:[],reward:await this.rewardFn({input:t,prediction:v,attempt:this.attempts.length+c.length+1,round:i,sampleIndex:A.index,traces:[],chatLog:[]}),metThreshold:!1};w.metThreshold=a!==void 0&&w.reward>=a,w.reward>p&&(p=w.reward,d=A.index),b&&b.index===-1&&w.metThreshold&&(b.index=A.index),c.push(w)}return b&&b.index>=0&&(d=b.index),d}})}catch(y){let b=this.attempts.filter(v=>v.error!==void 0).length+1,A={attempt:this.attempts.length+1,round:i,sampleIndex:0,strategy:"native-samples",input:t,metThreshold:!1,traces:El(this.program,l),chatLog:Ol(this.program,u),usage:Fl(this.program.getUsage()),error:y};if(b>s)throw new bn("Native sample attempt failed",[...this.attempts,A]);return[A]}let g=El(this.program,l),h=Ol(this.program,u),x=Fl(this.program.getUsage());if(c.length===0){let y=this.attempts.length+1,b=await this.rewardFn({input:t,prediction:m,attempt:y,round:i,sampleIndex:0,traces:g,chatLog:h});return c.push({attempt:y,round:i,sampleIndex:0,strategy:"native-samples",input:t,prediction:m,traces:g,chatLog:h,usage:x,reward:b,metThreshold:a!==void 0&&b>=a}),c}let f=c.find(y=>y.sampleIndex===d)??c[0];return f&&(f.prediction=m,f.traces=g,f.chatLog=h,f.usage=x),c}async runSerialBatch({ai:e,input:t,forwardOptions:r,count:o,round:i,failCount:s,threshold:a}){let c=[],l=this.attempts.filter(u=>u.error!==void 0).length;for(let u=0;u<o;u++){let d=this.attempts.length+c.length+1,p=this.program.getTraces().length,m=this.program.getChatLog().length;this.program.resetUsage();try{let g=await this.program.forward(e,t,{...r,mem:new On,sessionId:pv("ax-refine-serial"),modelConfig:mv(this.modelConfig,r?.modelConfig)}),h=El(this.program,p),x=Ol(this.program,m),f=await this.rewardFn({input:t,prediction:g,attempt:d,round:i,sampleIndex:u,traces:h,chatLog:x}),y={attempt:d,round:i,sampleIndex:u,strategy:"serial",input:t,prediction:g,reward:f,metThreshold:a!==void 0&&f>=a,traces:h,chatLog:x,usage:Fl(this.program.getUsage())};if(c.push(y),y.metThreshold)break}catch(g){l++;let h={attempt:d,round:i,sampleIndex:u,strategy:"serial",input:t,metThreshold:!1,traces:El(this.program,p),chatLog:Ol(this.program,m),usage:Fl(this.program.getUsage()),error:g};if(c.push(h),l>s)throw new bn(`Refine attempt failed after ${l} failures`,[...this.attempts,...c])}}return c}selectBest(e){let t=e.filter(r=>r.prediction!==void 0&&typeof r.reward=="number");if(t.length!==0)return t.reduce((r,o)=>o.reward>r.reward?o:r)}firstThreshold(e){return e.find(t=>t.prediction!==void 0&&t.metThreshold)}},Vs=class extends _l{n;failCount;threshold;strategy;modelConfig;rewardFn;onAttempt;constructor(e,t){super(e),this.n=Dm(t.n,"n"),this.failCount=Math.floor(t.failCount??this.n),this.threshold=t.threshold,this.strategy=t.strategy,this.modelConfig=t.modelConfig,this.rewardFn=t.rewardFn,this.onAttempt=t.onAttempt}async forward(e,t,r){this.resetRun();let o=gv(this.program,this.strategy),i=await this.runCandidateBatch({ai:e,input:t,forwardOptions:r,count:this.n,round:1,failCount:this.failCount,strategy:o,threshold:this.threshold});for(let a of i)await this.emitAttempt(this.onAttempt,a);let s=this.firstThreshold(this.attempts)??this.selectBest(this.attempts);if(!s?.prediction)throw new bn("bestOfN produced no successful candidates",this.attempts);return this.selectedAttempt=s,s.prediction}},Hs=class extends _l{rounds;samplesPerRound;failCount;threshold;strategy;modelConfig;rewardFn;feedbackAI;feedbackModelConfig;rewardDescription;programDescription;onAttempt;constructor(e,t){super(e),this.rounds=Dm(t.rounds,"rounds"),this.samplesPerRound=Dm(t.samplesPerRound??1,"samplesPerRound"),this.failCount=Math.floor(t.failCount??this.rounds*this.samplesPerRound),this.threshold=t.threshold,this.strategy=t.strategy,this.modelConfig=t.modelConfig,this.rewardFn=t.rewardFn,this.feedbackAI=t.feedbackAI,this.feedbackModelConfig=t.feedbackModelConfig,this.rewardDescription=t.rewardDescription,this.programDescription=t.programDescription,this.onAttempt=t.onAttempt}async forward(e,t,r){this.resetRun();let o=this.captureInstructionComponents(),i;try{for(let s=1;s<=this.rounds;s++){let a=gv(this.program,this.strategy),c=await this.runCandidateBatch({ai:e,input:t,forwardOptions:r,count:this.samplesPerRound,round:s,failCount:this.failCount,strategy:a,threshold:this.threshold});for(let d of c)await this.emitAttempt(this.onAttempt,d);let l=this.firstThreshold(c);if(l?.prediction)return this.selectedAttempt=l,l.prediction;let u=this.selectBest(c);if(u&&(!i||u.reward>(i.reward??Number.NEGATIVE_INFINITY))&&(i=u),s<this.rounds&&u?.prediction){let d=await this.generateAdvice(e,t,u,o),p=this.applyAdvice(d,o);u.advice=d,u.adviceApplied=p}}if(!i?.prediction)throw new bn("refine produced no successful candidates",this.attempts);return this.selectedAttempt=i,i.prediction}finally{this.restoreInstructionComponents(o)}}captureInstructionComponents(){let e=this.program.getOptimizableComponents().filter(t=>t.kind==="instruction");return new Map(e.map(t=>[t.key,t.current]))}applyAdvice(e,t){let r={};for(let[o,i]of t){let s=e[o]?.trim();s&&(r[o]=`${i}
|