mirror of
https://github.com/multipleof4/devsune.git
synced 2026-01-13 16:07:55 +00:00
Update index.html via Sune
This commit is contained in:
@@ -161,7 +161,8 @@ let sunes=(su.load()||[]).map(makeSune)
|
||||
const SUNE=window.SUNE=new Proxy({get list(){return sunes},get id(){return su.getActiveId()},get active(){return sunes.find(a=>a.id===su.getActiveId())||sunes[0]},get:id=>sunes.find(s=>s.id===id),setActive:id=>su.setActiveId(id||''),create(p={}){const s=makeSune(p);sunes.unshift(s);su.save(sunes);return s},delete(id){const curId=this.id;sunes=sunes.filter(s=>s.id!==id);su.save(sunes);if(sunes.length===0){const def=this.create({name:'Default'});this.setActive(def.id)}else if(curId===id)this.setActive(sunes[0].id)},save:()=>su.save(sunes)},{get(t,p){if(p in t)return t[p];if(p==='provider')return globalStore.provider;if(p==='apiKey')return globalStore.provider==='openai'?globalStore.apiKeyOAI:globalStore.apiKeyOR;if(p==='apiKeyOR')return globalStore.apiKeyOR;if(p==='apiKeyOAI')return globalStore.apiKeyOAI;if(p==='masterPrompt')return globalStore.masterPrompt;if(p==='titleModel')return globalStore.titleModel;const a=t.active;if(!a)return;if(p in a.settings)return a.settings[p];if(p in a)return a[p]},set(t,p,v){if(p==='provider'){globalStore.provider=v;return true}if(p==='apiKey'){if(globalStore.provider==='openai')globalStore.apiKeyOAI=v;else globalStore.apiKeyOR=v;return true}if(p==='apiKeyOR'){globalStore.apiKeyOR=v;return true}if(p==='apiKeyOAI'){globalStore.apiKeyOAI=v;return true}if(p==='masterPrompt'){globalStore.masterPrompt=v;return true}if(p==='titleModel'){globalStore.titleModel=v;return true}const a=t.active;if(!a)return false;const i=sunes.findIndex(s=>s.id===a.id);if(i<0)return false;if(p==='name'||p==='avatar'||p==='url'||p==='pinned')sunes[i][p]=v;else{if(p==='model')sunes[i].settings.model=v||DEFAULT_MODEL;else if(p==='system_prompt')sunes[i].settings.system_prompt=v||'';else sunes[i].settings[p]=v}sunes[i].updatedAt=Date.now();su.save(sunes);return true}})
|
||||
if(!sunes.length){const def=SUNE.create({name:'Default'});SUNE.setActive(def.id)}
|
||||
const state={messages:[],busy:false,controller:null,currentThreadId:null,abortRequested:false,attachments:[],stream:{rid:null,bubble:null,meta:null,text:'',done:false}}
|
||||
const getModelShort=m=>{const mm=m||SUNE.model||'';return mm.includes('/')?mm.split('/').pop():mm};const renderSuneTag=async(root=document)=>{for(const tag of root.querySelectorAll('sune[src^="gh://"]'))try{const[,org,repo,...path]=tag.getAttribute('src').replace('gh://','').split('/');if(!org||!repo||!path.length)throw new Error('Invalid gh:// URL');const suneData=await(await fetch(`https://cdn.jsdelivr.net/gh/${org}/${repo}/${path.join('/')}`)).json();const html=[suneData?.settings?.html,suneData?.settings?.extension_html].join('\n');const div=document.createElement('div');div.innerHTML=html;div.querySelectorAll('script').forEach(s=>{const n=document.createElement('script');[...s.attributes].forEach(a=>n.setAttribute(a.name,a.value));n.text=s.text;document.body.appendChild(n);s.remove()});tag.replaceWith(...div.childNodes)}catch(e){console.error(`Sune load failed: ${tag.getAttribute('src')}`,e);tag.outerHTML=`<!-- Sune load failed: ${esc(tag.getAttribute('src'))} -->`}};
|
||||
const getModelShort=m=>{const mm=m||SUNE.model||'';return mm.includes('/')?mm.split('/').pop():mm}
|
||||
const renderSuneTag=async(root=document)=>{for(const tag of root.querySelectorAll('sune[src^="gh://"]')){const src=tag.getAttribute('src');try{const[,org,repo,...path]=src.replace('gh://','').split('/');if(!org||!repo||!path.length)throw new Error('Invalid gh:// URL format');const url=`https://cdn.jsdelivr.net/gh/${org}/${repo}/${path.join('/')}`;const response=await fetch(url);if(!response.ok)throw new Error(`HTTP error ${response.status}: ${response.statusText}`);const suneData=await response.json();if(!suneData||typeof suneData!=='object')throw new Error('Invalid Sune JSON data');const html=[suneData?.settings?.html,suneData?.settings?.extension_html].join('\n');const div=document.createElement('div');div.innerHTML=html;div.querySelectorAll('script').forEach(s=>{const n=document.createElement('script');[...s.attributes].forEach(a=>n.setAttribute(a.name,a.value));n.text=s.text;document.body.appendChild(n);s.remove()});tag.replaceWith(...div.childNodes)}catch(e){const errorMsg=e instanceof Error?e.message:String(e);console.error(`Sune load failed for ${src}:`,errorMsg);tag.outerHTML=`<!-- Sune load failed for ${esc(src)}: ${esc(errorMsg)} -->`}}};
|
||||
const renderSuneHTML=async()=>{const c=[SUNE.html,SUNE.extension_html].map(x=>(x||'').trim()).join('\n'),m=el.suneHtml;m.innerHTML='';m.classList.toggle('hidden',!c);if(!c)return;m.insertAdjacentHTML('afterbegin',c);m.querySelectorAll('script').forEach(s=>{const n=document.createElement('script');[...s.attributes].forEach(a=>n.setAttribute(a.name,a.value));n.text=s.text;s.replaceWith(n)});await renderSuneTag(m)}
|
||||
const reflectActiveSune=async()=>{const a=SUNE.active;el.suneBtnTop.title=`Settings — ${a.name}`;el.suneBtnTop.innerHTML=a.avatar?`<img src="${esc(a.avatar)}" alt="" class="h-8 w-8 rounded-full object-cover"/>`:'✺';icons();await renderSuneHTML()}
|
||||
const suneRow=a=>`<div class="relative flex items-center gap-2 px-3 py-2 ${a.pinned?'bg-yellow-50':''}"><button data-sune-id="${a.id}" class="flex-1 text-left flex items-center gap-2 ${a.id===SUNE.id?'font-medium':''}">${a.avatar?`<img src="${esc(a.avatar)}" alt="" class="h-6 w-6 rounded-full object-cover"/>`:`<span class="h-6 w-6 rounded-full bg-gray-200 flex items-center justify-center">✺</span>`}<span class="truncate">${a.pinned?'📌 ':''}${esc(a.name)}</span></button><button data-sune-menu="${a.id}" class="h-8 w-8 rounded hover:bg-gray-100 flex items-center justify-center" title="More"><i data-lucide="more-horizontal" class="h-4 w-4"></i></button></div>`
|
||||
|
||||
Reference in New Issue
Block a user