// @kodax-ai/kodax — bundled distribution. See docs/ADR.md ADR-022 + ADR-024.
import{a as r}from"./chunk-V4WSBIXB.js";var N="\x1B[?2004h",j="\x1B[?2004l",b=!1,f=!1;function C(){if(!f)try{process.stdout.write(N),f=!0}catch{}}r(C,"enableBracketedPasteMode");function T(){if(f)try{process.stdout.write(j)}catch{}finally{f=!1}}r(T,"disableBracketedPasteMode");function L(){return f}r(L,"isBracketedPasteModeEnabled");function O(){if(b)return;b=!0;let e=r(()=>T(),"handler");process.once("beforeExit",e),process.once("exit",e),process.once("SIGINT",()=>{e(),process.kill(process.pid,"SIGINT")}),process.once("SIGTERM",()=>{e(),process.kill(process.pid,"SIGTERM")}),process.once("uncaughtException",t=>{throw e(),t})}r(O,"installBracketedPasteShutdownGuard");import{readFile as ae}from"node:fs/promises";import{spawn as E}from"node:child_process";import{mkdtemp as F,readFile as I,rm as z}from"node:fs/promises";import{tmpdir as W}from"node:os";import k from"node:path";var P=5e3;async function p(){switch(process.platform){case"darwin":return X();case"win32":return q();case"linux":return J();default:return null}}r(p,"readClipboardImage");async function _(e){let t=await F(k.join(W(),"kodax-clip-")),a=k.join(t,"clip.png");try{return await e(a)}finally{await z(t,{recursive:!0,force:!0}).catch(()=>{})}}r(_,"runWithTempPng");async function X(){return _(async e=>{let t=`set png_data to (the clipboard as \xABclass PNGf\xBB)
set fh to open for access POSIX file "${e.replace(/"/g,'\\"')}" with write permission
write png_data to fh
close access fh`;if(!await M("osascript",["-e",t]))return null;try{let n=await I(e);return n.length>0?n:null}catch{return null}})}r(X,"readMacClipboard");async function q(){return _(async e=>{let t=["Add-Type -AssemblyName System.Drawing | Out-Null","$img = Get-Clipboard -Format Image","if ($null -eq $img) { exit 1 }",`$img.Save('${e.replace(/'/g,"''")}', [System.Drawing.Imaging.ImageFormat]::Png)`].join("; ");if(!await M("powershell",["-NoProfile","-Command",t]))return null;try{let n=await I(e);return n.length>0?n:null}catch{return null}})}r(q,"readWinClipboard");async function J(){let e=await Y();return e||Z()}r(J,"readLinuxClipboard");async function Y(){return S("wl-paste",["--type","image/png"])}r(Y,"readLinuxWaylandClipboard");async function Z(){return S("xclip",["-selection","clipboard","-t","image/png","-o"])}r(Z,"readLinuxX11Clipboard");function M(e,t){return new Promise(a=>{let n=!1,i=E(e,t,{stdio:"ignore"}),o=setTimeout(()=>{if(!n){n=!0;try{i.kill()}catch{}a(!1)}},P);i.once("error",()=>{n||(n=!0,clearTimeout(o),a(!1))}),i.once("exit",s=>{n||(n=!0,clearTimeout(o),a(s===0))})})}r(M,"runCommand");function S(e,t){return new Promise(a=>{let n=!1,i=[],o;try{o=E(e,t,{stdio:["ignore","pipe","ignore"]})}catch{a(null);return}let s=setTimeout(()=>{if(!n){n=!0;try{o.kill()}catch{}a(null)}},P);o.stdout?.on("data",l=>i.push(l)),o.once("error",()=>{n||(n=!0,clearTimeout(s),a(null))}),o.once("exit",l=>{if(n)return;if(n=!0,clearTimeout(s),l!==0){a(null);return}let c=Buffer.concat(i);a(c.length>0?c:null)})})}r(S,"collectStdoutBytes");import{Jimp as H}from"jimp";var g=2e3,d=Math.floor(3.75*1024*1024),u=class extends Error{static{r(this,"ImageResizeError")}constructor(t){super(t),this.name="ImageResizeError"}},U=[80,60,40];async function h(e){if(e.length===0)throw new u("Empty image buffer.");let t;try{t=await H.read(e)}catch(c){let m=c instanceof Error?c.message:String(c);throw new u(`Failed to decode image: ${m}`)}let{width:a,height:n}=t.bitmap,i=Math.max(a,n);if(i>g){let c=g/i;t.resize({w:Math.max(1,Math.round(a*c)),h:Math.max(1,Math.round(n*c))})}let o=t.bitmap.width,s=t.bitmap.height,l=await t.getBuffer("image/png");if(l.length<=d)return{buffer:Buffer.from(l),mediaType:"image/png",width:o,height:s};for(let c of U){let m=await t.getBuffer("image/jpeg",{quality:c});if(m.length<=d)return{buffer:Buffer.from(m),mediaType:"image/jpeg",width:o,height:s}}throw new u(`Image still exceeds budget (${d} bytes raw) after PNG and JPEG q40 compression. Try a smaller image or take a fresh screenshot of a smaller region.`)}r(h,"normalizePastedImage");import{mkdir as V,readdir as K,stat as Q,unlink as v,writeFile as ee}from"node:fs/promises";import{tmpdir as te}from"node:os";import w from"node:path";import{createHash as re}from"node:crypto";var B="KODAX_PASTE_TMP_DIR",A=1440*60*1e3;function D(){return process.env[B]??w.join(te(),"kodax-paste")}r(D,"resolvePasteTmpDir");async function x(e){let t=D();await V(t,{recursive:!0});let a=e.mediaType==="image/jpeg"?".jpg":".png",i=`paste-${re("sha256").update(e.buffer).digest("hex").slice(0,16)}${a}`,o=w.join(t,i);return await ee(o,e.buffer),{type:"image",path:o,mediaType:e.mediaType}}r(x,"persistImageAsBlock");async function ne(e=Date.now()){let t=D(),a;try{a=await K(t)}catch{return 0}let n=e-A,i=0;return await Promise.all(a.filter(o=>o.startsWith("paste-")).map(async o=>{let s=w.join(t,o);try{(await Q(s)).mtimeMs<n&&(await v(s),i+=1)}catch{}})),i}r(ne,"prunePasteTmpDir");var oe=/\.(png|jpe?g|gif|webp|bmp)$/i;async function ie(e){if(e.length===0)return process.platform==="darwin"?R():{kind:"text",text:""};let t=G(e);return t.length>0?ce(t,e):{kind:"text",text:e}}r(ie,"handleBracketedPaste");async function se(){return R()}r(se,"triggerExplicitClipboardImage");async function R(){let e;try{e=await p()}catch(t){return{kind:"error",message:`Failed to read clipboard image: ${y(t)}`}}return e?$(e):{kind:"noop"}}r(R,"readClipboardAsImagesOutcome");async function ce(e,t){let a=[],n=[];for(let i of e){let o;try{o=await ae(i)}catch(l){n.push(`Failed to read ${i}: ${y(l)}`);continue}let s=await $(o);s.kind==="images"?a.push(...s.blocks):s.kind==="error"&&n.push(s.message)}return a.length===0?n.length>0?{kind:"error",message:n.join("; ")}:{kind:"text",text:t}:{kind:"images",blocks:a}}r(ce,"readImagePathsAsOutcome");async function $(e){try{let t=await h(e);return{kind:"images",blocks:[await x(t)]}}catch(t){return t instanceof u?{kind:"error",message:t.message}:{kind:"error",message:`Failed to process pasted image: ${y(t)}`}}}r($,"decodeBufferToOutcome");function G(e){return e.split(/ (?=\/|[A-Za-z]:\\)/).filter(a=>oe.test(a.trim()))}r(G,"extractImagePaths");function y(e){return e instanceof Error?e.message:String(e)}r(y,"describeError");export{C as a,T as b,L as c,O as d,p as e,g as f,d as g,u as h,h as i,B as j,A as k,x as l,ne as m,ie as n,se as o,G as p};
