diff --git a/extension/dist/main.js b/extension/dist/main.js index b67685b..1beee06 100644 --- a/extension/dist/main.js +++ b/extension/dist/main.js @@ -1,4 +1,4 @@ -var ye=Object.defineProperty;var ve=(e,t,l)=>t in e?ye(e,t,{enumerable:!0,configurable:!0,writable:!0,value:l}):e[t]=l;var W=(e,t,l)=>(ve(e,typeof t!="symbol"?t+"":t,l),l);(function(){const t=document.createElement("link").relList;if(t&&t.supports&&t.supports("modulepreload"))return;for(const o of document.querySelectorAll('link[rel="modulepreload"]'))n(o);new MutationObserver(o=>{for(const r of o)if(r.type==="childList")for(const s of r.addedNodes)s.tagName==="LINK"&&s.rel==="modulepreload"&&n(s)}).observe(document,{childList:!0,subtree:!0});function l(o){const r={};return o.integrity&&(r.integrity=o.integrity),o.referrerPolicy&&(r.referrerPolicy=o.referrerPolicy),o.crossOrigin==="use-credentials"?r.credentials="include":o.crossOrigin==="anonymous"?r.credentials="omit":r.credentials="same-origin",r}function n(o){if(o.ep)return;o.ep=!0;const r=l(o);fetch(o.href,r)}})();function T(){}function de(e){return e()}function Y(){return Object.create(null)}function A(e){e.forEach(de)}function ce(e){return typeof e=="function"}function ae(e,t){return e!=e?t==t:e!==t||e&&typeof e=="object"||typeof e=="function"}function be(e){return Object.keys(e).length===0}function w(e,t){e.appendChild(t)}function j(e,t,l){e.insertBefore(t,l||null)}function O(e){e.parentNode&&e.parentNode.removeChild(e)}function ke(e,t){for(let l=0;le.removeEventListener(t,l,n)}function a(e,t,l){l==null?e.removeAttribute(t):e.getAttribute(t)!==l&&e.setAttribute(t,l)}function Se(e){return Array.from(e.childNodes)}function Ce(e,t){t=""+t,e.data!==t&&(e.data=t)}function M(e,t){e.value=t??""}function Q(e,t,l){for(let n=0;ne.indexOf(n)===-1?t.push(n):l.push(n)),l.forEach(n=>n()),L=t}const q=new Set;let Te;function fe(e,t){e&&e.i&&(q.delete(e),e.i(t))}function Ae(e,t,l,n){if(e&&e.o){if(q.has(e))return;q.add(e),Te.c.push(()=>{q.delete(e)}),e.o(t)}}function te(e){return(e==null?void 0:e.length)!==void 0?e:Array.from(e)}function Ne(e){e&&e.c()}function we(e,t,l){const{fragment:n,after_update:o}=e.$$;n&&n.m(t,l),D(()=>{const r=e.$$.on_mount.map(de).filter(ce);e.$$.on_destroy?e.$$.on_destroy.push(...r):A(r),e.$$.on_mount=[]}),o.forEach(D)}function pe(e,t){const l=e.$$;l.fragment!==null&&(Le(l.after_update),A(l.on_destroy),l.fragment&&l.fragment.d(t),l.on_destroy=l.fragment=null,l.ctx=[])}function Be(e,t){e.$$.dirty[0]===-1&&($.push(e),Ie(),e.$$.dirty.fill(0)),e.$$.dirty[t/31|0]|=1<{const f=_.length?_[0]:c;return d.ctx&&o(d.ctx[u],d.ctx[u]=f)&&(!d.skip_bound&&d.bound[u]&&d.bound[u](f),g&&Be(e,u)),c}):[],d.update(),g=!0,A(d.before_update),d.fragment=n?n(d.ctx):!1,t.target){if(t.hydrate){const u=Se(t.target);d.fragment&&d.fragment.l(u),u.forEach(O)}else d.fragment&&d.fragment.c();t.intro&&fe(e.$$.fragment),we(e,t.target,t.anchor),ue()}N(m)}class he{constructor(){W(this,"$$");W(this,"$$set")}$destroy(){pe(this,1),this.$destroy=T}$on(t,l){if(!ce(l))return T;const n=this.$$.callbacks[t]||(this.$$.callbacks[t]=[]);return n.push(l),()=>{const o=n.indexOf(l);o!==-1&&n.splice(o,1)}}$set(t){this.$$set&&!be(t)&&(this.$$.skip_bound=!0,this.$$set(t),this.$$.skip_bound=!1)}}const Pe="4";typeof window<"u"&&(window.__svelte||(window.__svelte={v:new Set})).v.add(Pe);const le=async(e="",t="")=>{let l=null;const n=await fetch(`${t}/models`,{method:"GET",headers:{Accept:"application/json","Content-Type":"application/json",...e&&{authorization:`Bearer ${e}`}}}).then(async r=>{if(!r.ok)throw await r.json();return r.json()}).catch(r=>{var s;return l=`OpenAI: ${((s=r==null?void 0:r.error)==null?void 0:s.message)??"Network Problem"}`,[]});if(l)throw l;const o=Array.isArray(n)?n:(n==null?void 0:n.data)??null;return o&&o.map(r=>({id:r.id,name:r.name??r.id,url:t,custom_info:r.custom_info})).sort((r,s)=>r.name.localeCompare(s.name))},ne=async(e,t)=>{let l=await Promise.all([le(e,`${t}/ollama/v1`).catch(n=>(console.log(n),null)),le(e,`${t}/openai/api`).catch(n=>(console.log(n),null))]);return l=l.filter(n=>n).reduce((n,o,r,s)=>n.concat(o),[]),console.log(l),l},He=async(e="",t={},l="http://localhost:8080")=>{const n=new AbortController;let o=null;const r=await fetch(`${l}/chat/completions`,{signal:n.signal,method:"POST",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"},body:JSON.stringify(t)}).catch(s=>(console.log(s),o=s,null));if(o)throw o;return[r,n]},ze=e=>{let t="";return new TransformStream({transform(l,n){t+=l;const o=t.split(e);o.slice(0,-1).forEach(r=>n.enqueue(r)),t=o[o.length-1]},flush(l){t&&l.enqueue(t)}})};function oe(e,t,l){const n=e.slice();return n[6]=t[l],n}function re(e){let t,l,n;function o(i,m){return i[1]?qe:Ke}let r=o(e),s=r(e);return{c(){t=p("div"),s.c(),a(t,"class","tlwd-fixed tlwd-top-0 tlwd-right-0 tlwd-left-0 tlwd-bottom-0 tlwd-w-full tlwd-min-h-screen tlwd-h-screen tlwd-flex tlwd-justify-center tlwd-z-[9999999999] tlwd-overflow-hidden tlwd-overscroll-contain")},m(i,m){j(i,t,m),s.m(t,null),l||(n=S(t,"mousedown",e[15]),l=!0)},p(i,m){r===(r=o(i))&&s?s.p(i,m):(s.d(1),s=r(i),s&&(s.c(),s.m(t,null)))},d(i){i&&O(t),s.d(),l=!1,n()}}}function Ke(e){let t,l,n,o,r,s,i,m,d,g,u,c,_,f;return{c(){t=p("div"),l=p("div"),n=p("form"),o=p("div"),r=p("div"),r.innerHTML=``,s=C(),i=p("input"),m=C(),d=p("div"),g=p("div"),g.textContent="Press ⌘Space+Shift to toggle",u=C(),c=p("button"),c.innerHTML=``,a(r,"class","tlwd-flex tlwd-items-center"),a(i,"id","open-webui-search-input"),a(i,"placeholder","Search Open WebUI"),a(i,"class","tlwd-p-0 tlwd-m-0 tlwd-text-xl tlwd-w-full tlwd-font-medium tlwd-bg-transparent tlwd-border-none placeholder:tlwd-text-gray-500 tlwd-text-neutral-100 tlwd-outline-none"),a(i,"autocomplete","one-time-code"),a(o,"class","tlwd-flex tlwd-items-center tlwd-gap-2 tlwd-w-full"),a(g,"class","tlwd-text-right tlwd-text-[0.7rem] tlwd-p-0 tlwd-m-0 tlwd-text-neutral-300 tlwd-h-fit"),a(c,"class","tlwd-h-fit tlwd-flex tlwd-items-center tlwd-bg-transparent tlwd-text-neutral-100 tlwd-cursor-pointer tlwd-p-0 tlwd-m-0 tlwd-outline-none tlwd-border-none"),a(c,"type","button"),a(d,"class","tlwd-flex tlwd-justify-end tlwd-gap-1 tlwd-items-center"),a(n,"class","tlwd-text-gray-200 tlwd-w-full tlwd-p-0 tlwd-m-0"),a(n,"autocomplete","off"),a(l,"class","tlwd-w-full tlwd-flex tlwd-flex-col tlwd-justify-between tlwd-py-2.5 tlwd-px-3.5 tlwd-rounded-2xl tlwd-outline tlwd-outline-1 tlwd-outline-gray-850 tlwd-backdrop-blur-3xl tlwd-bg-gray-850/70 shadow-4xl modal-animation"),a(t,"class","tlwd-m-auto tlwd-max-w-xl tlwd-w-full tlwd-pb-32")},m(k,E){j(k,t,E),w(t,l),w(l,n),w(n,o),w(o,r),w(o,s),w(o,i),M(i,e[4]),w(n,m),w(n,d),w(d,g),w(d,u),w(d,c),_||(f=[S(i,"input",e[13]),S(c,"click",e[14]),S(n,"submit",e[7]),S(n,"mousedown",Ve)],_=!0)},p(k,E){E&16&&i.value!==k[4]&&M(i,k[4])},d(k){k&&O(t),_=!1,A(f)}}}function qe(e){let t,l,n,o,r,s,i,m,d,g,u,c,_,f,k,E,P,b=e[5]&&e[5].length>0&&se(e);return{c(){t=p("div"),l=p("div"),n=p("form"),o=p("div"),r=p("div"),r.innerHTML=``,s=C(),i=p("input"),m=C(),d=p("div"),g=p("div"),g.innerHTML=``,u=C(),c=p("input"),_=C(),f=p("button"),f.innerHTML=``,k=C(),b&&b.c(),a(r,"class","tlwd-flex tlwd-items-center"),a(i,"id","open-webui-url-input"),a(i,"placeholder","Open WebUI URL"),a(i,"class","tlwd-p-0 tlwd-m-0 tlwd-text-xl tlwd-w-full tlwd-font-medium tlwd-bg-transparent tlwd-border-none placeholder:tlwd-text-gray-500 tlwd-text-neutral-100 tlwd-outline-none"),a(i,"autocomplete","one-time-code"),i.required=!0,a(o,"class","tlwd-flex tlwd-items-center tlwd-gap-2 tlwd-w-full"),a(g,"class","tlwd-flex tlwd-items-center"),a(c,"placeholder","Open WebUI API Key"),a(c,"class","tlwd-p-0 tlwd-m-0 tlwd-text-xl tlwd-w-full tlwd-font-medium tlwd-bg-transparent tlwd-border-none placeholder:tlwd-text-gray-500 tlwd-text-neutral-100 tlwd-outline-none"),a(c,"autocomplete","one-time-code"),c.required=!0,a(f,"class","tlwd-flex tlwd-items-center tlwd-bg-transparent tlwd-text-neutral-100 tlwd-cursor-pointer tlwd-p-0 tlwd-m-0 tlwd-outline-none tlwd-border-none"),a(f,"type","button"),a(d,"class","tlwd-flex tlwd-items-center tlwd-gap-2 tlwd-w-full tlwd-mt-2"),a(n,"class","tlwd-text-gray-200 tlwd-w-full tlwd-p-0 tlwd-m-0"),a(n,"autocomplete","off"),a(l,"class","tlwd-w-full tlwd-flex tlwd-flex-col tlwd-justify-between tlwd-py-2.5 tlwd-px-3.5 tlwd-rounded-2xl tlwd-outline tlwd-outline-1 tlwd-outline-gray-850 tlwd-backdrop-blur-3xl tlwd-bg-gray-850/70 shadow-4xl modal-animation"),a(t,"class","tlwd-m-auto tlwd-max-w-sm tlwd-w-full tlwd-pb-32")},m(x,h){j(x,t,h),w(t,l),w(l,n),w(n,o),w(o,r),w(o,s),w(o,i),M(i,e[2]),w(n,m),w(n,d),w(d,g),w(d,u),w(d,c),M(c,e[3]),w(d,_),w(d,f),w(n,k),b&&b.m(n,null),E||(P=[S(i,"input",e[9]),S(c,"input",e[10]),S(f,"click",e[11]),S(n,"submit",e[8]),S(n,"mousedown",Ue)],E=!0)},p(x,h){h&4&&i.value!==x[2]&&M(i,x[2]),h&8&&c.value!==x[3]&&M(c,x[3]),x[5]&&x[5].length>0?b?b.p(x,h):(b=se(x),b.c(),b.m(n,null)):b&&(b.d(1),b=null)},d(x){x&&O(t),b&&b.d(),E=!1,A(P)}}}function se(e){let t,l,n,o,r,s,i,m,d,g=te(e[5]),u=[];for(let c=0;c`,n=C(),o=p("select"),r=p("option"),r.textContent="Select a model";for(let c=0;c`,a(l,"class","tlwd-flex tlwd-items-center"),r.__value="",M(r,r.__value),a(o,"id","open-webui-model-input"),a(o,"class","tlwd-p-0 tlwd-m-0 tlwd-text-xl tlwd-w-full tlwd-font-medium tlwd-bg-transparent tlwd-border-none placeholder:tlwd-text-gray-500 tlwd-text-neutral-100 tlwd-outline-none"),a(o,"autocomplete","off"),o.required=!0,e[6]===void 0&&D(()=>e[12].call(o)),a(i,"class","tlwd-flex tlwd-items-center tlwd-bg-transparent tlwd-text-neutral-100 tlwd-cursor-pointer tlwd-p-0 tlwd-m-0 tlwd-outline-none tlwd-border-none"),a(i,"type","submit"),a(t,"class","tlwd-flex tlwd-items-center tlwd-gap-2 tlwd-w-full tlwd-mt-2")},m(c,_){j(c,t,_),w(t,l),w(t,n),w(t,o),w(o,r);for(let f=0;f{e.stopPropagation()},Ve=e=>{e.stopPropagation()};function Re(e,t,l){let n=!1,o=!0,r="",s="",i="",m="",d=[];const g=()=>{console.log("resetConfig");try{chrome.storage.local.clear().then(()=>{console.log("Value is cleared")})}catch(h){console.log(h),localStorage.setItem("url",""),localStorage.setItem("key",""),localStorage.setItem("model","")}l(2,r=""),l(3,s=""),l(6,i=""),l(1,o=!0)},u=h=>{h.preventDefault(),window.open(`${r}/?q=${encodeURIComponent(m)}&models=${i}`,"_blank"),l(4,m=""),l(0,n=!1)},c=h=>{h.preventDefault();try{chrome.storage.local.set({url:r,key:s,model:i}).then(()=>{console.log("Value is set")})}catch(H){console.log(H),localStorage.setItem("url",r),localStorage.setItem("key",s),localStorage.setItem("model",i)}l(1,o=!1)};Oe(async()=>{let h=null;try{h=await chrome.storage.local.get()}catch(y){console.log(y)}h&&(l(2,r=h.url??""),l(3,s=h.key??""),l(6,i=h.model??""),h.url&&h.key&&h.model&&(l(5,d=await ne(h.key,h.url)),l(1,o=!1)));const H=async y=>{var G;if(n&&y.shiftKey&&y.key==="Escape"?g():y.key==="Escape"&&l(0,n=!1),y.key===" "&&(y.metaKey||y.ctrlKey)&&(y.shiftKey||y.altKey)){y.preventDefault();try{const v=await chrome.runtime.sendMessage({action:"getSelection"});((v==null?void 0:v.data)??!1)&&l(4,m=v.data)}catch(v){console.log("catch",v)}l(0,n=!n),setTimeout(()=>{const v=document.getElementById("open-webui-search-input");v&&v.focus()},0)}if(s!==""&&r!==""&&y.key==="Enter"&&(y.metaKey||y.ctrlKey)&&(y.shiftKey||y.altKey)){y.preventDefault();try{const v=await chrome.runtime.sendMessage({action:"getSelection"});if((v==null?void 0:v.data)??!1){await chrome.runtime.sendMessage({action:"writeText",text:` -`});const[U,Je]=await He(s,{model:i,messages:[{role:"system",content:"You are a helpful assistant."},{role:"user",content:v.data}],stream:!0},(G=d.find(V=>V.id===i))==null?void 0:G.url);if(U&&U.ok){const V=U.body.pipeThrough(new TextDecoderStream).pipeThrough(ze(` -`)).getReader();for(;;){const{value:ge,done:_e}=await V.read();if(_e)break;try{let R=ge.split(` -`);for(const z of R)if(z!=="")if(console.log(z),z==="data: [DONE]")console.log("DONE");else{let K=JSON.parse(z.replace(/^data: /,""));console.log(K),"request_id"in K?console.log(K.request_id):await chrome.runtime.sendMessage({action:"writeText",text:K.choices[0].delta.content??""})}}catch(R){console.log(R)}}}}}catch(v){console.log(v)}}};return document.addEventListener("keydown",H,{capture:!0}),()=>document.removeEventListener("keydown",H)});function _(){r=this.value,l(2,r)}function f(){s=this.value,l(3,s)}const k=async()=>{r.endsWith("/")&&l(2,r=r.slice(0,-1)),l(5,d=await ne(s,r))};function E(){i=Ee(this),l(6,i),l(5,d)}function P(){m=this.value,l(4,m)}return[n,o,r,s,m,d,i,u,c,_,f,k,E,P,()=>{l(1,o=!0)},()=>{l(0,n=!1)}]}class We extends he{constructor(t){super(),me(this,t,Re,De,ae,{})}}function Ze(e){let t,l,n;return l=new We({}),{c(){t=p("div"),Ne(l.$$.fragment),a(t,"class","extension-container")},m(o,r){j(o,t,r),we(l,t,null),n=!0},p:T,i(o){n||(fe(l.$$.fragment,o),n=!0)},o(o){Ae(l.$$.fragment,o),n=!1},d(o){o&&O(t),pe(l)}}}class Fe extends he{constructor(t){super(),me(this,t,null,Ze,ae,{})}}new Fe({target:document.getElementById("extension-app")}); +var _e=Object.defineProperty;var ye=(e,t,l)=>t in e?_e(e,t,{enumerable:!0,configurable:!0,writable:!0,value:l}):e[t]=l;var W=(e,t,l)=>(ye(e,typeof t!="symbol"?t+"":t,l),l);(function(){const t=document.createElement("link").relList;if(t&&t.supports&&t.supports("modulepreload"))return;for(const o of document.querySelectorAll('link[rel="modulepreload"]'))n(o);new MutationObserver(o=>{for(const r of o)if(r.type==="childList")for(const i of r.addedNodes)i.tagName==="LINK"&&i.rel==="modulepreload"&&n(i)}).observe(document,{childList:!0,subtree:!0});function l(o){const r={};return o.integrity&&(r.integrity=o.integrity),o.referrerPolicy&&(r.referrerPolicy=o.referrerPolicy),o.crossOrigin==="use-credentials"?r.credentials="include":o.crossOrigin==="anonymous"?r.credentials="omit":r.credentials="same-origin",r}function n(o){if(o.ep)return;o.ep=!0;const r=l(o);fetch(o.href,r)}})();function $(){}function ie(e){return e()}function Y(){return Object.create(null)}function A(e){e.forEach(ie)}function de(e){return typeof e=="function"}function ce(e,t){return e!=e?t==t:e!==t||e&&typeof e=="object"||typeof e=="function"}function ve(e){return Object.keys(e).length===0}function w(e,t){e.appendChild(t)}function L(e,t,l){e.insertBefore(t,l||null)}function j(e){e.parentNode&&e.parentNode.removeChild(e)}function be(e,t){for(let l=0;le.removeEventListener(t,l,n)}function u(e,t,l){l==null?e.removeAttribute(t):e.getAttribute(t)!==l&&e.setAttribute(t,l)}function xe(e){return Array.from(e.childNodes)}function Se(e,t){t=""+t,e.data!==t&&(e.data=t)}function M(e,t){e.value=t??""}function Q(e,t,l){for(let n=0;ne.indexOf(n)===-1?t.push(n):l.push(n)),l.forEach(n=>n()),I=t}const q=new Set;let Ie;function ae(e,t){e&&e.i&&(q.delete(e),e.i(t))}function $e(e,t,l,n){if(e&&e.o){if(q.has(e))return;q.add(e),Ie.c.push(()=>{q.delete(e)}),e.o(t)}}function te(e){return(e==null?void 0:e.length)!==void 0?e:Array.from(e)}function Ae(e){e&&e.c()}function fe(e,t,l){const{fragment:n,after_update:o}=e.$$;n&&n.m(t,l),D(()=>{const r=e.$$.on_mount.map(ie).filter(de);e.$$.on_destroy?e.$$.on_destroy.push(...r):A(r),e.$$.on_mount=[]}),o.forEach(D)}function we(e,t){const l=e.$$;l.fragment!==null&&(Te(l.after_update),A(l.on_destroy),l.fragment&&l.fragment.d(t),l.on_destroy=l.fragment=null,l.ctx=[])}function Be(e,t){e.$$.dirty[0]===-1&&(T.push(e),Le(),e.$$.dirty.fill(0)),e.$$.dirty[t/31|0]|=1<{const f=y.length?y[0]:c;return d.ctx&&o(d.ctx[a],d.ctx[a]=f)&&(!d.skip_bound&&d.bound[a]&&d.bound[a](f),h&&Be(e,a)),c}):[],d.update(),h=!0,A(d.before_update),d.fragment=n?n(d.ctx):!1,t.target){if(t.hydrate){const a=xe(t.target);d.fragment&&d.fragment.l(a),a.forEach(j)}else d.fragment&&d.fragment.c();t.intro&&ae(e.$$.fragment),fe(e,t.target,t.anchor),ue()}B(p)}class me{constructor(){W(this,"$$");W(this,"$$set")}$destroy(){we(this,1),this.$destroy=$}$on(t,l){if(!de(l))return $;const n=this.$$.callbacks[t]||(this.$$.callbacks[t]=[]);return n.push(l),()=>{const o=n.indexOf(l);o!==-1&&n.splice(o,1)}}$set(t){this.$$set&&!ve(t)&&(this.$$.skip_bound=!0,this.$$set(t),this.$$.skip_bound=!1)}}const Ne="4";typeof window<"u"&&(window.__svelte||(window.__svelte={v:new Set})).v.add(Ne);const le=async(e,t)=>{let l=null;const n=await fetch(`${t}/api/models`,{method:"GET",headers:{Accept:"application/json","Content-Type":"application/json",...e&&{authorization:`Bearer ${e}`}}}).then(async r=>{if(!r.ok)throw await r.json();return r.json()}).catch(r=>(console.log(r),l=r,null));if(l)throw l;let o=(n==null?void 0:n.data)??[];return o=o.filter(r=>r).sort((r,i)=>{const s=r.name.toLowerCase(),p=i.name.toLowerCase();return sp?1:ri?1:0}),console.log(o),o},He=async(e="",t={},l="http://localhost:8080")=>{const n=new AbortController;let o=null;const r=await fetch(`${l}/chat/completions`,{signal:n.signal,method:"POST",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"},body:JSON.stringify(t)}).catch(i=>(console.log(i),o=i,null));if(o)throw o;return[r,n]},ze=e=>{let t="";return new TransformStream({transform(l,n){t+=l;const o=t.split(e);o.slice(0,-1).forEach(r=>n.enqueue(r)),t=o[o.length-1]},flush(l){t&&l.enqueue(t)}})};function ne(e,t,l){const n=e.slice();return n[6]=t[l],n}function oe(e){let t,l,n;function o(s,p){return s[1]?Pe:Ke}let r=o(e),i=r(e);return{c(){t=m("div"),i.c(),u(t,"class","tlwd-fixed tlwd-top-0 tlwd-right-0 tlwd-left-0 tlwd-bottom-0 tlwd-w-full tlwd-min-h-screen tlwd-h-screen tlwd-flex tlwd-justify-center tlwd-z-[9999999999] tlwd-overflow-hidden tlwd-overscroll-contain")},m(s,p){L(s,t,p),i.m(t,null),l||(n=S(t,"mousedown",e[15]),l=!0)},p(s,p){r===(r=o(s))&&i?i.p(s,p):(i.d(1),i=r(s),i&&(i.c(),i.m(t,null)))},d(s){s&&j(t),i.d(),l=!1,n()}}}function Ke(e){let t,l,n,o,r,i,s,p,d,h,a,c,y,f;return{c(){t=m("div"),l=m("div"),n=m("form"),o=m("div"),r=m("div"),r.innerHTML=``,i=C(),s=m("input"),p=C(),d=m("div"),h=m("div"),h.textContent="Press ⌘Space+Shift to toggle",a=C(),c=m("button"),c.innerHTML=``,u(r,"class","tlwd-flex tlwd-items-center"),u(s,"id","open-webui-search-input"),u(s,"placeholder","Search Open WebUI"),u(s,"class","tlwd-p-0 tlwd-m-0 tlwd-text-xl tlwd-w-full tlwd-font-medium tlwd-bg-transparent tlwd-border-none placeholder:tlwd-text-gray-500 tlwd-text-neutral-100 tlwd-outline-none"),u(s,"autocomplete","one-time-code"),u(o,"class","tlwd-flex tlwd-items-center tlwd-gap-2 tlwd-w-full"),u(h,"class","tlwd-text-right tlwd-text-[0.7rem] tlwd-p-0 tlwd-m-0 tlwd-text-neutral-300 tlwd-h-fit"),u(c,"class","tlwd-h-fit tlwd-flex tlwd-items-center tlwd-bg-transparent tlwd-text-neutral-100 tlwd-cursor-pointer tlwd-p-0 tlwd-m-0 tlwd-outline-none tlwd-border-none"),u(c,"type","button"),u(d,"class","tlwd-flex tlwd-justify-end tlwd-gap-1 tlwd-items-center"),u(n,"class","tlwd-text-gray-200 tlwd-w-full tlwd-p-0 tlwd-m-0"),u(n,"autocomplete","off"),u(l,"class","tlwd-w-full tlwd-flex tlwd-flex-col tlwd-justify-between tlwd-py-2.5 tlwd-px-3.5 tlwd-rounded-2xl tlwd-outline tlwd-outline-1 tlwd-outline-gray-850 tlwd-backdrop-blur-3xl tlwd-bg-gray-850/70 shadow-4xl modal-animation"),u(t,"class","tlwd-m-auto tlwd-max-w-xl tlwd-w-full tlwd-pb-32")},m(k,E){L(k,t,E),w(t,l),w(l,n),w(n,o),w(o,r),w(o,i),w(o,s),M(s,e[4]),w(n,p),w(n,d),w(d,h),w(d,a),w(d,c),y||(f=[S(s,"input",e[13]),S(c,"click",e[14]),S(n,"submit",e[7]),S(n,"mousedown",Ue)],y=!0)},p(k,E){E&16&&s.value!==k[4]&&M(s,k[4])},d(k){k&&j(t),y=!1,A(f)}}}function Pe(e){let t,l,n,o,r,i,s,p,d,h,a,c,y,f,k,E,H,b=e[5]&&e[5].length>0&&re(e);return{c(){t=m("div"),l=m("div"),n=m("form"),o=m("div"),r=m("div"),r.innerHTML=``,i=C(),s=m("input"),p=C(),d=m("div"),h=m("div"),h.innerHTML=``,a=C(),c=m("input"),y=C(),f=m("button"),f.innerHTML=``,k=C(),b&&b.c(),u(r,"class","tlwd-flex tlwd-items-center"),u(s,"id","open-webui-url-input"),u(s,"placeholder","Open WebUI URL"),u(s,"class","tlwd-p-0 tlwd-m-0 tlwd-text-xl tlwd-w-full tlwd-font-medium tlwd-bg-transparent tlwd-border-none placeholder:tlwd-text-gray-500 tlwd-text-neutral-100 tlwd-outline-none"),u(s,"autocomplete","one-time-code"),s.required=!0,u(o,"class","tlwd-flex tlwd-items-center tlwd-gap-2 tlwd-w-full"),u(h,"class","tlwd-flex tlwd-items-center"),u(c,"placeholder","Open WebUI API Key"),u(c,"class","tlwd-p-0 tlwd-m-0 tlwd-text-xl tlwd-w-full tlwd-font-medium tlwd-bg-transparent tlwd-border-none placeholder:tlwd-text-gray-500 tlwd-text-neutral-100 tlwd-outline-none"),u(c,"autocomplete","one-time-code"),c.required=!0,u(f,"class","tlwd-flex tlwd-items-center tlwd-bg-transparent tlwd-text-neutral-100 tlwd-cursor-pointer tlwd-p-0 tlwd-m-0 tlwd-outline-none tlwd-border-none"),u(f,"type","button"),u(d,"class","tlwd-flex tlwd-items-center tlwd-gap-2 tlwd-w-full tlwd-mt-2"),u(n,"class","tlwd-text-gray-200 tlwd-w-full tlwd-p-0 tlwd-m-0"),u(n,"autocomplete","off"),u(l,"class","tlwd-w-full tlwd-flex tlwd-flex-col tlwd-justify-between tlwd-py-2.5 tlwd-px-3.5 tlwd-rounded-2xl tlwd-outline tlwd-outline-1 tlwd-outline-gray-850 tlwd-backdrop-blur-3xl tlwd-bg-gray-850/70 shadow-4xl modal-animation"),u(t,"class","tlwd-m-auto tlwd-max-w-sm tlwd-w-full tlwd-pb-32")},m(x,g){L(x,t,g),w(t,l),w(l,n),w(n,o),w(o,r),w(o,i),w(o,s),M(s,e[2]),w(n,p),w(n,d),w(d,h),w(d,a),w(d,c),M(c,e[3]),w(d,y),w(d,f),w(n,k),b&&b.m(n,null),E||(H=[S(s,"input",e[9]),S(c,"input",e[10]),S(f,"click",e[11]),S(n,"submit",e[8]),S(n,"mousedown",De)],E=!0)},p(x,g){g&4&&s.value!==x[2]&&M(s,x[2]),g&8&&c.value!==x[3]&&M(c,x[3]),x[5]&&x[5].length>0?b?b.p(x,g):(b=re(x),b.c(),b.m(n,null)):b&&(b.d(1),b=null)},d(x){x&&j(t),b&&b.d(),E=!1,A(H)}}}function re(e){let t,l,n,o,r,i,s,p,d,h=te(e[5]),a=[];for(let c=0;c`,n=C(),o=m("select"),r=m("option"),r.textContent="Select a model";for(let c=0;c`,u(l,"class","tlwd-flex tlwd-items-center"),r.__value="",M(r,r.__value),u(o,"id","open-webui-model-input"),u(o,"class","tlwd-p-0 tlwd-m-0 tlwd-text-xl tlwd-w-full tlwd-font-medium tlwd-bg-transparent tlwd-border-none placeholder:tlwd-text-gray-500 tlwd-text-neutral-100 tlwd-outline-none"),u(o,"autocomplete","off"),o.required=!0,e[6]===void 0&&D(()=>e[12].call(o)),u(s,"class","tlwd-flex tlwd-items-center tlwd-bg-transparent tlwd-text-neutral-100 tlwd-cursor-pointer tlwd-p-0 tlwd-m-0 tlwd-outline-none tlwd-border-none"),u(s,"type","submit"),u(t,"class","tlwd-flex tlwd-items-center tlwd-gap-2 tlwd-w-full tlwd-mt-2")},m(c,y){L(c,t,y),w(t,l),w(t,n),w(t,o),w(o,r);for(let f=0;f{e.stopPropagation()},Ue=e=>{e.stopPropagation()};function Ve(e,t,l){let n=!1,o=!0,r="",i="",s="",p="",d=[];const h=()=>{console.log("resetConfig");try{chrome.storage.local.clear().then(()=>{console.log("Value is cleared")})}catch(g){console.log(g),localStorage.setItem("url",""),localStorage.setItem("key",""),localStorage.setItem("model","")}l(2,r=""),l(3,i=""),l(6,s=""),l(5,d=[]),l(1,o=!0)},a=g=>{g.preventDefault(),window.open(`${r}/?q=${encodeURIComponent(p)}&models=${s}`,"_blank"),l(4,p=""),l(0,n=!1)},c=g=>{g.preventDefault();try{chrome.storage.local.set({url:r,key:i,model:s}).then(()=>{console.log("Value is set")})}catch(z){console.log(z),localStorage.setItem("url",r),localStorage.setItem("key",i),localStorage.setItem("model",s)}l(1,o=!1)};Me(async()=>{let g=null;try{g=await chrome.storage.local.get()}catch(_){console.log(_)}g&&(l(2,r=g.url??""),l(3,i=g.key??""),l(6,s=g.model??""),g.url&&g.key&&g.model&&(l(5,d=await le(g.key,g.url).catch(_=>{console.log(_),h()})),d&&l(1,o=!1)));const z=async _=>{var G;if(n&&_.shiftKey&&_.key==="Escape"?h():_.key==="Escape"&&l(0,n=!1),_.key===" "&&(_.metaKey||_.ctrlKey)&&(_.shiftKey||_.altKey)){_.preventDefault();try{const v=await chrome.runtime.sendMessage({action:"getSelection"});((v==null?void 0:v.data)??!1)&&l(4,p=v.data)}catch(v){console.log("catch",v)}l(0,n=!n),setTimeout(()=>{const v=document.getElementById("open-webui-search-input");v&&v.focus()},0)}if(i!==""&&r!==""&&_.key==="Enter"&&(_.metaKey||_.ctrlKey)&&(_.shiftKey||_.altKey)){_.preventDefault();try{const v=await chrome.runtime.sendMessage({action:"getSelection"});if((v==null?void 0:v.data)??!1){await chrome.runtime.sendMessage({action:"writeText",text:` +`});const[U,Fe]=await He(i,{model:s,messages:[{role:"system",content:"You are a helpful assistant."},{role:"user",content:v.data}],stream:!0},((G=d.find(V=>V.id===s))==null?void 0:G.owned_by)==="openai"?`${r}/openai`:`${r}/ollama/v1`);if(U&&U.ok){const V=U.body.pipeThrough(new TextDecoderStream).pipeThrough(ze(` +`)).getReader();for(;;){const{value:he,done:ge}=await V.read();if(ge)break;try{let R=he.split(` +`);for(const K of R)if(K!=="")if(console.log(K),K==="data: [DONE]")console.log("DONE");else{let P=JSON.parse(K.replace(/^data: /,""));console.log(P),"request_id"in P?console.log(P.request_id):await chrome.runtime.sendMessage({action:"writeText",text:P.choices[0].delta.content??""})}}catch(R){console.log(R)}}}}}catch(v){console.log(v)}}};return document.addEventListener("keydown",z,{capture:!0}),()=>document.removeEventListener("keydown",z)});function y(){r=this.value,l(2,r)}function f(){i=this.value,l(3,i)}const k=async()=>{r.endsWith("/")&&l(2,r=r.slice(0,-1)),l(5,d=await le(i,r))};function E(){s=Ce(this),l(6,s),l(5,d)}function H(){p=this.value,l(4,p)}return[n,o,r,i,p,d,s,a,c,y,f,k,E,H,()=>{l(1,o=!0)},()=>{l(0,n=!1)}]}class Re extends me{constructor(t){super(),pe(this,t,Ve,qe,ce,{})}}function We(e){let t,l,n;return l=new Re({}),{c(){t=m("div"),Ae(l.$$.fragment),u(t,"class","extension-container")},m(o,r){L(o,t,r),fe(l,t,null),n=!0},p:$,i(o){n||(ae(l.$$.fragment,o),n=!0)},o(o){$e(l.$$.fragment,o),n=!1},d(o){o&&j(t),we(l)}}}class Ze extends me{constructor(t){super(),pe(this,t,null,We,ce,{})}}new Ze({target:document.getElementById("extension-app")}); diff --git a/extension/src/lib/apis/index.js b/extension/src/lib/apis/index.js index 239f313..546a26b 100644 --- a/extension/src/lib/apis/index.js +++ b/extension/src/lib/apis/index.js @@ -1,12 +1,12 @@ -export const getOpenAIModels = async (token = "", url = "") => { +export const getModels = async (key, url) => { let error = null; - const res = await fetch(`${url}/models`, { + const res = await fetch(`${url}/api/models`, { method: "GET", headers: { Accept: "application/json", "Content-Type": "application/json", - ...(token && { authorization: `Bearer ${token}` }), + ...(key && { authorization: `Bearer ${key}` }), }, }) .then(async (res) => { @@ -14,48 +14,36 @@ export const getOpenAIModels = async (token = "", url = "") => { return res.json(); }) .catch((err) => { - error = `OpenAI: ${err?.error?.message ?? "Network Problem"}`; - return []; + console.log(err); + error = err; + return null; }); if (error) { throw error; } - const models = Array.isArray(res) ? res : res?.data ?? null; - - return models - ? models - .map((model) => ({ - id: model.id, - name: model.name ?? model.id, - url: url, - custom_info: model.custom_info, - })) - .sort((a, b) => { - return a.name.localeCompare(b.name); - }) - : models; -}; - -export const getModels = async (key, url) => { - let models = await Promise.all([ - getOpenAIModels(key, `${url}/ollama/v1`).catch((error) => { - console.log(error); - return null; - }), - getOpenAIModels(key, `${url}/openai/api`).catch((error) => { - console.log(error); - return null; - }), - ]); + let models = res?.data ?? []; models = models .filter((models) => models) - .reduce((a, e, i, arr) => a.concat(e), []); + .sort((a, b) => { + // Compare case-insensitively + const lowerA = a.name.toLowerCase(); + const lowerB = b.name.toLowerCase(); + + if (lowerA < lowerB) return -1; + if (lowerA > lowerB) return 1; + + // If same case-insensitively, sort by original strings, + // lowercase will come before uppercase due to ASCII values + if (a < b) return -1; + if (a > b) return 1; + + return 0; // They are equal + }); console.log(models); - return models; }; diff --git a/extension/src/lib/components/SpotlightSearch.svelte b/extension/src/lib/components/SpotlightSearch.svelte index 806d1c3..9766a29 100644 --- a/extension/src/lib/components/SpotlightSearch.svelte +++ b/extension/src/lib/components/SpotlightSearch.svelte @@ -31,6 +31,7 @@ url = ""; key = ""; model = ""; + models = []; showConfig = true; }; @@ -80,8 +81,16 @@ key = _storageCache.key ?? ""; model = _storageCache.model ?? ""; if (_storageCache.url && _storageCache.key && _storageCache.model) { - models = await getModels(_storageCache.key, _storageCache.url); - showConfig = false; + models = await getModels(_storageCache.key, _storageCache.url).catch( + (error) => { + console.log(error); + resetConfig(); + } + ); + + if (models) { + showConfig = false; + } } } @@ -161,7 +170,11 @@ ], stream: true, }, - models.find((m) => m.id === model)?.url + + models.find((m) => m.id === model)?.owned_by === "openai" ?? + false + ? `${url}/openai` + : `${url}/ollama/v1` ); if (res && res.ok) {