"use strict";exports.id=4066,exports.ids=[4066],exports.modules={64066:(a,b,c)=>{c.a(a,async(a,d)=>{try{c.d(b,{pickBestImage:()=>m});var e=c(73024),f=c(76760),g=c.n(f),h=c(77598),i=c.n(h),j=c(3724),k=c(53905),l=a([j]);async function m(a){if(0===a.candidates.length)return{pickIndex:-1,reason:"no candidates",via:"fallback"};if(1===a.candidates.length)return{pickIndex:0,reason:"only one candidate",via:"fallback"};try{return await n(a)}catch(d){let b=d instanceof Error?d.message:String(d),c=o(a);return{...c,reason:`agent judge failed (${b}); ${c.reason}`}}}async function n(a){let b=await (0,k.getRoot)(a.rootId);if(!b)throw Error(`unknown rootId: ${a.rootId}`);let c=g().join(b.path,".reflex",".tmp",`judge-${i().randomBytes(6).toString("hex")}`);await e.promises.mkdir(c,{recursive:!0});let d=(await Promise.all(a.candidates.map(async(a,b)=>{try{let d=await fetch(a.thumb,{signal:AbortSignal.timeout(1e4)});if(!d.ok)return null;let f=d.headers.get("content-type")?.split(";")[0]?.trim()||"image/jpeg";if(!f.startsWith("image/"))return null;let h=await d.arrayBuffer();if(h.byteLength>5242880)return null;let i=function(a){switch(a.toLowerCase()){case"image/png":return"png";case"image/jpeg":case"image/jpg":return"jpg";case"image/webp":return"webp";case"image/gif":return"gif";case"image/avif":return"avif";case"image/svg+xml":return"svg";default:return"img"}}(f),j=g().join(c,`cand-${b}.${i}`);return await e.promises.writeFile(j,Buffer.from(h)),{index:b,path:j,mime:f,size:h.byteLength}}catch{return null}}))).filter(a=>null!==a);if(0===d.length)return await p(c),o(a);let f=[`Course/topic: ${a.context}`,`Search query: "${a.query}"`,`What the image should depict: ${a.alt}`,"",`On disk you have ${d.length} search-result candidates. OPEN EACH ONE through the Read tool — it returns the image as vision content so you can see it:`,""];for(let b=0;b<d.length;b++){let c=a.candidates[d[b].index],e="(unknown)";try{e=new URL(c.url).hostname}catch{}f.push(`  [${b}] ${d[b].path}  (source: ${e}, author: ${c.attribution.name})`)}f.push("","Read each one (Read tool, one at a time), look at what is REALLY in the image, and pick the ONE that BEST works as an educational illustration.","","Criteria (in order of importance):",'  1. The image ACTUALLY depicts what the query asks for. For example, if the query is "plaster sphere" and the image shows a hand — reject it.',"  2. Quality is textbook-grade: not clipart, not a meme, not a logo, not blurry.","  3. The context matches the course topic.","  4. If ALL candidates are unsuitable — return pick: -1, and the system will generate an AI illustration instead.","",`At the VERY END of your response, output ONE line of JSON: {"pick": number from 0 to ${d.length-1} OR -1, "reason": "short rationale"}`,"Before that line you may think out loud / describe what you see — only the final JSON line matters.");let h=d.map(a=>({name:g().basename(a.path),absPath:a.path,size:a.size,mime:a.mime})),l=-1,m="";try{let b=await (0,j.runHeadlessAgent)({rootId:a.rootId,prompt:f.join("\n"),label:`[images.pickBest] ${a.query}`,timeoutMs:3e5,attachments:h}),c=function(a,b){let c=a.trim().split(/\r?\n/);for(let a=c.length-1;a>=0;a--){let d=c[a].trim();if(d.startsWith("{"))try{let a=JSON.parse(d),c="number"==typeof a.pick&&Number.isInteger(a.pick)?a.pick:NaN;if(Number.isFinite(c)&&(-1===c||c>=0&&c<b))return{pickIndex:c,reason:"string"==typeof a.reason?a.reason:""}}catch{}}let d=/\{[^{}]*"pick"[^{}]*\}/.exec(a);if(d)try{let a=JSON.parse(d[0]),c="number"==typeof a.pick&&Number.isInteger(a.pick)?a.pick:NaN;if(Number.isFinite(c))return{pickIndex:-1===c||c>=0&&c<b?c:0,reason:"string"==typeof a.reason?a.reason:""}}catch{}return{pickIndex:0,reason:"could not parse pick from agent reply"}}(b.text,d.length);l=c.pickIndex,m=c.reason}finally{await p(c)}return -1===l?{pickIndex:-1,reason:m||"agent rejected all",via:"agent"}:l<0||l>=d.length?{pickIndex:d[0].index,reason:`agent returned invalid pick (${l}); falling back to first usable`,via:"agent"}:{pickIndex:d[l].index,reason:m,via:"agent"}}function o(a){let b=/(wikipedia|wikimedia|britannica|mdn|github|stanford|mit\.edu|nature\.com|nasa\.gov|noaa\.gov|smithsonian)/i;for(let c=0;c<a.candidates.length;c++){let d=a.candidates[c];try{let a=new URL(d.url).hostname;if(b.test(a))return{pickIndex:c,reason:`trusted domain ${a}`,via:"heuristic"}}catch{}}return{pickIndex:0,reason:"no trusted domain; first result",via:"heuristic"}}async function p(a){try{await e.promises.rm(a,{recursive:!0,force:!0})}catch{}}j=(l.then?(await l)():l)[0],d()}catch(a){d(a)}})}};