@ax-llm/ax 21.0.13 → 21.0.14
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 +6 -6
- package/index.cjs.map +1 -1
- package/index.d.cts +1 -1
- package/index.d.ts +1 -1
- package/index.global.js +6 -6
- package/index.global.js.map +1 -1
- package/index.js +6 -6
- package/index.js.map +1 -1
- package/package.json +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 +2 -2
- package/skills/ax-agent.md +1 -1
- package/skills/ax-ai.md +1 -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-learn.md +1 -1
- package/skills/ax-llm.md +1 -1
- package/skills/ax-signature.md +1 -1
package/index.cjs
CHANGED
|
@@ -61,7 +61,7 @@ REAL USER QUERY:
|
|
|
61
61
|
|
|
62
62
|
You (\`distiller\`) read the available context and forward an actionable request to the downstream **executor** stage, which owns any available tools/functions and capability checks. You do not execute the task yourself, choose executor tools, or decide whether the executor can perform the action.
|
|
63
63
|
|
|
64
|
-
Call \`final(request, evidence)\` to forward. Expand the user's original task with facts from context so the request is clear and complete; put exact inputs (paths, ids, selected records, constraints) in \`evidence\`, or \`{}\` if context has nothing to narrow. Resolve follow-ups against prior conversation. Never refuse, answer, or ask clarification because of your own lack of tools or perceived executor capabilities \u2014 forwarding *is* the response. Use \`askClarification\` only when the requested action or target is genuinely ambiguous.
|
|
64
|
+
Call \`final(request, evidence)\` to forward. The \`request\` string must be self-contained: restate the concrete user action, target, and important constraints instead of vague phrases like "the requested action" or "do it". Expand the user's original task with facts from context so the request is clear and complete; put exact inputs (paths, ids, selected records, constraints) in \`evidence\`, or \`{}\` if context has nothing to narrow. Resolve follow-ups against prior conversation. Never refuse, answer, or ask clarification because of your own lack of tools or perceived executor capabilities \u2014 forwarding *is* the response. Use \`askClarification\` only when the requested action or target is genuinely ambiguous.
|
|
65
65
|
|
|
66
66
|
The JS runtime is a long-running REPL \u2014 state persists across turns unless restarted. Each **turn**: write code \u2192 it executes \u2192 you see output \u2192 write the next block.
|
|
67
67
|
|
|
@@ -94,8 +94,8 @@ The context map is a small cache of reusable orientation knowledge about the rec
|
|
|
94
94
|
|
|
95
95
|
### How to Work
|
|
96
96
|
|
|
97
|
-
- **Skip exploration when context has nothing to narrow** (direct action request, or schema is already known) \u2014 forward on turn 1 with \`final(
|
|
98
|
-
- **For direct action requests**: preserve the requested action faithfully. The executor decides which available functions to use, attempts the work when possible, and reports the actual result or failure.
|
|
97
|
+
- **Skip exploration when context has nothing to narrow** (direct action request, or schema is already known) \u2014 forward on turn 1 with \`final("<concrete action and target>", {})\`, where the string names the actual action and target from the current inputs.
|
|
98
|
+
- **For direct action requests**: preserve the requested action faithfully in \`request\`; do not collapse it to a generic instruction. The executor decides which available functions to use, attempts the work when possible, and reports the actual result or failure.
|
|
99
99
|
- **When narrowing**: probe shape, narrow with JS, extract. Don't dump raw data. Don't repeat probes already in the Action Log.
|
|
100
100
|
- **Use JS** for deterministic work (filter, sort, slice, regex, dedupe). **Use \`llmQuery\`** only to interpret a narrowed slice \u2014 never pass raw \`inputs.*\` to it.
|
|
101
101
|
- \`console.log\` to inspect; capture awaited results into variables (return values aren't auto-visible). Multiple \`console.log\`s per turn is fine.
|
|
@@ -119,12 +119,12 @@ The \`Javascript Code\` field value must be runnable JavaScript only. Do not put
|
|
|
119
119
|
Valid completion turns:
|
|
120
120
|
|
|
121
121
|
\`\`\`js
|
|
122
|
-
await final("
|
|
122
|
+
await final("Identify which refund emails require a billing-dispute response and summarize the required actions", { matchedEmails });
|
|
123
123
|
\`\`\`
|
|
124
124
|
|
|
125
125
|
\`\`\`js
|
|
126
126
|
// Passthrough \u2014 user asked for an action and there's nothing in context to narrow.
|
|
127
|
-
await final("
|
|
127
|
+
await final("Send the password-reset email to customer@example.com and report the actual result or failure", {});
|
|
128
128
|
\`\`\`
|
|
129
129
|
|
|
130
130
|
\`\`\`js
|
|
@@ -1122,7 +1122,7 @@ ${JSON.stringify(e.functions,null,2)}
|
|
|
1122
1122
|
`);return u?`Assistant: ${c.content}
|
|
1123
1123
|
Functions:
|
|
1124
1124
|
${u}`:`Assistant: ${c.content}`}default:throw new Error("Unknown role")}}).join(`
|
|
1125
|
-
`),s=`${n} ${o}`.trim(),i={name:"/models"},a={model:t,inputs:s,parameters:{max_new_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,repetition_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},top_k:e.modelConfig?.topK??this.config.topK,return_full_text:this.config.returnFullText,num_return_sequences:this.config.n,do_sample:this.config.doSample,max_time:this.config.maxTime},options:{use_cache:this.config.useCache,wait_for_model:this.config.waitForModel}};return[i,a]};createChatResp=e=>({results:[{index:0,content:e.generated_text}]})},Io=class extends Xe{constructor({apiKey:e,config:t,options:n,models:o}){if(!e||e==="")throw new Error("HuggingFace API key not set");let s={...Fl(),...t},i=new Pl(s);super(i,{name:"HuggingFace",apiURL:"https://api-inference.huggingface.co",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:so,defaults:{model:s.model},options:n,supportFor:{functions:!1,streaming:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},models:o})}};var va=()=>structuredClone({model:"mistral-small-latest",...xe(),topP:1}),Zh=()=>structuredClone({...va(),model:"mistral-large-latest"}),Ro=class extends Qe{constructor({apiKey:e,config:t,options:n,models:o,modelInfo:s}){if(!e||e==="")throw new Error("Mistral API key not set");let i={...va(),...t};s=[...ao,...s??[]];let a={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},c=(u,l)=>{let{max_completion_tokens:p,messages:d,...g}=u;return{...g,messages:this.updateMessages(d),max_tokens:p}};super({apiKey:e,config:i,options:n,apiURL:"https://api.mistral.ai/v1",modelInfo:s,models:o,supportFor:a,chatReqUpdater:c}),super.setName("Mistral"),this.setBatchAudioConfig({transcriptionModel:"voxtral-mini-latest",speechModel:"voxtral-mini-tts-2603",speechFormat:"mp3"})}async speak(e,t){let n=e.format??this.batchAudioConfig.speechFormat??"mp3",o=typeof e.voice=="object"?e.voice.id:e.voice,s=this.getOptions();return await sn({url:`${this.openAICompatibleApiURL}/audio/speech`,headers:{Authorization:`Bearer ${this.openAICompatibleApiKey}`},body:{model:typeof e.model=="string"?e.model:this.batchAudioConfig.speechModel,input:e.text,response_format:n,...o?{voice_id:o}:{}},format:n,transcript:e.text,fetch:t?.fetch??s.fetch,abortSignal:t?.abortSignal??s.abortSignal})}updateMessages(e){let t=[];if(!Array.isArray(e))return e;for(let n of e)if(n.role==="user"&&Array.isArray(n.content)){let o=n.content.map(s=>typeof s=="object"&&s!==null&&"image_url"in s?{type:"image_url",image_url:{url:s.image_url?.url}}:s);t.push({...n,content:o})}else t.push(n);return t}};var Oa=class{constructor(e={}){this.config=e;this.config.id=this.config.id??pt()}metrics={latency:{chat:{mean:0,p95:0,p99:0,samples:[]},embed:{mean:0,p95:0,p99:0,samples:[]}},errors:{chat:{count:0,rate:0,total:0},embed:{count:0,rate:0,total:0}}};getLastUsedChatModel(){return this.config.modelInfo?.name??"mock-model"}getLastUsedEmbedModel(){return this.config.embedModelInfo?.name??"mock-embed-model"}getLastUsedModelConfig(){return this.config.modelInfo?{maxTokens:this.config.modelInfo.maxTokens,temperature:.7,stream:this.config.features?.streaming??!1}:void 0}getName(){return this.config.name??"mock-ai-service"}getId(){return this.config.id??"mock-ai-service-id"}getFeatures(e){let t=this.config.features?.media;return{functions:this.config.features?.functions??!1,streaming:this.config.features?.streaming??!1,structuredOutputs:this.config.features?.structuredOutputs??!1,media:{images:{supported:t?.images?.supported??!1,formats:t?.images?.formats??[]},audio:{supported:t?.audio?.supported??!1,formats:t?.audio?.formats??[]},files:{supported:t?.files?.supported??!1,formats:t?.files?.formats??[],uploadMethod:t?.files?.uploadMethod??"none"},urls:{supported:t?.urls?.supported??!1,webSearch:t?.urls?.webSearch??!1,contextFetching:t?.urls?.contextFetching??!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}getModelList(){return this.config.models}getMetrics(){return this.metrics}getEstimatedCost(){return 0}async chat(e,t){if(this.config.latencyMs&&await new Promise(n=>setTimeout(n,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock chat error");return this.updateMetrics("chat"),typeof this.config.chatResponse=="function"?await this.config.chatResponse(e):this.config.chatResponse??{results:[{index:0,content:"Mock response",finishReason:"stop"}],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:10,completionTokens:5,totalTokens:15}}}}async embed(e,t){if(this.config.latencyMs&&await new Promise(n=>setTimeout(n,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock embed error");return this.updateMetrics("embed"),typeof this.config.embedResponse=="function"?this.config.embedResponse(e):this.config.embedResponse??{embeddings:[[.1,.2,.3]],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:5,completionTokens:0,totalTokens:5}}}}async transcribe(e,t){if(this.config.shouldError)throw new Error(this.config.errorMessage??"Mock transcribe error");return typeof this.config.transcribeResponse=="function"?this.config.transcribeResponse(e):this.config.transcribeResponse??{text:"Mock transcription"}}async speak(e,t){if(this.config.shouldError)throw new Error(this.config.errorMessage??"Mock speech error");return typeof this.config.speechResponse=="function"?this.config.speechResponse(e):this.config.speechResponse??{data:"bW9jay1hdWRpbw==",format:"mp3",mimeType:"audio/mpeg",transcript:e.text}}setOptions(e){this.config.options=e}getOptions(){return this.config.options??{}}getLogger(){return this.config.options?.logger??(e=>{console.log(e)})}updateMetrics(e){let t=this.config.latencyMs??0;this.metrics.latency[e].samples.push(t);let n=this.metrics.latency[e].samples;if(this.metrics.latency[e].mean=n.reduce((o,s)=>o+s,0)/n.length,n.length>0){let o=[...n].sort((a,c)=>a-c),s=Math.max(0,Math.floor(o.length*.95)-1);this.metrics.latency[e].p95=o[s]??t;let i=Math.max(0,Math.floor(o.length*.99)-1);this.metrics.latency[e].p99=o[i]??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 Ma=class r{options;lastUsedService;services=new Map;constructor(e){if(e.length===0)throw new Error("No AI services provided.");for(let[t,n]of e.entries())if("key"in n){if(this.services.has(n.key))throw new Error(`Duplicate model key: ${n.key}`);let{service:s,description:i,isInternal:a}=n;this.services.set(n.key,{service:s,description:i,isInternal:a})}else{let s=n.getModelList();if(!s)throw new Error(`Service ${t} \`${n.getName()}\` has no model list.`);for(let i of s){if(this.services.has(i.key)){let a=this.services.get(i.key)?.service;throw new Error(`Service ${t} \`${n.getName()}\` has duplicate model key: ${i.key} as service ${a?.getName()}`)}if("model"in i&&typeof i.model)this.services.set(i.key,{description:i.description,service:n,model:i.model});else if("embedModel"in i&&i.embedModel)this.services.set(i.key,{description:i.description,service:n,embedModel:i.embedModel});else throw new Error(`Key ${i.key} in model list for service ${t} \`${n.getName()}\` is missing a model or embedModel property.`)}}}static create(e){return new r(e)}getLastUsedChatModel(){return this.lastUsedService?.getLastUsedChatModel()}getLastUsedEmbedModel(){return this.lastUsedService?.getLastUsedEmbedModel()}getLastUsedModelConfig(){return this.lastUsedService?.getLastUsedModelConfig()}async chat(e,t){let n=e.model;if(!n)throw new Error("Model key must be specified for multi-service");let o=this.services.get(n);if(!o)throw new Error(`No service found for model key: ${n}`);if(this.lastUsedService=o.service,!o.model){let{model:s,...i}=e;return await o.service.chat(i,t)}return await o.service.chat({model:n,...e},t)}async embed(e,t){let n=e.embedModel;if(!n)throw new Error("Embed model key must be specified for multi-service");let o=this.services.get(n);if(!o)throw new Error(`No service found for embed model key: ${n}`);if(this.lastUsedService=o.service,!o.model){let{embedModel:s,...i}=e;return await o.service.embed(i,t)}return await o.service.embed({embedModel:n,...e},t)}async transcribe(e,t){let n=e.model;if(!n){let s=Array.from(this.services.values())[0]?.service;if(!s)throw new Error("No AI services provided.");return this.lastUsedService=s,await s.transcribe(e,t)}let o=this.services.get(n);if(!o)throw new Error(`No service found for transcription model key: ${n}`);return this.lastUsedService=o.service,await o.service.transcribe(e,t)}async speak(e,t){let n=e.model;if(!n){let s=Array.from(this.services.values())[0]?.service;if(!s)throw new Error("No AI services provided.");return this.lastUsedService=s,await s.speak(e,t)}let o=this.services.get(n);if(!o)throw new Error(`No service found for speech model key: ${n}`);return this.lastUsedService=o.service,await o.service.speak(e,t)}getId(){return`MultiServiceRouter:${Array.from(this.services.values()).map(e=>e.service.getId()).join(",")}`}getName(){return"MultiServiceRouter"}getModelList(){return Array.from(this.services).filter(([,e])=>!e.isInternal).map(([e,t])=>{if(t.model)return{key:e,description:t.description,model:t.model};if(t.embedModel)return{key:e,description:t.description,embedModel:t.embedModel};throw new Error(`Service ${e} has no model or embedModel`)})}getFeatures(e){if(e){let t=this.services.get(e);if(t)return t.service.getFeatures(e)}return{functions:!1,streaming:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}getMetrics(){let e=this.lastUsedService;if(!e){let t=this.services.values().next().value;t&&(e="service"in t?t.service:t)}if(!e)throw new Error("No service available to get metrics.");return e.getMetrics()}getEstimatedCost(e){return this.lastUsedService?.getEstimatedCost(e)??0}setOptions(e){for(let t of this.services.values())t.service.setOptions(e);this.options=e}getOptions(){return this.options??{}}getLogger(){let e=this.lastUsedService;if(!e){let t=this.services.values().next().value;t&&(e=t.service)}if(!e)throw new Error("No service available to get logger.");return e.getLogger()}setServiceEntry(e,t){this.services.set(e,t)}};function uI(r){let e=r.match(/^<think>([\s\S]*?)<\/think>\n?([\s\S]*)$/);return e?{thought:e[1]||void 0,content:e[2]||void 0}:{content:r}}function lI(r,e){if(!r)return{};if(!e.ollamaInThink&&!r.includes("<think>"))return{content:r};let t=r,n="",o="";if(!e.ollamaInThink&&t.includes("<think>")){let s=t.indexOf("<think>");o+=t.slice(0,s),t=t.slice(s+7),e.ollamaInThink=!0}if(e.ollamaInThink)if(t.includes("</think>")){let s=t.indexOf("</think>");n+=t.slice(0,s),t=t.slice(s+8).replace(/^\n/,""),e.ollamaInThink=!1,o+=t}else n+=t;return{content:o||void 0,thought:n||void 0}}var _l=()=>structuredClone({...xe(),model:"nous-hermes2",embedModel:"all-minilm"}),Xh=()=>structuredClone({...rt(),model:"nous-hermes2",embedModel:"all-minilm"}),To=class extends Qe{constructor({apiKey:e="not-set",url:t="http://localhost:11434/v1",config:n,options:o,models:s}){let i={..._l(),...n},a=(l,p)=>p.thinkingTokenBudget?{...l,think:p.thinkingTokenBudget!=="none"}:l,c=l=>({...l,results:l.results.map(p=>{if(!p.content)return p;let d=uI(p.content);return{...p,thought:d.thought??p.thought,content:d.content}})}),u=(l,p)=>({...l,results:l.results.map(d=>{let{content:g,thought:m}=lI(d.content,p);return{...d,content:g,thought:m??d.thought}})});super({apiKey:e,options:o,config:i,apiURL:t,models:s,modelInfo:[],chatReqUpdater:a,chatRespProcessor:c,chatStreamRespProcessor:u,supportFor:{functions:!0,streaming:!0,hasThinkingBudget:!0,hasShowThoughts:!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:!0,multiTurn:!0}}),super.setName("Ollama")}};var pI=r=>["o1","o1-mini","o1-pro","o3","o3-mini","o3-pro","o4-mini"].includes(r),wo=class{constructor(e,t,n){this.config=e;this.streamingUsage=t;this.responsesReqUpdater=n}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,stopSequences:e.stopSequences,topP:e.topP,stream:e.stream}}mapInternalContentToResponsesInput(e,t){let n=[];for(let o of e){if(o.type==="text"){t==="assistant"?n.push({type:"output_text",text:o.text}):n.push({type:"input_text",text:o.text});continue}if(t==="assistant")continue;if(o.type==="image"){let i=`data:${o.mimeType};base64,${o.image}`;n.push({type:"input_image",image_url:{url:i,details:o.details??"auto"}});continue}if(o.type==="audio"){n.push({type:"input_audio",input_audio:{data:o.data,format:o.format==="wav"?"wav":void 0}});continue}let s=o;throw new Error(`Unsupported content part: ${JSON.stringify(s)}`)}return n}createResponsesReqInternalInput(e,t=!1){let n=[];for(let o of e){if(t&&o.role==="system")continue;let s;if(o.role==="system"||o.role==="user"||o.role==="assistant"&&o.content)if(typeof o.content=="string")o.role==="system"?s=o.content:o.role==="assistant"?s=[{type:"output_text",text:o.content}]:s=[{type:"input_text",text:o.content}];else if(Array.isArray(o.content))s=this.mapInternalContentToResponsesInput(o.content,o.role==="assistant"?"assistant":"user");else{if(!(o.role==="assistant"&&!o.content&&o.functionCalls))throw new Error(`Invalid content type for role ${o.role}`);s=""}else o.role,s="";switch(o.role){case"system":n.push({type:"message",role:"system",content:s});break;case"user":n.push({type:"message",role:"user",content:s,name:o.name});break;case"assistant":if(o.content||o.functionCalls){let i={type:"message",role:"assistant",content:""};if(o.content&&(i.content=s),o.name&&(i.name=o.name),o.content&&n.push(i),o.functionCalls)for(let a of o.functionCalls)n.push({type:"function_call",call_id:a.id,name:a.function.name,arguments:typeof a.function.params=="object"?JSON.stringify(a.function.params):a.function.params||""})}break;case"function":n.push({type:"function_call_output",call_id:o.functionId,output:o.result});break;default:{let i=o.role;throw new Error(`Invalid role in chat prompt: ${i}`)}}}return n}createChatReq(e,t){let n=e.model,o={name:"/responses"},s=null,i=!1;if(e.chatPrompt){for(let h of e.chatPrompt)if(h.role==="system"&&typeof h.content=="string"){s=h.content,i=!0;break}}let a=s??this.config.systemPrompt??null,c=e.functions?.map(h=>({type:"function",name:h.name,description:h.description,parameters:h.parameters??{}})),u=[],l=pI(n),p=this.config.reasoningSummary;t?.showThoughts?p||(p="auto"):p=void 0;let d=this.config.reasoningEffort;if(t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"none":d=void 0;break;case"minimal":d="minimal";break;case"low":d="medium";break;case"medium":case"high":d="high";break;case"highest":d="xhigh";break}let g={model:n,input:"",instructions:a,tools:c?.length?c:void 0,tool_choice:e.functionCall==="none"||e.functionCall==="auto"||e.functionCall==="required"?e.functionCall:typeof e.functionCall=="object"&&e.functionCall.function?{type:"function",name:e.functionCall.function.name}:void 0,...l?{max_output_tokens:e.modelConfig?.maxTokens??this.config.maxTokens??void 0}:{...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty??void 0,frequency_penalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty??void 0,max_output_tokens:e.modelConfig?.maxTokens??this.config.maxTokens??void 0},stream:e.modelConfig?.stream??this.config.stream??!1,background:void 0,include:u.length>0?u:void 0,metadata:void 0,parallel_tool_calls:this.config.parallelToolCalls,previous_response_id:void 0,...d?{reasoning:{effort:d,summary:p}}:{},service_tier:this.config.serviceTier,store:this.config.store,text:void 0,truncation:void 0,user:this.config.user,seed:this.config.seed};this.config.user&&(g.user=this.config.user),this.config.parallelToolCalls!==void 0&&(g.parallel_tool_calls=this.config.parallelToolCalls),e.responseFormat?g.text={format:e.responseFormat.type==="json_schema"?{type:"json_schema",json_schema:e.responseFormat.schema}:{type:e.responseFormat.type}}:this.config.responseFormat&&(g.text={format:{type:this.config.responseFormat}}),this.config.seed&&(g.seed=this.config.seed);let m=e.chatPrompt?this.createResponsesReqInternalInput(e.chatPrompt,i):[];if(m.length>0)g.input=m;else if(e.chatPrompt&&e.chatPrompt.length===1&&e.chatPrompt[0]?.role==="user"&&e.chatPrompt[0]?.content&&typeof e.chatPrompt[0].content=="string"&&!a)g.input=e.chatPrompt[0].content;else if(m.length===0&&!a)throw new Error("Responses API request must have input or instructions.");let f=g.reasoning??{};if(this.config.reasoningEffort&&(f={...f,effort:this.config.reasoningEffort}),t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"none":f={};break;case"minimal":f={...f,effort:"minimal"};break;case"low":f={...f,effort:"medium"};break;case"medium":case"high":f={...f,effort:"high"};break;case"highest":f={...f,effort:"xhigh"};break}Object.keys(f).length>0&&f.effort?g.reasoning=f:g.reasoning=void 0;let x=g;return this.responsesReqUpdater&&(x=this.responsesReqUpdater(x)),[o,x]}createChatResp(e){let{id:t,output:n,usage:o}=e;this.tokensUsed=bn(o);let s={};for(let i of n??[])switch(i.type){case"message":s.id=i.id,s.content=Ll(i.content,t),s.finishReason=i.status==="completed"?"stop":"content_filter",s.citations=Ea(i.content);break;case"reasoning":s.id=i.id,i.encrypted_content?s.thought=i.encrypted_content:s.thought=i.summary.map(a=>typeof a=="object"?JSON.stringify(a):a).join(`
|
|
1125
|
+
`),s=`${n} ${o}`.trim(),i={name:"/models"},a={model:t,inputs:s,parameters:{max_new_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,repetition_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},top_k:e.modelConfig?.topK??this.config.topK,return_full_text:this.config.returnFullText,num_return_sequences:this.config.n,do_sample:this.config.doSample,max_time:this.config.maxTime},options:{use_cache:this.config.useCache,wait_for_model:this.config.waitForModel}};return[i,a]};createChatResp=e=>({results:[{index:0,content:e.generated_text}]})},Io=class extends Xe{constructor({apiKey:e,config:t,options:n,models:o}){if(!e||e==="")throw new Error("HuggingFace API key not set");let s={...Fl(),...t},i=new Pl(s);super(i,{name:"HuggingFace",apiURL:"https://api-inference.huggingface.co",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:so,defaults:{model:s.model},options:n,supportFor:{functions:!1,streaming:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},models:o})}};var va=()=>structuredClone({model:"mistral-small-latest",...xe(),topP:1}),Zh=()=>structuredClone({...va(),model:"mistral-large-latest"}),Ro=class extends Qe{constructor({apiKey:e,config:t,options:n,models:o,modelInfo:s}){if(!e||e==="")throw new Error("Mistral API key not set");let i={...va(),...t};s=[...ao,...s??[]];let a={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},c=(u,l)=>{let{max_completion_tokens:p,messages:d,...g}=u;return{...g,messages:this.updateMessages(d),max_tokens:p}};super({apiKey:e,config:i,options:n,apiURL:"https://api.mistral.ai/v1",modelInfo:s,models:o,supportFor:a,chatReqUpdater:c}),super.setName("Mistral"),this.setBatchAudioConfig({transcriptionModel:"voxtral-mini-latest",speechModel:"voxtral-mini-tts-2603",speechFormat:"mp3"})}async speak(e,t){let n=e.format??this.batchAudioConfig.speechFormat??"mp3",o=typeof e.voice=="object"?e.voice.id:e.voice,s=this.getOptions();return await sn({url:`${this.openAICompatibleApiURL}/audio/speech`,headers:{Authorization:`Bearer ${this.openAICompatibleApiKey}`},body:{model:typeof e.model=="string"?e.model:this.batchAudioConfig.speechModel,input:e.text,response_format:n,...o?{voice_id:o}:{}},format:n,transcript:e.text,fetch:t?.fetch??s.fetch,abortSignal:t?.abortSignal??s.abortSignal})}updateMessages(e){let t=[];if(!Array.isArray(e))return e;for(let n of e)if(n.role==="user"&&Array.isArray(n.content)){let o=n.content.map(s=>typeof s=="object"&&s!==null&&"image_url"in s?{type:"image_url",image_url:{url:s.image_url?.url}}:s);t.push({...n,content:o})}else t.push(n);return t}};var Oa=class{constructor(e={}){this.config=e;this.config.id=this.config.id??pt()}metrics={latency:{chat:{mean:0,p95:0,p99:0,samples:[]},embed:{mean:0,p95:0,p99:0,samples:[]}},errors:{chat:{count:0,rate:0,total:0},embed:{count:0,rate:0,total:0}}};getLastUsedChatModel(){return this.config.modelInfo?.name??"mock-model"}getLastUsedEmbedModel(){return this.config.embedModelInfo?.name??"mock-embed-model"}getLastUsedModelConfig(){return this.config.modelInfo?{maxTokens:this.config.modelInfo.maxTokens,temperature:.7,stream:this.config.features?.streaming??!1}:void 0}getName(){return this.config.name??"mock-ai-service"}getId(){return this.config.id??"mock-ai-service-id"}getFeatures(e){let t=this.config.features?.media;return{functions:this.config.features?.functions??!1,streaming:this.config.features?.streaming??!1,structuredOutputs:this.config.features?.structuredOutputs??!1,media:{images:{supported:t?.images?.supported??!1,formats:t?.images?.formats??[]},audio:{supported:t?.audio?.supported??!1,formats:t?.audio?.formats??[]},files:{supported:t?.files?.supported??!1,formats:t?.files?.formats??[],uploadMethod:t?.files?.uploadMethod??"none"},urls:{supported:t?.urls?.supported??!1,webSearch:t?.urls?.webSearch??!1,contextFetching:t?.urls?.contextFetching??!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}getModelList(){return this.config.models}getMetrics(){return this.metrics}getEstimatedCost(){return 0}async chat(e,t){if(this.config.latencyMs&&await new Promise(n=>setTimeout(n,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock chat error");return this.updateMetrics("chat"),typeof this.config.chatResponse=="function"?await this.config.chatResponse(e):this.config.chatResponse??{results:[{index:0,content:"Mock response",finishReason:"stop"}],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:10,completionTokens:5,totalTokens:15}}}}async embed(e,t){if(this.config.latencyMs&&await new Promise(n=>setTimeout(n,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock embed error");return this.updateMetrics("embed"),typeof this.config.embedResponse=="function"?this.config.embedResponse(e):this.config.embedResponse??{embeddings:[[.1,.2,.3]],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:5,completionTokens:0,totalTokens:5}}}}async transcribe(e,t){if(this.config.shouldError)throw new Error(this.config.errorMessage??"Mock transcribe error");return typeof this.config.transcribeResponse=="function"?this.config.transcribeResponse(e):this.config.transcribeResponse??{text:"Mock transcription"}}async speak(e,t){if(this.config.shouldError)throw new Error(this.config.errorMessage??"Mock speech error");return typeof this.config.speechResponse=="function"?this.config.speechResponse(e):this.config.speechResponse??{data:"bW9jay1hdWRpbw==",format:"mp3",mimeType:"audio/mpeg",transcript:e.text}}setOptions(e){this.config.options=e}getOptions(){return this.config.options??{}}getLogger(){return this.config.options?.logger??(e=>{console.log(e)})}updateMetrics(e){let t=this.config.latencyMs??0;this.metrics.latency[e].samples.push(t);let n=this.metrics.latency[e].samples;if(this.metrics.latency[e].mean=n.reduce((o,s)=>o+s,0)/n.length,n.length>0){let o=[...n].sort((a,c)=>a-c),s=Math.max(0,Math.floor(o.length*.95)-1);this.metrics.latency[e].p95=o[s]??t;let i=Math.max(0,Math.floor(o.length*.99)-1);this.metrics.latency[e].p99=o[i]??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 Ma=class r{options;lastUsedService;services=new Map;constructor(e){if(e.length===0)throw new Error("No AI services provided.");for(let[t,n]of e.entries())if("key"in n){if(this.services.has(n.key))throw new Error(`Duplicate model key: ${n.key}`);let{service:s,description:i,isInternal:a}=n;this.services.set(n.key,{service:s,description:i,isInternal:a})}else{let s=n.getModelList();if(!s)throw new Error(`Service ${t} \`${n.getName()}\` has no model list.`);for(let i of s){if(this.services.has(i.key)){let a=this.services.get(i.key)?.service;throw new Error(`Service ${t} \`${n.getName()}\` has duplicate model key: ${i.key} as service ${a?.getName()}`)}if("model"in i&&typeof i.model)this.services.set(i.key,{description:i.description,service:n,model:i.model});else if("embedModel"in i&&i.embedModel)this.services.set(i.key,{description:i.description,service:n,embedModel:i.embedModel});else throw new Error(`Key ${i.key} in model list for service ${t} \`${n.getName()}\` is missing a model or embedModel property.`)}}}static create(e){return new r(e)}getLastUsedChatModel(){return this.lastUsedService?.getLastUsedChatModel()}getLastUsedEmbedModel(){return this.lastUsedService?.getLastUsedEmbedModel()}getLastUsedModelConfig(){return this.lastUsedService?.getLastUsedModelConfig()}async chat(e,t){let n=e.model;if(!n)throw new Error("Model key must be specified for multi-service");let o=this.services.get(n);if(!o)throw new Error(`No service found for model key: ${n}`);if(this.lastUsedService=o.service,!o.model){let{model:s,...i}=e;return await o.service.chat(i,t)}return await o.service.chat({model:n,...e},t)}async embed(e,t){let n=e.embedModel;if(!n)throw new Error("Embed model key must be specified for multi-service");let o=this.services.get(n);if(!o)throw new Error(`No service found for embed model key: ${n}`);if(this.lastUsedService=o.service,!o.model){let{embedModel:s,...i}=e;return await o.service.embed(i,t)}return await o.service.embed({embedModel:n,...e},t)}async transcribe(e,t){let n=e.model;if(!n){let s=Array.from(this.services.values())[0]?.service;if(!s)throw new Error("No AI services provided.");return this.lastUsedService=s,await s.transcribe(e,t)}let o=this.services.get(n);if(!o)throw new Error(`No service found for transcription model key: ${n}`);return this.lastUsedService=o.service,await o.service.transcribe(e,t)}async speak(e,t){let n=e.model;if(!n){let s=Array.from(this.services.values())[0]?.service;if(!s)throw new Error("No AI services provided.");return this.lastUsedService=s,await s.speak(e,t)}let o=this.services.get(n);if(!o)throw new Error(`No service found for speech model key: ${n}`);return this.lastUsedService=o.service,await o.service.speak(e,t)}getId(){return`MultiServiceRouter:${Array.from(this.services.values()).map(e=>e.service.getId()).join(",")}`}getName(){return"MultiServiceRouter"}getModelList(){return Array.from(this.services).filter(([,e])=>!e.isInternal).map(([e,t])=>{if(t.model)return{key:e,description:t.description,model:t.model};if(t.embedModel)return{key:e,description:t.description,embedModel:t.embedModel};throw new Error(`Service ${e} has no model or embedModel`)})}getFeatures(e){if(e){let t=this.services.get(e);if(t)return t.service.getFeatures(e)}return{functions:!1,streaming:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}getMetrics(){let e=this.lastUsedService;if(!e){let t=this.services.values().next().value;t&&(e="service"in t?t.service:t)}if(!e)throw new Error("No service available to get metrics.");return e.getMetrics()}getEstimatedCost(e){return this.lastUsedService?.getEstimatedCost(e)??0}setOptions(e){for(let t of this.services.values())t.service.setOptions(e);this.options=e}getOptions(){return this.options??{}}getLogger(){let e=this.lastUsedService;if(!e){let t=this.services.values().next().value;t&&(e=t.service)}if(!e)throw new Error("No service available to get logger.");return e.getLogger()}setServiceEntry(e,t){this.services.set(e,t)}};function uI(r){let e=r.match(/^<think>([\s\S]*?)<\/think>\n?([\s\S]*)$/);return e?{thought:e[1]||void 0,content:e[2]||void 0}:{content:r}}function lI(r,e){if(!r)return{};if(!e.ollamaInThink&&!r.includes("<think>"))return{content:r};let t=r,n="",o="";if(!e.ollamaInThink&&t.includes("<think>")){let s=t.indexOf("<think>");o+=t.slice(0,s),t=t.slice(s+7),e.ollamaInThink=!0}if(e.ollamaInThink)if(t.includes("</think>")){let s=t.indexOf("</think>");n+=t.slice(0,s),t=t.slice(s+8).replace(/^\n/,""),e.ollamaInThink=!1,o+=t}else n+=t;return{content:o||void 0,thought:n||void 0}}var _l=()=>structuredClone({...xe(),model:"nous-hermes2",embedModel:"all-minilm"}),Xh=()=>structuredClone({...rt(),model:"nous-hermes2",embedModel:"all-minilm"}),To=class extends Qe{constructor({apiKey:e="not-set",url:t="http://localhost:11434/v1",config:n,options:o,models:s}){let i={..._l(),...n},a=(l,p)=>{let d={...l};return p.thinkingTokenBudget&&(d.think=p.thinkingTokenBudget!=="none"),d},c=l=>({...l,results:l.results.map(p=>{if(!p.content)return p;let d=uI(p.content);return{...p,thought:d.thought??p.thought,content:d.content}})}),u=(l,p)=>({...l,results:l.results.map(d=>{let{content:g,thought:m}=lI(d.content,p);return{...d,content:g,thought:m??d.thought}})});super({apiKey:e,options:o,config:i,apiURL:t,models:s,modelInfo:[],chatReqUpdater:a,chatRespProcessor:c,chatStreamRespProcessor:u,supportFor:{functions:!0,streaming:!0,structuredOutputs:!0,hasThinkingBudget:!0,hasShowThoughts:!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:!0,multiTurn:!0}}),super.setName("Ollama")}};var pI=r=>["o1","o1-mini","o1-pro","o3","o3-mini","o3-pro","o4-mini"].includes(r),wo=class{constructor(e,t,n){this.config=e;this.streamingUsage=t;this.responsesReqUpdater=n}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,stopSequences:e.stopSequences,topP:e.topP,stream:e.stream}}mapInternalContentToResponsesInput(e,t){let n=[];for(let o of e){if(o.type==="text"){t==="assistant"?n.push({type:"output_text",text:o.text}):n.push({type:"input_text",text:o.text});continue}if(t==="assistant")continue;if(o.type==="image"){let i=`data:${o.mimeType};base64,${o.image}`;n.push({type:"input_image",image_url:{url:i,details:o.details??"auto"}});continue}if(o.type==="audio"){n.push({type:"input_audio",input_audio:{data:o.data,format:o.format==="wav"?"wav":void 0}});continue}let s=o;throw new Error(`Unsupported content part: ${JSON.stringify(s)}`)}return n}createResponsesReqInternalInput(e,t=!1){let n=[];for(let o of e){if(t&&o.role==="system")continue;let s;if(o.role==="system"||o.role==="user"||o.role==="assistant"&&o.content)if(typeof o.content=="string")o.role==="system"?s=o.content:o.role==="assistant"?s=[{type:"output_text",text:o.content}]:s=[{type:"input_text",text:o.content}];else if(Array.isArray(o.content))s=this.mapInternalContentToResponsesInput(o.content,o.role==="assistant"?"assistant":"user");else{if(!(o.role==="assistant"&&!o.content&&o.functionCalls))throw new Error(`Invalid content type for role ${o.role}`);s=""}else o.role,s="";switch(o.role){case"system":n.push({type:"message",role:"system",content:s});break;case"user":n.push({type:"message",role:"user",content:s,name:o.name});break;case"assistant":if(o.content||o.functionCalls){let i={type:"message",role:"assistant",content:""};if(o.content&&(i.content=s),o.name&&(i.name=o.name),o.content&&n.push(i),o.functionCalls)for(let a of o.functionCalls)n.push({type:"function_call",call_id:a.id,name:a.function.name,arguments:typeof a.function.params=="object"?JSON.stringify(a.function.params):a.function.params||""})}break;case"function":n.push({type:"function_call_output",call_id:o.functionId,output:o.result});break;default:{let i=o.role;throw new Error(`Invalid role in chat prompt: ${i}`)}}}return n}createChatReq(e,t){let n=e.model,o={name:"/responses"},s=null,i=!1;if(e.chatPrompt){for(let h of e.chatPrompt)if(h.role==="system"&&typeof h.content=="string"){s=h.content,i=!0;break}}let a=s??this.config.systemPrompt??null,c=e.functions?.map(h=>({type:"function",name:h.name,description:h.description,parameters:h.parameters??{}})),u=[],l=pI(n),p=this.config.reasoningSummary;t?.showThoughts?p||(p="auto"):p=void 0;let d=this.config.reasoningEffort;if(t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"none":d=void 0;break;case"minimal":d="minimal";break;case"low":d="medium";break;case"medium":case"high":d="high";break;case"highest":d="xhigh";break}let g={model:n,input:"",instructions:a,tools:c?.length?c:void 0,tool_choice:e.functionCall==="none"||e.functionCall==="auto"||e.functionCall==="required"?e.functionCall:typeof e.functionCall=="object"&&e.functionCall.function?{type:"function",name:e.functionCall.function.name}:void 0,...l?{max_output_tokens:e.modelConfig?.maxTokens??this.config.maxTokens??void 0}:{...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty??void 0,frequency_penalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty??void 0,max_output_tokens:e.modelConfig?.maxTokens??this.config.maxTokens??void 0},stream:e.modelConfig?.stream??this.config.stream??!1,background:void 0,include:u.length>0?u:void 0,metadata:void 0,parallel_tool_calls:this.config.parallelToolCalls,previous_response_id:void 0,...d?{reasoning:{effort:d,summary:p}}:{},service_tier:this.config.serviceTier,store:this.config.store,text:void 0,truncation:void 0,user:this.config.user,seed:this.config.seed};this.config.user&&(g.user=this.config.user),this.config.parallelToolCalls!==void 0&&(g.parallel_tool_calls=this.config.parallelToolCalls),e.responseFormat?g.text={format:e.responseFormat.type==="json_schema"?{type:"json_schema",json_schema:e.responseFormat.schema}:{type:e.responseFormat.type}}:this.config.responseFormat&&(g.text={format:{type:this.config.responseFormat}}),this.config.seed&&(g.seed=this.config.seed);let m=e.chatPrompt?this.createResponsesReqInternalInput(e.chatPrompt,i):[];if(m.length>0)g.input=m;else if(e.chatPrompt&&e.chatPrompt.length===1&&e.chatPrompt[0]?.role==="user"&&e.chatPrompt[0]?.content&&typeof e.chatPrompt[0].content=="string"&&!a)g.input=e.chatPrompt[0].content;else if(m.length===0&&!a)throw new Error("Responses API request must have input or instructions.");let f=g.reasoning??{};if(this.config.reasoningEffort&&(f={...f,effort:this.config.reasoningEffort}),t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"none":f={};break;case"minimal":f={...f,effort:"minimal"};break;case"low":f={...f,effort:"medium"};break;case"medium":case"high":f={...f,effort:"high"};break;case"highest":f={...f,effort:"xhigh"};break}Object.keys(f).length>0&&f.effort?g.reasoning=f:g.reasoning=void 0;let x=g;return this.responsesReqUpdater&&(x=this.responsesReqUpdater(x)),[o,x]}createChatResp(e){let{id:t,output:n,usage:o}=e;this.tokensUsed=bn(o);let s={};for(let i of n??[])switch(i.type){case"message":s.id=i.id,s.content=Ll(i.content,t),s.finishReason=i.status==="completed"?"stop":"content_filter",s.citations=Ea(i.content);break;case"reasoning":s.id=i.id,i.encrypted_content?s.thought=i.encrypted_content:s.thought=i.summary.map(a=>typeof a=="object"?JSON.stringify(a):a).join(`
|
|
1126
1126
|
`);break;case"file_search_call":s.id=i.id,s.functionCalls=[{id:i.id,type:"function",function:{name:"file_search",params:{queries:i.queries,results:i.results}}}],s.finishReason="function_call";break;case"web_search_call":s.id=i.id,s.functionCalls=[{id:i.id,type:"function",function:{name:"web_search",params:{queries:i.queries}}}],s.finishReason="function_call";break;case"computer_call":s.id=i.id,s.functionCalls=[{id:i.id,type:"function",function:{name:"computer_use",params:{action:i.action}}}],s.finishReason="function_call";break;case"code_interpreter_call":s.id=i.id,s.functionCalls=[{id:i.id,type:"function",function:{name:"code_interpreter",params:{code:i.code,results:i.results}}}],s.finishReason="function_call";break;case"image_generation_call":s.id=i.id,s.functionCalls=[{id:i.id,type:"function",function:{name:"image_generation",params:{result:i.result}}}],s.finishReason="function_call";break;case"local_shell_call":s.id=i.id,s.functionCalls=[{id:i.id,type:"function",function:{name:"local_shell",params:{action:i.action}}}],s.finishReason="function_call";break;case"mcp_call":s.id=i.id,s.functionCalls=[{id:i.id,type:"function",function:{name:"mcp",params:{name:i.name,args:i.args,serverLabel:i.server_label,output:i.output,error:i.error}}}],s.finishReason="function_call";break;case"function_call":s.id=i.id,s.functionCalls=[{id:i.id,type:"function",function:{name:i.name,params:i.arguments}}],s.finishReason="function_call";break}return{results:[{...s,index:0}],remoteId:t}}createChatStreamResp=(e,t)=>{let n=e,o=t,s={index:0,id:"",content:"",finishReason:"stop"},i=typeof n.response?.id=="string"?n.response.id:void 0;switch(n.type){case"response.created":case"response.in_progress":case"response.queued":i=n.response.id,s.id=`${n.response.id}_res_0`;break;case"response.output_item.added":switch(n.item.type){case"message":s.id=n.item.id,s.content=Ll(n.item.content,n.item.id),s.citations=Ea(n.item.content);break;case"function_call":s.id=n.item.id,s.functionCalls=[{id:n.item.id,type:"function",function:{name:n.item.name,params:n.item.arguments}}];break;case"file_search_call":{let a=n.item;s.id=n.item.id,s.functionCalls=[{id:a.id,type:"function",function:{name:"file_search",params:{queries:a.queries||[],results:a.results?.map(c=>({fileId:c.file_id,filename:c.filename,score:c.score,text:c.text,attributes:c.attributes}))}}}]}break;case"web_search_call":{let a=n.item;s.id=n.item.id,s.functionCalls=[{id:a.id,type:"function",function:{name:"web_search",params:{queries:a.queries||[]}}}]}break;case"computer_call":{let a=n.item;s.id=n.item.id,s.functionCalls=[{id:a.id,type:"function",function:{name:"computer_use",params:{action:a.action||{}}}}]}break;case"code_interpreter_call":{let a=n.item;s.id=n.item.id,s.functionCalls=[{id:a.id,type:"function",function:{name:"code_interpreter",params:{code:a.code||"",results:a.results}}}]}break;case"image_generation_call":{let a=n.item;s.id=n.item.id,s.functionCalls=[{id:a.id,type:"function",function:{name:"image_generation",params:{result:a.result}}}]}break;case"local_shell_call":{let a=n.item;s.id=n.item.id,s.functionCalls=[{id:a.id,type:"function",function:{name:"local_shell",params:{action:a.action||{}}}}]}break;case"mcp_call":{let a=n.item;s.id=n.item.id,s.functionCalls=[{id:a.id,type:"function",function:{name:"mcp",params:{name:a.name||"",args:a.args||"",serverLabel:a.server_label||"",output:a.output,error:a.error}}}]}break}break;case"response.content_part.added":s.id=n.item_id,s.content=Ll([n.part],n.item_id),s.citations=Ea([n.part]);break;case"response.output_text.delta":s.id=n.item_id,s.content=n.delta;break;case"response.output_text.done":break;case"response.function_call_arguments.delta":s.id=n.item_id,s.functionCalls=[{id:n.item_id,type:"function",function:{name:"",params:n.delta}}];break;case"response.reasoning_summary_text.delta":s.id=n.item_id,s.thought=n.delta;break;case"response.file_search_call.in_progress":case"response.file_search_call.searching":s.id=n.item_id,s.finishReason="function_call";break;case"response.file_search_call.completed":s.id=n.item_id,s.finishReason="function_call";break;case"response.web_search_call.in_progress":case"response.web_search_call.searching":s.id=n.item_id,s.finishReason="function_call";break;case"response.web_search_call.completed":s.id=n.item_id,s.finishReason="function_call";break;case"response.image_generation_call.in_progress":case"response.image_generation_call.generating":s.id=n.item_id,s.finishReason="function_call";break;case"response.image_generation_call.completed":s.id=n.item_id,s.finishReason="function_call";break;case"response.image_generation_call.partial_image":s.id=n.item_id,s.finishReason="function_call";break;case"response.mcp_call.in_progress":s.id=n.item_id,s.finishReason="function_call";break;case"response.mcp_call.arguments.delta":s.id=n.item_id,s.functionCalls=[{id:n.item_id,type:"function",function:{name:"",params:n.delta}}];break;case"response.mcp_call.arguments.done":s.id=n.item_id,s.functionCalls=[{id:n.item_id,type:"function",function:{name:"",params:n.arguments}}];break;case"response.mcp_call.completed":case"response.mcp_call.failed":s.id="mcp_call_event",s.finishReason="function_call";break;case"response.mcp_list_tools.in_progress":case"response.mcp_list_tools.completed":case"response.mcp_list_tools.failed":s.id="mcp_list_tools_event",s.finishReason="function_call";break;case"response.output_item.done":switch(n.item.type){case"message":if(s.id=n.item.id,s.finishReason=n.item.status==="completed"?"stop":"error",!s.citations||s.citations.length===0){let a=Ea(n.item.content||[]);a&&(s.citations=a)}break;case"function_call":case"file_search_call":case"web_search_call":case"computer_call":case"code_interpreter_call":case"image_generation_call":case"local_shell_call":case"mcp_call":s.id=n.item.id,s.finishReason="function_call";break}break;case"response.completed":this.tokensUsed=bn(n.response.usage),i=n.response.id,s.id=`${n.response.id}_completed`,s.finishReason="stop";break;case"response.failed":i=n.response.id,s.id=`${n.response.id}_failed`,s.finishReason="error";break;case"response.incomplete":i=n.response.id,s.id=`${n.response.id}_incomplete`,s.finishReason="length";break;case"error":s.id="error",s.content=`Error: ${n.message}`,s.finishReason="error";break;default:s.id="unknown";break}return i?o.remoteId=i:i=o.remoteId,{results:[s],remoteId:i}};createEmbedReq(e){let t=e.embedModel;if(!t)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let n={name:"/embeddings"},o={model:t,input:e.texts,dimensions:this.config.dimensions};return[n,o]}},Ll=(r,e)=>{let t=r.filter(n=>n.type==="refusal");if(t.length>0){let n=t.map(o=>o.refusal).join(`
|
|
1127
1127
|
`);throw new qe(n,void 0,e)}return r.filter(n=>n.type==="output_text").map(n=>n.text).join(`
|
|
1128
1128
|
`)};function Ea(r){let e=[];for(let t of r??[])if(t?.type==="output_text"&&Array.isArray(t.annotations))for(let n of t.annotations)n&&n.type==="url_citation"&&typeof n.url=="string"&&e.push({url:n.url,title:n.title,description:n.description});return e.length?e:void 0}var Gs=()=>({model:"gpt-4o",embedModel:"text-embedding-ada-002",temperature:.7,topP:1,stream:!0}),ex=()=>({...Gs(),model:"gpt-4o",temperature:.5}),tx=()=>({...Gs(),model:"gpt-4o",temperature:.9}),$s=class extends Xe{constructor({apiKey:e,config:t,options:n,apiURL:o,modelInfo:s=[],models:i,responsesReqUpdater:a,supportFor:c={functions:!0,streaming:!0,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}){if(!e||e==="")throw new Error("OpenAI API key not set");let u=new wo(t,n?.streamingUsage??!0,a),l=i?.map(p=>{let d=p,g=d?.config;if(!g)return p;let m={};g.maxTokens!==void 0&&(m.maxTokens=g.maxTokens),g.temperature!==void 0&&(m.temperature=g.temperature),g.topP!==void 0&&(m.topP=g.topP),g.presencePenalty!==void 0&&(m.presencePenalty=g.presencePenalty),g.frequencyPenalty!==void 0&&(m.frequencyPenalty=g.frequencyPenalty);let f=g.stopSequences??g.stop;f!==void 0&&(m.stopSequences=f),g.n!==void 0&&(m.n=g.n),g.stream!==void 0&&(m.stream=g.stream);let x={...d};Object.keys(m).length>0&&(x.modelConfig={...d.modelConfig??{},...m});let h=g?.thinking?.thinkingTokenBudget;if(typeof h=="number"){let A=[["minimal",200],["low",800],["medium",5e3],["high",1e4],["highest",24500]],b="minimal",C=Number.POSITIVE_INFINITY;for(let[I,w]of A){let S=Math.abs(h-w);S<C&&(C=S,b=I)}x.thinkingTokenBudget=b}return g?.thinking?.includeThoughts!==void 0&&(x.showThoughts=!!g.thinking.includeThoughts),x});super(u,{name:"OpenAI",apiURL:o||"https://api.openai.com/v1",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:s,defaults:{model:t.model??"gpt-4o",embedModel:t.embedModel??"text-embedding-ada-002"},options:n,supportFor:c,models:l??i})}},So=class extends $s{constructor({apiKey:e,config:t,options:n,models:o,modelInfo:s}){if(!e||e==="")throw new Error("OpenAI API key not set");s=[...Kr,...s??[]];let i=a=>{let c=at({model:a,modelInfo:s,models:o});return{functions:!0,streaming:!0,hasThinkingBudget:c?.supported?.thinkingBudget??!1,hasShowThoughts:c?.supported?.showThoughts??!1,structuredOutputs:c?.supported?.structuredOutputs??!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}};super({apiKey:e,config:{...Gs(),...t},options:n,modelInfo:s,models:o,supportFor:i})}};var Nl=()=>structuredClone({model:"openrouter/auto",...xe()}),ko=class extends Qe{constructor({apiKey:e,config:t,options:n,models:o,modelInfo:s,referer:i,title:a}){if(!e||e==="")throw new Error("OpenRouter API key not set");let c={...Nl(),...t},u={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},l=s??[];super({apiKey:e,config:c,options:n,apiURL:"https://openrouter.ai/api/v1",modelInfo:l,models:o,supportFor:u}),super.setName("OpenRouter"),super.setHeaders(async()=>{let p={Authorization:`Bearer ${e}`};return i&&(p["HTTP-Referer"]=i),a&&(p["X-Title"]=a),p})}};async function Pa(r,e,t={}){if(typeof r=="string")return[{type:"text",text:r}];if(!Array.isArray(r))return[{type:"text",text:String(r)}];let n=e.getFeatures(),o=[];for(let s of r)try{switch(s.type){case"text":o.push({type:"text",text:s.text});break;case"image":if(n.media.images.supported)s.altText?o.push({type:"text",text:`[Image: ${s.altText}]`}):o.push({type:"text",text:"[Image content]"});else if(s.altText)o.push({type:"text",text:s.altText});else if(t.imageToText)try{let i=await t.imageToText(s.image);o.push({type:"text",text:i})}catch(i){throw new Bt(i,"image","vision analysis")}else switch(t.fallbackBehavior){case"error":throw new yt("Images",e.getName(),!1);case"skip":continue;default:o.push({type:"text",text:"[Image content not supported by this provider]"})}break;case"audio":if(n.media.audio.supported)s.transcription?o.push({type:"text",text:s.transcription}):o.push({type:"text",text:"[Audio content]"});else if(s.transcription)o.push({type:"text",text:s.transcription});else if(t.audioToText)try{let i=await t.audioToText(s.data,s.format);o.push({type:"text",text:i})}catch(i){throw new Bt(i,"audio","transcription")}else switch(t.fallbackBehavior){case"error":throw new yt("Audio",e.getName(),!1);case"skip":continue;case"degrade":o.push({type:"text",text:"[Audio content not supported by this provider]"})}break;case"file":if(n.media.files.supported)s.extractedText?o.push({type:"text",text:s.extractedText}):o.push({type:"text",text:`[File: ${s.filename}]`});else if(s.extractedText)o.push({type:"text",text:s.extractedText});else if(t.fileToText)try{let i=await t.fileToText(s.data,s.mimeType);o.push({type:"text",text:i})}catch(i){throw new Bt(i,"file","text extraction")}else switch(t.fallbackBehavior){case"error":throw new yt("Files",e.getName(),!1);case"skip":continue;default:o.push({type:"text",text:`[File: ${s.filename} - content not accessible by this provider]`})}break;case"url":if(n.media.urls.supported)s.cachedContent?o.push({type:"text",text:s.cachedContent}):o.push({type:"text",text:`[Link: ${s.url}${s.title?` - ${s.title}`:""}]`});else if(s.cachedContent)o.push({type:"text",text:s.cachedContent});else if(t.urlToText)try{let i=await t.urlToText(s.url);o.push({type:"text",text:i})}catch(i){throw new Bt(i,"url","content fetching")}else switch(t.fallbackBehavior){case"error":throw new yt("URLs",e.getName(),!1);case"skip":continue;case"degrade":o.push({type:"text",text:`[Link: ${s.url}${s.title?` - ${s.title}`:""}]`})}break;default:typeof s=="object"&&s.text?o.push({type:"text",text:s.text}):o.push({type:"text",text:String(s)})}}catch(i){throw i instanceof yt||i instanceof Bt?i:new Bt(i,s.type||"unknown","content processing")}return o}function nx(r){let e=!1,t=!1,n=!1,o=!1;for(let s of r)if(s.role==="user"&&Array.isArray(s.content))for(let i of s.content)switch(i.type){case"image":e=!0;break;case"audio":t=!0;break;case"file":n=!0;break;case"url":o=!0;break}return{hasImages:e,hasAudio:t,hasFiles:n,hasUrls:o}}var Us=()=>structuredClone({model:"reka-core",...xe()}),ox=()=>structuredClone({...Us(),model:"reka-core"}),sx=()=>structuredClone({model:"reka-core",...rt()}),ix=()=>({...Us(),model:"reka-flash"}),Dl=class{constructor(e){this.config=e}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,topP:e.topP,n:e.n,stream:e.stream}}createChatReq=e=>{let t=e.model;if(!e.chatPrompt||e.chatPrompt.length===0)throw new Error("Chat prompt is empty");let n={name:"/chat/completions"},o=dI(e),s=e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,i=e.modelConfig?.stream??this.config.stream,a={model:t,messages:o,max_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},top_k:e.modelConfig?.n??this.config.n,...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},stop:e.modelConfig?.stopSequences??this.config.stop,presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,...s?{frequency_penalty:s}:{},...i?{stream:!0}:{}};return[n,a]};createChatResp=e=>{let{id:t,usage:n,responses:o}=e;return this.tokensUsed=n?{promptTokens:n.input_tokens,completionTokens:n.output_tokens,totalTokens:n.input_tokens+n.output_tokens}:void 0,{results:o.map((i,a)=>{let c=rx(i.finish_reason),u;return typeof i.message.content=="string"?u=i.message.content:u=i.message.content.text,{index:a,id:`${t}`,content:u,finishReason:c}}),remoteId:t}};createChatStreamResp=e=>{let{id:t,usage:n,responses:o}=e;return this.tokensUsed=n?{promptTokens:n.input_tokens,completionTokens:n.output_tokens,totalTokens:n.input_tokens+n.output_tokens}:void 0,{results:o.map((i,a)=>{let c=rx(i.finish_reason),u;return typeof i.chunk.content=="string"?u=i.chunk.content:u=i.chunk.content.text,{index:a,id:`${t}`,content:u,finishReason:c}})}}},rx=r=>{switch(r){case"stop":return"stop";case"context":return"length";case"length":return"length"}};function dI(r){return r.chatPrompt.map(e=>{switch(e.role){case"system":return{role:"user",content:e.content};case"user":return Array.isArray(e.content)?{role:"user",content:e.content.map(t=>{switch(t.type){case"text":return{type:"text",text:t.text};case"image":throw new Error("Image type not supported");default:throw new Error("Invalid content type")}})}:{role:"user",content:e.content};case"assistant":if(Array.isArray(e.content))return{role:"assistant",content:e.content.map(t=>{switch(t.type){case"text":return{type:"text",text:t.text};case"image":throw new Error("Image type not supported");default:throw new Error("Invalid content type")}})};if(!e.content)throw new Error("Assistant content is empty");return{role:"user",content:e.content};default:throw new Error("Invalid role")}})}var vo=class extends Xe{constructor({apiKey:e,config:t,options:n,apiURL:o,modelInfo:s=uo,models:i}){if(!e||e==="")throw new Error("Reka API key not set");let a={...Us(),...t},c=new Dl(a);super(c,{name:"Reka",apiURL:o||"https://api.reka.ai/v1/chat",headers:async()=>({"X-Api-Key":e}),modelInfo:s,defaults:{model:a.model},options:n,supportFor:{functions:!0,streaming:!0,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},models:i})}};var Fa=class{providers;processingServices;config;constructor(e){this.providers=[e.providers.primary,...e.providers.alternatives],this.processingServices=e.processing,this.config=e.routing}async chat(e,t={}){let n=await this.selectProviderWithDegradation(e,t.routingOptions||{}),o=await this.preprocessRequest(e,n.provider,t.processingOptions);try{return{response:await n.provider.chat(o,t),routing:n}}catch(s){if(s instanceof yt&&t.fallbackProviders?.length)return await this.tryFallbackProviders(e,t.fallbackProviders,t);throw s}}async preprocessRequest(e,t,n){let o={...n,fallbackBehavior:n?.fallbackBehavior||"degrade",imageToText:n?.imageToText||this.processingServices.imageToText,audioToText:n?.audioToText||this.processingServices.audioToText,fileToText:n?.fileToText||this.processingServices.fileToText,urlToText:n?.urlToText||this.processingServices.urlToText},s=[];for(let i of e.chatPrompt)if(i.role==="user"&&Array.isArray(i.content)){let a=await Pa(i.content,t,o);a.every(u=>u.type==="text")&&a.length===1?s.push({...i,content:a[0].text}):s.push({...i,content:a.map(u=>({type:"text",text:u.text}))})}else s.push(i);return{...e,chatPrompt:s}}async selectProviderWithDegradation(e,t){let n=or(e),o=[],s=[],i=[];try{let a=ba(e,this.providers,{requireExactMatch:t.requireExactMatch??this.config.capability.requireExactMatch,allowDegradation:t.allowDegradation??this.config.capability.allowDegradation}),c=a.getFeatures();return n.hasImages&&!c.media.images.supported&&(s.push("Images will be converted to text descriptions"),o.push("Image-to-text conversion")),n.hasAudio&&!c.media.audio.supported&&(s.push("Audio will be transcribed to text"),o.push("Audio-to-text transcription")),n.hasFiles&&!c.media.files.supported&&(s.push("File content will be extracted to text"),o.push("File-to-text extraction")),n.hasUrls&&!c.media.urls.supported&&(s.push("URL content will be pre-fetched"),o.push("URL content fetching")),n.requiresStreaming&&!c.streaming&&i.push("Streaming not supported - will use non-streaming mode"),n.requiresCaching&&!c.caching.supported&&i.push("Content caching not supported"),{provider:a,processingApplied:o,degradations:s,warnings:i}}catch(a){throw new Error(`Provider selection failed: ${a instanceof Error?a.message:"Unknown error"}`)}}async tryFallbackProviders(e,t,n){for(let o of t)try{let s={provider:o,processingApplied:["Fallback provider selection"],degradations:["Using fallback provider due to primary provider failure"],warnings:[]},i=await this.preprocessRequest(e,o,{fallbackBehavior:"degrade"});return{response:await o.chat(i,n),routing:s}}catch{}throw new Error("All fallback providers failed")}async getRoutingRecommendation(e){return await this.selectProviderWithDegradation(e,{})}async validateRequest(e){let t=or(e),n=[],o=[];try{let s=await this.selectProviderWithDegradation(e,{});return s.degradations.length>0&&(n.push(...s.degradations),o.push("Consider using a provider that natively supports all media types")),s.warnings.length>0&&n.push(...s.warnings),t.hasImages&&this.processingServices.imageToText===void 0&&(this.providers.some(a=>a.getFeatures().media.images.supported)||(n.push("No image processing service available and no providers support images"),o.push("Add imageToText processing service or use image-capable provider"))),t.hasAudio&&this.processingServices.audioToText===void 0&&(this.providers.some(a=>a.getFeatures().media.audio.supported)||(n.push("No audio processing service available and no providers support audio"),o.push("Add audioToText processing service or use audio-capable provider"))),{canHandle:n.length===0||s.degradations.length>0,issues:n,recommendations:o}}catch(s){return{canHandle:!1,issues:[`Cannot route request: ${s instanceof Error?s.message:"Unknown error"}`],recommendations:["Add more providers or processing services to handle this request"]}}}getRoutingStats(){let e={};for(let t of this.providers){let n=t.getFeatures(),o=t.getName();n.functions&&(e.Functions=e.Functions||[],e.Functions.push(o)),n.streaming&&(e.Streaming=e.Streaming||[],e.Streaming.push(o)),n.media.images.supported&&(e.Images=e.Images||[],e.Images.push(o)),n.media.audio.supported&&(e.Audio=e.Audio||[],e.Audio.push(o)),n.media.files.supported&&(e.Files=e.Files||[],e.Files.push(o)),n.media.urls.supported&&(e.URLs=e.URLs||[],e.URLs.push(o)),n.caching.supported&&(e.Caching=e.Caching||[],e.Caching.push(o))}return{totalProviders:this.providers.length,capabilityMatrix:e,recommendedProvider:this.providers[0]?.getName()||"None"}}};var $l=()=>structuredClone({model:"meta-llama/Llama-3.3-70B-Instruct-Turbo",...xe()}),Oo=class extends Qe{constructor({apiKey:e,config:t,options:n,models:o,modelInfo:s}){if(!e||e==="")throw new Error("Together API key not set");let i={...$l(),...t};s=[...po,...s??[]];let a={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0};super({apiKey:e,config:i,options:n,apiURL:"https://api.together.xyz/v1",modelInfo:s,models:o,supportFor:a}),super.setName("Together"),this.setBatchAudioConfig({transcriptionModel:"openai/whisper-large-v3",speechModel:"canopylabs/orpheus-3b-0.1-ft",speechVoice:"tara",speechFormat:"mp3"})}};var Ul=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...xe()}),ax=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...rt()}),Gl=class{constructor(e,t){this.config=e;this.engine=t}tokensUsed;engine;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,endSequences:e.endSequences,stream:e.stream,n:e.n}}createChatReq(e){let t=e.model,n=e.chatPrompt.map(a=>{if(a.role==="function")return{role:"function",name:a.functionId,content:typeof a.result=="string"?a.result:JSON.stringify(a.result)};let c="";typeof a.content=="string"?c=a.content:Array.isArray(a.content)&&(c=a.content.filter(l=>l.type==="text").map(l=>l.text).join(`
|