Files
store/github-utilities/fetch.sune

1 line
14 KiB
JSON

[{"id":"zp2je1g","name":"Github Fetch","pinned":false,"avatar":"data:image/webp;base64,UklGRooHAABXRUJQVlA4WAoAAAAgAAAAfwAAfwAASUNDUMgBAAAAAAHIAAAAAAQwAABtbnRyUkdCIFhZWiAH4AABAAEAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAACRyWFlaAAABFAAAABRnWFlaAAABKAAAABRiWFlaAAABPAAAABR3dHB0AAABUAAAABRyVFJDAAABZAAAAChnVFJDAAABZAAAAChiVFJDAAABZAAAAChjcHJ0AAABjAAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAAgAAAAcAHMAUgBHAEJYWVogAAAAAAAAb6IAADj1AAADkFhZWiAAAAAAAABimQAAt4UAABjaWFlaIAAAAAAAACSgAAAPhAAAts9YWVogAAAAAAAA9tYAAQAAAADTLXBhcmEAAAAAAAQAAAACZmYAAPKnAAANWQAAE9AAAApbAAAAAAAAAABtbHVjAAAAAAAAAAEAAAAMZW5VUwAAACAAAAAcAEcAbwBvAGcAbABlACAASQBuAGMALgAgADIAMAAxADZWUDggnAUAADAfAJ0BKoAAgAA+bTaWSKQioiEilmqQgA2JaQDUWEEf6T2k/2DoM/J/rjnHPrF998b+8Haf/z28EgA/I/5f+qviISkP5l/t/Jy8Abw32A/4//Rv+z/iv7J8IP7N/zf8t+SHsm/Jf6p/0f8j8An8c/nP/E/vH+R9531m/tv7B36w/9YhW0wbdUMVAp86gdlU+cb71sDJl67tYOiLTSMimadBQ+aLv5eVhjY/ZjU+DqGaK/9MDthXkm7ZkhaqAg7h5oGKwibYvlEui2hxDGk7FWbw2bCKjg6czZvjt4atJzKb9JmTHTDp5U8Bv05jbiwiPQMi3GGOZtLEXKiCz+dAxE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rw+tx2RyIrvQOImk/CaGYgqjSiNpLqN6LZD1y3SvvO1Yy9uSaeBRqs7+PwdPQleIzKhohc4Dr1xWl2+l8re3A9dDJv/yLH2pv6QB4lXScnF+wFZcWXEn4kx8EAYmntIUqcDwCYuMgKWTV45A3qLyyseP/HSoMdHVJz+5/JNd6C+O3cFjQDrNwIs4P4yl/6k3/Tv9Z3UqNAQz2KIs0TIXJsx18K4tBpyR3PttyPUTTDQBq7aHNmeBr5SaAkflfaJRNdnGAOt6wS2KludQkYL1FSFTGZzm/FobOPqe22PUD8wdUqyf45uPzbbLU46f40Px/+HFKu0tqnpqO+DG464z6Rk9wrbasl7NpsC9HKucWrwyCACsR6S13XqeqraZNPoDq8sBKQkMgb/8pqOWPvkMLDkW8beuhtNEmSfWGn9f/Zdn57z6QkvFfBD9zzdBEb7oPhDOmC49nPqjSJZ9L6fS9MEHxMb6xFoWpMgdqYbk+Pn4a3yB5M3CCfJFpvdsTdSg4o9EAA","url":"gh://sune-org/store/github-utilities/fetch.sune","updatedAt":1758988028464,"settings":{"model":"","temperature":"","top_p":"","top_k":"","frequency_penalty":"","repetition_penalty":"","min_p":"","top_a":"","verbosity":"","reasoning_effort":"default","system_prompt":"","html":"<div id=\"ghFetchSune\" class=\"p-1.5 bg-slate-100 rounded-lg shadow-sm font-sans\" x-data>\n <div class=\"flex items-center justify-between\">\n <div class=\"flex items-center gap-1.5\">\n <i data-lucide=\"github\" class=\"h-4 w-4 text-slate-600\"></i>\n <span class=\"text-xs font-medium text-slate-800\">GitHub Fetch</span>\n <span class=\"text-[10px] text-slate-400\">v1.9.1</span>\n </div>\n <div class=\"flex items-center gap-1\">\n <button id=\"ghFetchAllBtn\" type=\"button\" class=\"rounded-md px-2 py-1 text-xs font-semibold bg-slate-800 text-white hover:bg-slate-700 active:scale-95 transition-all\">\n Fetch All\n </button>\n <button type=\"button\" id=\"ghAddRowBtn\" class=\"h-7 w-7 rounded-md bg-slate-200/80 text-slate-600 hover:bg-white active:shadow-inner active:scale-95 flex items-center justify-center transition-all\" title=\"Add URL\">\n <i data-lucide=\"plus\" class=\"h-4 w-4\"></i>\n </button>\n <button type=\"button\" id=\"ghHelpBtn\" class=\"h-7 w-7 rounded-md bg-slate-200/80 text-slate-600 hover:bg-white active:shadow-inner active:scale-95 flex items-center justify-center transition-all\" title=\"Help\">\n <i data-lucide=\"help-circle\" class=\"h-4 w-4\"></i>\n </button>\n </div>\n </div>\n\n <div id=\"ghRows\" class=\"mt-2 space-y-1.5\"></div>\n <div id=\"ghStatus\" class=\"mt-1.5 px-1 text-[11px] text-slate-600 min-h-[1em]\"></div>\n</div>\n\n<script>\n(()=>{const r=document.getElementById('ghFetchSune');if(!r)return;const $=s=>r.querySelector(s),els={rows:$('#ghRows'),add:$('#ghAddRowBtn'),all:$('#ghFetchAllBtn'),help:$('#ghHelpBtn'),status:$('#ghStatus')},cache={},icons=()=>{try{lucide.createIcons({attrs:{'aria-hidden':!0}})}catch(e){console.error(\"Lucide err:\",e)}},getKey=()=>`gh_fetch_sune_${window.SUNE?.id||'gh_fetch_default'}_urls_v3`,load=()=>{try{const d=JSON.parse(localStorage.getItem(getKey()));return Array.isArray(d)?d.map(x=>String(x||'')):['']}catch{return['']}},save=d=>localStorage.setItem(getKey(),JSON.stringify(d.map(u=>(u||'').trim()))),setStatus=(el,msg,kind)=>{const k={error:'text-red-600',ok:'text-emerald-600',info:el.id==='ghStatus'?'text-slate-600':'text-slate-500'};el.textContent=msg||'';el.className=`${el.dataset.baseClass} ${k[kind]||k.info}`},setGlobalStatus=(m,k='info')=>setStatus(els.status,m,k),setRowStatus=(i,m,k='info')=>{const n=$(`[data-role=\"row-status\"][data-index=\"${i}\"]`);n&&setStatus(n,m,k)},spinner=c=>`<svg class=\"animate-spin ${c}\" viewBox=\"0 0 24 24\"><circle class=\"opacity-25\" cx=\"12\" cy=\"12\" r=\"10\" stroke=\"currentColor\" stroke-width=\"4\" fill=\"none\"></circle><path class=\"opacity-75\" fill=\"currentColor\" d=\"M4 12a8 8 0 018-8v4A4 4 0 004 12z\"></path></svg>`,ghFetch=async(url,opts={})=>{const h=opts.headers||{};window.globalStore?.ghToken&&(h.Authorization=`Bearer ${window.globalStore.ghToken}`);const res=await fetch(url,{...opts,headers:h});if(!res.ok)throw new Error(`Request failed: HTTP ${res.status}`);return res},getInfo=async(o,r)=>{const k=`${o}/${r}`;if(cache[k])return cache[k];const d=await(await ghFetch(`https://api.github.com/repos/${o}/${r}`)).json();if(!d.default_branch)throw new Error('No default branch.');return cache[k]=d};async function parse(u){try{const lM=u.match(/#L(\\d+)(?:-L(\\d+))?$/i),lines=lM?{start:+lM[1],end:+lM[2]||+lM[1]}:null,urlStr=u.split('#')[0];try{const url=new URL(urlStr);let owner,repo,ref,path;if('github.com'===url.hostname){const p=url.pathname.split('/').filter(Boolean);if(p.length>=4&&('blob'===p[2]||'raw'===p[2]))return[owner,repo,,ref]=p,path=p.slice(4).join('/'),{type:'file',p:{owner,repo,ref,path,lines,raw:`https://raw.githubusercontent.com/${owner}/${repo}/${ref}/${path}`}}}else if('raw.githubusercontent.com'===url.hostname){const p=url.pathname.split('/').filter(Boolean);if(p.length>=4)return[owner,repo,ref]=p,path=p.slice(3).join('/'),{type:'file',p:{owner,repo,ref,path,lines,raw:url.href}}}}catch(e){}const p=urlStr.split('/').filter(Boolean);if(p.length>=2){const[owner,repo]=p;if(p.length>2){const path=p.slice(2).join('/'),{default_branch:ref}=await getInfo(owner,repo);return{type:'file',p:{owner,repo,ref,path,lines,raw:`https://raw.githubusercontent.com/${owner}/${repo}/${ref}/${path}`}}}const info=await getInfo(owner,repo);return{type:'repo',p:{owner,repo,default_branch:info.default_branch}}}}catch(err){return null}}const isBin=p=>/\\.(png|jpe?g|gif|bmp|webp|ico|tif?f|svg|zip|rar|7z|tar|gz|bz2|xz|pdf|docx?|xlsx?|pptx?|mp[34]|wav|ogg|webm|mov|avi|mkv|woff2?|ttf|otf|eot|exe|dll|so|dylib|bin|o|a|class|jar|pyc|iso|img|dmg)$/i.test(p),getLang=p=>(p.split('.').pop()||'').toLowerCase(),getFence=s=>'`'.repeat(Math.max(3,1+Math.max(0,...(s.match(/`+/g)||[]).map(x=>x.length)))),fetchFile=async({raw,path,lines,owner,repo,ref})=>{const h=`[${owner}/${repo}@${ref}/${path}](https://github.com/${owner}/${repo}/blob/${ref}/${path})`;if(isBin(path))return{md:`${h}\\n\\n*Binary file - content not displayed.*`};let content=await(await ghFetch(raw,{cache:'no-store'})).text();if(path.toLowerCase().endsWith('.sune')){try{const d=JSON.parse(content),html=d?.[0]?.settings?.html;if('string'==typeof html)return{md:html}}catch{}}lines&&(content=content.replace(/\\r\\n/g,'\\n').split('\\n').slice(lines.start-1,lines.end).join('\\n'));const fence=getFence(content);return{md:`${h}\\n\\n${fence}${getLang(path)}\\n${content}\\n${fence}`}},fetchRepo=async({owner,repo,default_branch},i)=>{setRowStatus(i,'Fetching tree...');const{commit:{sha}}=await(await ghFetch(`https://api.github.com/repos/${owner}/${repo}/branches/${default_branch}`)).json(),{tree,truncated}=await(await ghFetch(`https://api.github.com/repos/${owner}/${repo}/git/trees/${sha}?recursive=1`)).json();if(truncated)throw new Error('Repo too large to fetch completely.');const files=tree.filter(i=>'blob'===i.type);if(!files.length)throw new Error('No files found in repo.');setRowStatus(i,`Fetching ${files.length} file(s)...`);const contents=await Promise.all(files.map(async f=>{if(isBin(f.path))return{path:f.path,bin:!0};try{return{path:f.path,content:await(await ghFetch(`https://raw.githubusercontent.com/${owner}/${repo}/${sha}/${f.path}`)).text()}}catch{return{path:f.path,content:'Error: Could not fetch file.'}}}));return{md:contents.map(({path,content,bin})=>{const h=`[${owner}/${repo}@${default_branch}/${path}](https://github.com/${owner}/${repo}/blob/${default_branch}/${path})`;if(bin)return`${h}\\n\\n*Binary file - content not displayed.*`;const fence=getFence(content);return`${h}\\n\\n${fence}${getLang(path)}\\n${content}\\n${fence}`}).join('\\n\\n---\\n\\n')}};let urls=load();const rowTpl=(i,v='')=>`<div><div class=\"flex items-stretch gap-1\"><input data-role=\"url\" data-index=\"${i}\" type=\"text\" placeholder=\"user/repo or user/repo/file or full URL...\" class=\"flex-1 min-w-0 rounded-md border-0 bg-white shadow-inner px-2.5 py-1.5 text-xs text-slate-800 placeholder:text-slate-400 focus:ring-2 focus:ring-slate-400 transition\" value=\"${v.replace(/\"/g,'&quot;')}\"><button data-role=\"copy\" data-index=\"${i}\" type=\"button\" class=\"shrink-0 rounded-md w-8 bg-slate-200/80 text-slate-600 hover:bg-white active:shadow-inner active:scale-95 flex items-center justify-center transition-all\" title=\"Copy URL\"><i data-lucide=\"copy\" class=\"h-4 w-4\"></i></button><button data-role=\"fetch-one\" data-index=\"${i}\" type=\"button\" class=\"shrink-0 rounded-md px-2.5 text-xs font-semibold bg-slate-800 text-white hover:bg-slate-700 active:scale-95 transition-all\">Fetch</button><button data-role=\"remove\" data-index=\"${i}\" type=\"button\" class=\"shrink-0 rounded-md w-8 text-slate-500 bg-slate-200/80 hover:bg-red-100 hover:text-red-600 active:scale-95 flex items-center justify-center transition-all\" title=\"Remove\"><i data-lucide=\"trash-2\" class=\"h-4 w-4\"></i></button></div><div data-role=\"row-status\" data-index=\"${i}\" data-base-class=\"mt-1 px-1 text-[11px] min-h-[1em]\" class=\"mt-1 px-1 text-[11px] text-slate-500 min-h-[1em]\"></div></div>`,render=()=>{els.rows.innerHTML=(urls.length?urls:['']).map((u,i)=>rowTpl(i,u)).join('');icons()},post=async md=>window.USER?.log?await window.USER.log(md):new Error('Chat injection failed.');async function fetchRow(i){const inp=$(`[data-role=\"url\"][data-index=\"${i}\"]`),btn=$(`[data-role=\"fetch-one\"][data-index=\"${i}\"]`);if(!inp||!btn)return!1;setRowStatus(i,'');const url=(inp.value||'').trim();if(!url)return setRowStatus(i,'Please enter a path or URL.','error'),!1;const prev=btn.innerHTML;btn.innerHTML=spinner('h-4 w-4'),btn.disabled=inp.disabled=!0;try{setRowStatus(i,'Parsing...');const parsed=await parse(url);if(!parsed)throw new Error('Invalid URL or repo not found.');const result=await('file'===parsed.type?fetchFile(parsed.p):fetchRepo(parsed.p,i));await post(result.md);return setRowStatus(i,'Added to chat.','ok'),!0}catch(err){return setRowStatus(i,err.message||String(err),'error'),!1}finally{btn.innerHTML=prev,btn.disabled=inp.disabled=!1}}els.rows.addEventListener('click',async e=>{const b=e.target.closest('button[data-role]');if(!b)return;const i=+b.dataset.index;({copy:async()=>{const n=$(`input[data-index=\"${i}\"]`);if(!n?.value)return setRowStatus(i,'Nothing to copy.');try{await navigator.clipboard.writeText(n.value),setRowStatus(i,'Copied!','ok')}catch{setRowStatus(i,'Copy failed.','error')}},'fetch-one':()=>fetchRow(i),remove:()=>{urls.length>1?urls.splice(i,1):urls[0]='';save(urls);render()}})[b.dataset.role]?.()});els.rows.addEventListener('input',e=>{const i=e.target.closest('input[data-role=\"url\"]');i&&(urls[+i.dataset.index]=i.value,save(urls))}),els.rows.addEventListener('keydown',e=>'Enter'===e.key&&e.target.matches('input[data-role=\"url\"]')&&(e.preventDefault(),fetchRow(+e.target.dataset.index))),els.add.addEventListener('click',()=>{urls.push(''),save(urls),render(),$(`input[data-index=\"${urls.length-1}\"]`)?.focus()}),els.all.addEventListener('click',async()=>{const list=urls.map((u,i)=>({u:u.trim(),i})).filter(i=>i.u);if(!list.length)return setGlobalStatus('No URLs to fetch. Add one with +.','error');const prev=els.all.innerHTML;els.all.innerHTML=spinner('h-4 w-4'),els.all.disabled=!0,setGlobalStatus(`Fetching ${list.length} item(s)...`);let s=0,f=0;for(const{i}of list)await fetchRow(i)?s++:f++;els.all.innerHTML=prev,els.all.disabled=!1,0===f?setGlobalStatus(`Fetched ${s} item(s) successfully.`,'ok'):setGlobalStatus(`Completed: ${s} succeeded, ${f} failed.`,'error')}),els.help.addEventListener('click',()=>alert(['GitHub Fetch Sune - Help','','• Add URL fields with the + button.','• You can fetch single files or entire repositories.','','FORMATS:',' - Repo: user/repo',' - File: user/repo/path/to/file.js',' - Full URL: .../blob/main/file.js','','• For files, you can add #L10-L20 to specify lines.','• For .sune files, only the HTML content is injected.','• Fetching a whole repo is only possible for smaller repositories.','','Pro Tip: Set a GitHub Token in Account Settings to fetch from private repos and avoid API rate limits.'].join('\\n'))),els.status.dataset.baseClass='mt-1.5 px-1 text-[11px] min-h-[1em]',render()})();\n</script>\n","extension_html":"<sune src='https://raw.githubusercontent.com/sune-org/store/refs/heads/main/sync.sune' private></sune>","hide_composer":false,"include_thoughts":false,"json_output":false,"ignore_master_prompt":false,"json_schema":"","presence_penalty":0,"max_tokens":0},"storage":{}}]