import{d as e}from"./io-CH2g3vsv-JJtUIa_U.mjs";import{n as t,r as n,t as r}from"./models-config-BmpSQJQF-BL6S5qAo.mjs";import{s as i}from"./oauth-25y2wc7K.mjs";import{a,c as o,i as s,l as c,n as l,r as u,t as d}from"./provider-stream-COLujAAo-Dpq1DSIk.mjs";let f=null;function p(){return f??=import(`./pi-model-discovery-runtime-PrjWOmsV.mjs`),f}function m(e,t=4096){return typeof e!=`number`||!Number.isFinite(e)||e<=0?t:Math.min(t,e)}function h(e){return!!e&&typeof e==`object`&&!Array.isArray(e)}function g(t){return t.api!==`openai-responses`&&t.api!==`azure-openai-responses`&&t.api!==`openai-codex-responses`?!1:e({provider:t.provider,api:t.api,baseUrl:t.baseUrl,capability:`image`,transport:`media-understanding`}).usesKnownNativeOpenAIRoute}function _(e){if(!Array.isArray(e))return e;let t=e.filter(e=>e!==`reasoning.encrypted_content`);return t.length>0?t:void 0}function v(e,t){if(!h(e))return;let n={...e};delete n.reasoning,delete n.reasoning_effort;let r=_(n.include);return r===void 0?delete n.include:n.include=r,g(t)&&(n.reasoning={effort:`none`}),n}function y(e){return e instanceof Error&&/^Image model returned no text\b/.test(e.message)}async function b(e){await r(e.cfg,e.agentDir);let{discoverAuthStorage:i,discoverModels:a}=await p(),s=i(e.agentDir),c=a(s,e.agentDir),l=n(e.provider,e.model),u=c.find(l.provider,l.model);if(!u)throw Error(`Unknown model: ${l.provider}/${l.model}`);if(!u.input?.includes(`image`))throw Error(`Model does not support images: ${e.provider}/${e.model}`);let d=t(await o({model:u,cfg:e.cfg,agentDir:e.agentDir,profileId:e.profile,preferredProfile:e.preferredProfile,store:e.authStore}),u.provider);return s.setRuntimeApiKey(u.provider,d),{apiKey:d,model:u}}function x(e,t){return{systemPrompt:e,messages:[{role:`user`,content:t.map(e=>({type:`image`,data:e.buffer.toString(`base64`),mimeType:e.mime??`image/jpeg`})),timestamp:Date.now()}]}}async function S(e){let t=[];for(let[n,r]of e.images.entries()){let i=e.images.length>1?`${e.prompt}\n\nDescribe image ${n+1} of ${e.images.length} independently.`:e.prompt,o=await a({apiKey:e.apiKey,prompt:i,imageDataUrl:`data:${r.mime??`image/jpeg`};base64,${r.buffer.toString(`base64`)}`,modelBaseUrl:e.modelBaseUrl});t.push(e.images.length>1?`Image ${n+1}:\n${o.trim()}`:o.trim())}return{text:t.join(`

`).trim(),model:e.modelId}}function C(e){return e instanceof Error&&/^Unknown model:/i.test(e.message)}function w(e,t){let n=e.models?.providers?.[t];if(typeof n?.baseUrl==`string`&&n.baseUrl.trim())return n.baseUrl.trim()}async function T(e){return{apiKey:t(await c({provider:e.provider,cfg:e.cfg,profileId:e.profile,preferredProfile:e.preferredProfile,agentDir:e.agentDir}),e.provider),modelBaseUrl:w(e.cfg,e.provider)}}async function E(e){let t=e.prompt??`Describe the image.`,n,r;try{let t=await b(e);n=t.apiKey,r=t.model}catch(n){if(!s(e.provider,e.model)||!C(n))throw n;let r=await T(e);return await S({apiKey:r.apiKey,modelId:e.model,modelBaseUrl:r.modelBaseUrl,prompt:t,images:e.images})}if(s(r.provider,r.id))return await S({apiKey:n,modelId:r.id,modelBaseUrl:r.baseUrl,prompt:t,images:e.images});d({model:r,cfg:e.cfg,agentDir:e.agentDir});let a=x(t,e.images),o=new AbortController,c=typeof e.timeoutMs==`number`&&Number.isFinite(e.timeoutMs)&&e.timeoutMs>0?setTimeout(()=>o.abort(),e.timeoutMs):void 0,f=m(r.maxTokens,e.maxTokens??512),p=async e=>await i(r,a,{apiKey:n,maxTokens:f,signal:o.signal,...e?{onPayload:e}:{}});try{let e=await p();try{return{text:l({message:e,provider:r.provider,model:r.id}),model:r.id}}catch(t){if(!y(t)||!u(e))throw t}return{text:l({message:await p(v),provider:r.provider,model:r.id}),model:r.id}}finally{clearTimeout(c)}}async function D(e){return await E({images:[{buffer:e.buffer,fileName:e.fileName,mime:e.mime}],model:e.model,provider:e.provider,prompt:e.prompt,maxTokens:e.maxTokens,timeoutMs:e.timeoutMs,profile:e.profile,preferredProfile:e.preferredProfile,authStore:e.authStore,agentDir:e.agentDir,cfg:e.cfg})}export{D as describeImageWithModel,E as describeImagesWithModel};