wiki-plugin-similarity 0.22.1 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/similarity.js +55 -54
- package/client/similarity.js.map +3 -3
- package/package.json +1 -1
- package/pages/about-similarity-plugin +16 -0
- package/pages/search-algorithm +3 -3
- package/pages/search-tool +2 -2
- package/server/server.js +1 -1
package/client/similarity.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
/* wiki-plugin-similarity - 0.
|
|
2
|
-
(()=>{var dt={high:.78,medium:.68,low:.58},ct=dt.medium,
|
|
3
|
-
`)){let
|
|
4
|
-
`).slice(0,2e3);return{slug:
|
|
5
|
-
`)){let s=n.trim();if(!s||s.startsWith("#"))continue;let[
|
|
6
|
-
`),
|
|
1
|
+
/* wiki-plugin-similarity - 0.23.0 - Sat, 05 Sep 2026 00:40:14 GMT */
|
|
2
|
+
(()=>{var dt={high:.78,medium:.68,low:.58},ct=dt.medium,kt=10,ht=t=>{let e=[],n=[],s=[],i=null,l=null,c="search",w=!1,k=!1,g=!1,T=null,O=null,D=null,P=null,a=(r,h)=>r===h||r.startsWith(h)&&/^[\s:]/.test(r.slice(h.length)),E=(r,h)=>r.slice(h.length).replace(/^\s*:?\s*/,"").trim();for(let r of t.split(`
|
|
3
|
+
`)){let h=r.trim();if(!h||h.startsWith("#"))continue;let x=h.toUpperCase();if(a(x,"LIVE")){w=!0;continue}if(a(x,"SUBJECT")){k=!0;continue}if(a(x,"AUTHOR")){!e.length&&c==="search"&&(c="author");continue}if(a(x,"REPORT")){c==="search"&&(c="report");continue}if(a(x,"KEYWORD")){c==="search"&&(c="keyword");continue}if(a(x,"SITES")){c==="search"&&(c="sites");continue}if(a(x,"BUILD")){c==="search"&&(c="build");continue}if(a(x,"FORCE")){g=!0;continue}if(a(x,"GHOST")){T=E(h,"GHOST"),c==="search"&&(c="ghost");continue}if(a(x,"BUTTON")){O=E(h,"BUTTON");continue}if(a(x,"BATCH")){let C=E(h,"BATCH").toLowerCase();D=C==="off"||C==="no"||C==="0"?0:parseInt(C)||100;continue}if(a(x,"ALGORITHM")){P=E(h,"ALGORITHM").replace(/^\[\[|\]\]$/g,"").trim()||"search-algorithm",c==="search"&&!e.length&&(c="algorithm");continue}if(a(x,"ROSTER")){let C=E(h,"ROSTER");C&&n.push(C);continue}if(a(x,"FARM")){let C=E(h,"FARM");C&&s.push(C);continue}if(a(x,"LIST")){!e.length&&c==="search"&&(c="list");continue}if(a(x,"STATUS")){c==="search"&&(c="status");continue}if(a(x,"SIMILAR")){let C=E(x,"SIMILAR").toLowerCase();i=dt[C]||ct,!e.length&&c==="search"&&(c="similar");continue}if(a(x,"THRESHOLD")){let C=E(h,"THRESHOLD");i=dt[C.toLowerCase()]??(parseFloat(C)||ct);continue}if(a(x,"LIMIT")){l=parseInt(E(h,"LIMIT"))||kt;continue}e.push(["PUBLIC","LOCAL","PRIVATE","GALAXY","SITE"].includes(x)?x:h)}return{mode:c,specs:e,rosterRefs:n,farms:s,threshold:i??ct,limit:l??kt,live:w,subject:k,force:g,ghostUrl:T,label:O,batch:D,algorithm:P,thresholdSet:i!==null}},et=t=>t.includes("*")||t.includes("?"),nt=t=>t==="PUBLIC"||t==="LOCAL"||t==="PRIVATE"||t==="GALAXY";var Et=t=>t.toLowerCase().replace(/\s+/g,"-").replace(/[^a-z0-9-]/g,""),ut=new Map,oe=async(t,e)=>{if(ut.has(t))return ut.get(t);let n=`${e}/system/indexed-domains.json?pattern=${encodeURIComponent(t)}`,s=await fetch(n);if(!s.ok)throw new Error(`indexed-domains failed: ${s.status}`);let i=await s.json();return ut.set(t,i),i},Q=async(t,e)=>{t.length||(t=[window.location.hostname]);let n=new Set,s=[];for(let i of t)if(i==="*"||et(i)||nt(i))for(let l of await oe(i,e))n.has(l.domain)||(n.add(l.domain),s.push(l));else n.has(i)||(n.add(i),s.push({domain:i,page_count:null}));return s},ie=/^([a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+|localhost)(:\d+)?$/,ae=/^ROSTER ([A-Za-z0-9.\-:]+\/[a-z0-9-]+)$/,re=/^REFERENCES ([A-Za-z0-9.\-:]+\/[a-z0-9-]+)$/,Lt=async t=>{let e=t.indexOf("/"),n=await fetch(`//${t.slice(0,e)}/${t.slice(e+1)}.json`);if(!n.ok)throw new Error(`roster page ${t} failed: ${n.status}`);return n.json()},jt=async(t,e,n)=>{if(n.has(t))return;n.add(t);let s;try{s=await Lt(t)}catch(i){console.warn(i.message);return}for(let i of s.story||[])if(i.type==="roster")for(let l of(i.text||"").split(/\r?\n/)){let c=l.trim();if(!c)continue;let w=c.match(ie);if(w){e.add(w[0]);continue}let k=c.match(ae);if(k){await jt(k[1],e,n);continue}let g=c.match(re);if(g)try{let T=await Lt(g[1]);for(let O of T.story||[])O.type==="reference"&&O.site&&e.add(O.site)}catch(T){console.warn(T.message)}}},le=async t=>{let e=new Set,n=new Set;for(let s of t)await jt(s,e,n);return[...e]},Ct=async(t,e)=>{let n=typeof window<"u"&&window.location?window.location.hostname:null,s=t.map(i=>i.toUpperCase()==="SITE"&&n?n:i);return e.length?[...s,...await le(e)]:s};var mt=new Map,ce=t=>`${window.location.origin}/system/semantic-vectors.json?domain=${encodeURIComponent(t)}`,de=async t=>{if(mt.has(t))return mt.get(t);let e=await fetch(ce(t));if(!e.ok)return[];let n=await e.json();return mt.set(t,n),n},ft=async(t,e)=>{let n=await fetch(`${e}/system/embed.json`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({text:t})});if(!n.ok)throw new Error(`embed failed: ${n.status}`);return(await n.json()).vector};var pt=(t,e,{threshold:n,limit:s,excludeSlug:i,excludeDomain:l})=>{let c=[];for(let{domain:w,pages:k}of e)for(let{slug:g,title:T,vector:O}of k){if(g===i&&w===l)continue;let D=0;for(let P=0;P<t.length;P++)D+=t[P]*O[P];D>=n&&c.push({domain:w,slug:g,title:T,score:D})}return c.sort((w,k)=>k.score-w.score),c.slice(0,s)},gt=async(t,e)=>{let n=await Q(t,e);return(await Promise.all(n.map(async({domain:i})=>({domain:i,pages:await de(i)})))).filter(i=>i.pages.length>0)};var It=t=>{let e=t.parents(".page:first"),n=e.prev(".page"),s=n.length?n:e,i=(s.attr("id")||"").split("_rev")[0],l=s.data("site")||window.location.hostname,c=s.data("data")||{},w=c.title||s.find(".title").text().trim()||i,k=[w,...(c.story||[]).filter(g=>g.type==="markdown"||g.type==="paragraph").map(g=>(g.text||"").trim()).filter(Boolean)].join(`
|
|
4
|
+
`).slice(0,2e3);return{slug:i,site:l,title:w,text:k,isSelf:!n.length}},V=t=>`Subject: ${t.title}${t.isSelf?" (this page)":` @ ${t.site}`}`;var Tt=t=>`sim-cache-${t}`,At=t=>{try{let e=JSON.parse(localStorage.getItem(Tt(t.id))||"null");return e?.text===(t.text||"")?e:null}catch{return null}},Z=(t,e)=>{try{localStorage.setItem(Tt(t.id),JSON.stringify({text:t.text||"",ts:Date.now(),...e}))}catch{}},wt=t=>{let e=Math.floor((Date.now()-t)/1e3);return e<60?`${e}s ago`:e<3600?`${Math.floor(e/60)}m ago`:e<86400?`${Math.floor(e/3600)}h ago`:`${Math.floor(e/86400)}d ago`};var q=t=>`${t.site} ${t.slug}`,he=(t,e,n)=>{let s=new Set(t.map(q));return e.slice(0,n).filter(i=>!s.has(q(i))).length},ue=(t,e,n,s)=>{let i=new Map;t.forEach((T,O)=>{s.has(q(T))&&i.set(O,T)});let l=new Set([...i.values()].map(q)),c=e.filter(T=>!l.has(q(T))),w=[],k=0,g=Math.max(n,i.size?Math.max(...i.keys())+1:0);for(let T=0;T<g;T++)i.has(T)?w.push(i.get(T)):k<c.length&&w.push(c[k++]);return w},me=(t,e)=>{let n=new Map(e.map(s=>[q(s),s]));return t.map(s=>n.get(q(s))||s)},fe=()=>{let t=new Set;try{for(let e of Object.keys(window.wiki?.neighborhood||{}))t.add(e);document.querySelectorAll(".page").forEach(e=>{let n=window.$?window.$(e).data("site"):null;t.add(n||window.location.hostname)})}catch{}return[...t]},Rt=async(t,e)=>{let n=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!n.ok){let s=new Error(`${t.split("/").pop()} failed: ${n.status}`);throw s.status=n.status,s.body=await n.json().catch(()=>({})),s}return n.json()},pe=(t,e,n)=>{let s=t.slice(0,n).map(q).join("|"),i=e.slice(0,n).map(q).join("|");return s===i},Ot=async t=>{let{origin:e,query:n,seeded:s={},specs:i,limit:l,batch:c,algorithm:w,render:k,resume:g}=t,T=A=>t.status&&t.status(A),O;if(g&&Array.isArray(g.remaining))O={batches:g.remaining,count:g.total,sites:g.sites||[],siteIndex:g.siteIndex||null,unindexed:g.unindexed||[]};else{let A={roster:[...t.roster||[],...w&&w.liked||[]],neighborhood:fe(),followed:!0,...t.prefer||{}};T("Ranking sites\u2026"),O=await Rt(`${e}/system/site-rank.json`,{query:n,...s,domains:i,prefer:A,algorithm:w||{},batch:c||100})}let D=O.batches||[],P=O.count||D.reduce((A,u)=>A+u.length,0),a={searched:g?.searched||0,total:P,batches:D.length,done:0,stopped:!1,converged:!1,running:!0,unindexed:O.unindexed||[],answered:g?.answered||{local:0},siteIndex:O.siteIndex||null,siteInfo:new Map((O.sites||[]).map(A=>[A.domain,A])),shown:g?.shown||[],pending:0,opened:new Set(g?.opened||[]),resumed:!!g},E=new Map((g?.merged||[]).map(A=>[q(A),A])),r=[...E.values()].sort((A,u)=>u.score-A.score),h=0,x=()=>{a.stopped=!0};a.stop=x;let C=()=>{try{t.save&&t.save(a.snapshot())}catch{}};a.foldIn=()=>{a.shown=ue(a.shown,r,l,a.opened),a.pending=0,k(r,a),C()},a.markOpened=A=>{a.opened.add(A),k(r,a),C()},a.snapshot=()=>({query:n,total:P,searched:a.searched,converged:a.converged,stopped:a.stopped,merged:r.slice(0,150),shown:a.shown,opened:[...a.opened],remaining:D.slice(a.done),answered:a.answered,siteIndex:a.siteIndex,unindexed:a.unindexed,sites:[...a.siteInfo.values()].slice(0,400)});let X=async A=>{let u={query:n,...s,domains:A,limit:Math.max(l*3,30),flat:!0};t.thresholdSet&&(u.threshold=t.threshold);let y=await Rt(`${e}/system/search-report.json`,u);for(let o of y.results||[]){let p=q(o),S=E.get(p);(!S||S.score<o.score)&&E.set(p,o)}let d=0;for(let o of y.peers||[])a.answered[o.host]=(a.answered[o.host]||0)+(o.domains||0),d+=o.domains||0;a.answered.local+=Math.max(0,(y.stats&&y.stats.domains||0)-d);let f=[...E.values()].sort((o,p)=>p.score-o.score),m=pe(r,f,l);r=f,a.searched+=A.length,a.done+=1,h=m?h+1:0,a.shown.length?a.shown=me(a.shown,f):a.shown=f.slice(0,l),a.pending=he(a.shown,f,l)},Y=()=>D.slice(a.done),tt=async(A=!1)=>{for(a.running=!0,a.stopped=!1;a.done<D.length&&!a.stopped;)if(await X(D[a.done]),k(r,a),C(),!A&&h>=3&&a.done<D.length){a.converged=!0;break}return a.running=!1,k(r,a),C(),r};return a.continueAll=()=>(a.converged=!1,h=-1/0,tt(!0)),g&&g.remaining&&!g.remaining.length?(a.running=!1,a.converged=!!g.converged,k(r,a),{results:r,state:a,remaining:Y}):(await tt(!1),{results:r,state:a,remaining:Y})};var ot="similarity:learned",ge=new Set(["liked","visited","neighbourhood","neighborhood","followed","centroid","fresh","reliable","stale"]),st=/^([a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+|localhost)(:\d+)?$/,yt=t=>{let e={weights:{},always:[],never:[],batch:null,trust:null};for(let n of(t||"").split(`
|
|
5
|
+
`)){let s=n.trim();if(!s||s.startsWith("#"))continue;let[i,...l]=s.split(/\s+/),c=i.toUpperCase().replace(/:$/,"");if(c==="WEIGHT"&&l.length>=2){let w=l[0].toLowerCase().replace("neighborhood","neighbourhood"),k=parseFloat(l[1]);ge.has(w)&&Number.isFinite(k)&&(e.weights[w]=k)}else c==="ALWAYS"&&l[0]&&st.test(l[0])?e.always.push(l[0].toLowerCase()):c==="NEVER"&&l[0]&&st.test(l[0])?e.never.push(l[0].toLowerCase()):c==="BATCH"&&l[0]?e.batch=parseInt(l[0])||null:c==="TRUST"&&l[0]&&["solid","flaky","any"].includes(l[0].toLowerCase())&&(e.trust=l[0].toLowerCase())}return e},$t=()=>{try{let t=JSON.parse(localStorage.getItem(ot)||"{}"),e={};for(let[n,s]of Object.entries(t.sites||t)){let i=typeof s=="number"?s:s&&typeof s.score=="number"?s.score:0;i>0&&(e[n.toLowerCase()]=Math.min(1,i))}return e}catch{return{}}},we=(t,e)=>{if(!t)return`${e}/search-algorithm.json`;let n=t.indexOf("/");if(n>0&&st.test(t.slice(0,n)))return`//${t.slice(0,n)}/${t.slice(n+1)}.json`;let s=t.toLowerCase().replace(/\s+/g,"-").replace(/[^a-z0-9-]/g,"");return`${e}/${s}.json`},ye=t=>{let e=new Set;for(let n of t.story||[])if(n.type==="roster")for(let s of(n.text||"").split(/\r?\n/)){let i=s.trim();st.test(i)&&e.add(i.toLowerCase())}return[...e]},Dt=new Map,Mt=async(t,e)=>{let n=we(t,e),s=Dt.get(n);if(!s){s={weights:{},always:[],never:[],liked:[],batch:null,trust:null,page:null};try{let i=await fetch(n);if(i.ok){let l=await i.json(),c=(l.story||[]).find(w=>w.type==="similarity"&&/^\s*ALGORITHM\b/im.test(w.text||""));c&&Object.assign(s,yt(c.text)),s.liked=ye(l),s.page={url:n,title:l.title}}}catch{}Dt.set(n,s)}return{...s,learned:$t()}};var $e={neighbourhood:.1,visited:.2,clicked:.35},be=7*864e5,Pt=200,qt=()=>{try{return JSON.parse(localStorage.getItem(ot)||"{}").sites||{}}catch{return{}}},Ft=t=>{try{localStorage.setItem(ot,JSON.stringify({sites:t,savedAt:Date.now()}))}catch{}},it=(t,e)=>{let n=Math.max(0,(e-(t.at||e))/be);return(t.score||0)*Math.pow(.5,n)},at=(t,e)=>{if(!t||t==="localhost")return 0;let n=t.toLowerCase(),s=Date.now(),i=qt(),l=i[n]?it(i[n],s):0,c=Math.min(1,l+($e[e]||.1));i[n]={score:c,at:s};let w=Object.keys(i);if(w.length>Pt){w.sort((k,g)=>it(i[g],s)-it(i[k],s));for(let k of w.slice(Pt))delete i[k]}return Ft(i),c},xe=()=>{let t=Date.now();return Object.entries(qt()).map(([e,n])=>[e,it(n,t)]).filter(([,e])=>e>.01).sort((e,n)=>n[1]-e[1])},Nt=()=>Ft({}),Ut=!1,Ht=t=>{try{return window.$&&window.$(t).data("site")||window.location.hostname}catch{return window.location.hostname}},Bt=()=>{if(Ut||typeof document>"u")return;Ut=!0;let t=new Set;document.querySelectorAll(".main > .page").forEach(n=>{let s=Ht(n);t.has(s)||(t.add(s),at(s,"neighbourhood"))});let e=document.querySelector(".main");!e||typeof MutationObserver>"u"||new MutationObserver(n=>{for(let s of n)for(let i of s.addedNodes)i.nodeType===1&&i.classList.contains("page")&&!i.classList.contains("ghost")&&at(Ht(i),"visited")}).observe(e,{childList:!0})},_t=(t,e=20)=>new Promise((n,s)=>{let l=((t.data("data")||{}).story||[]).find(g=>g.type==="roster"&&/^\s*sites i visit\b/i.test(g.text||""));if(!l)return s(new Error('no "Sites I visit" roster on this page'));let c=xe().slice(0,e);if(!c.length)return s(new Error("nothing learned yet"));let w=["Sites I visit",...c.map(([g])=>g)].join(`
|
|
6
|
+
`),k={...l,text:w};try{window.wiki.pageHandler.put(t,{type:"edit",id:l.id,item:k});let g=t.find(`.item[data-id="${l.id}"]`);g.length&&(g.data("item",k),window.wiki.plugins?.roster?.emit?.(g,k)),n(c.length)}catch(g){s(g)}});var z=`.sim-results li.sim-opened{opacity:.72}.sim-results .sim-mark{color:#6a6;margin-left:.3em}.sim-results li.sim-pending{list-style:none;margin-left:-1.2em}.sim-results .sim-fold{font-size:90%;padding:0 .4em}
|
|
7
|
+
|
|
7
8
|
.sim-form { display:flex; gap:6px; margin-bottom:8px; }
|
|
8
9
|
.sim-input { flex:1; padding:6px 8px; font-size:14px; border:1px solid #ccc; border-radius:3px; }
|
|
9
10
|
.sim-btn { padding:6px 14px; background:#c4561d; color:white; border:none;
|
|
@@ -27,97 +28,97 @@
|
|
|
27
28
|
.sim-list td { padding:3px 8px 3px 0; border-bottom:1px solid #f0f0f0; vertical-align:middle; }
|
|
28
29
|
.sim-list td:last-child { text-align:right; color:#999; font-size:11px; }
|
|
29
30
|
.sim-list .sim-flag { margin-right:6px; }
|
|
30
|
-
`,
|
|
31
|
+
`,zt=(t,e)=>`<img class="sim-flag remote" src="${window.wiki.site(t).flag()}"
|
|
31
32
|
title="${t} \u2014 score ${e.toFixed(3)}"
|
|
32
|
-
data-site="${t}">`;var
|
|
33
|
-
`)}),s.push({type:"markdown",id:n(),text:"# Results"});for(let l of e)s.push({type:"reference",id:n(),site:l.site,slug:l.slug,title:l.title,text:(l.synopsis||`score ${l.score}`)+(l.via?` \u2014 via ${l.via}`:"")});return{title:`${t} Federated Search`,story:s}},
|
|
34
|
-
<style>${
|
|
33
|
+
data-site="${t}">`;var Se="Search Tool",ve="search-tool",rt=null,ke=()=>fetch(`/${ve}.json`,{method:"GET"}).then(t=>{rt=t.ok}).catch(()=>{rt=!1}),Le=t=>{let e=window.$(t).parents(".page");return e.length?e:null},Gt=(t,e)=>{window.wiki.doInternalLink(Se,e?null:t)},Kt=()=>{typeof document>"u"||!window.wiki||!window.$||(ke(),document.addEventListener("click",t=>{if(rt!==!0)return;let e=t.target&&t.target.closest&&t.target.closest("a.search");if(!e)return;let n=Le(e);n&&(t.preventDefault(),t.stopPropagation(),Gt(n,t.shiftKey))},!0),document.addEventListener("keydown",t=>{if(rt!==!0||t.key!=="Enter")return;let e=t.target;if(!e||!e.classList||!e.classList.contains("search")||!e.closest(".searchbox"))return;let n=(e.value||"").trim();n&&(t.preventDefault(),t.stopPropagation(),window.$(".incremental-search").remove(),e.value="",Ee(n),Gt(window.$(".page").last(),!1))},!0))},bt=null,Ee=t=>{bt=t},Vt=()=>{let t=bt;return bt=null,t};var lt=(t,e,n,s)=>`<a class="sim-link" data-title="${n}" data-slug="${e}" data-site="${t}" href="#">${zt(t,s)} ${n}</a>`,Yt=t=>{try{return new Date(t).toLocaleDateString(void 0,{day:"numeric",month:"short"})}catch{return""}},je=t=>{if(!t||!t.feed)return"";let e=t.by||{},s=["abandoned","stale","dead","moved","lapsed","unreliable","flaky"].filter(w=>e[w]).map(w=>`${e[w].toLocaleString()} ${w}`),i=String(t.feed).replace(/^https?:\/\//,""),l=t.fetchedAt?new Date(t.fetchedAt).toLocaleString(void 0,{day:"numeric",month:"short",hour:"2-digit",minute:"2-digit"}):"\u2014";return`<tr><th>Graveyard verdicts</th><td>${t.sites?`${t.sites.toLocaleString()} sites${s.length?` \u2014 ${s.join(", ")}`:""}`:"none yet"} \u2014 read from <code>${i}</code>'s sitemap, ${l}</td></tr>`},Ce=(t,e)=>{let n=()=>Math.random().toString(16).slice(2,18).padEnd(16,"0"),s=[{type:"markdown",id:n(),text:`Federated search for **${t}** \u2014 ${e.length} results, searched a batch at a time, nearest sites first.`}],i=[...new Set(e.map(l=>l.title))];i.length&&s.push({type:"markdown",id:n(),text:i.map(l=>`- [[${l}]]`).join(`
|
|
34
|
+
`)}),s.push({type:"markdown",id:n(),text:"# Results"});for(let l of e)s.push({type:"reference",id:n(),site:l.site,slug:l.slug,title:l.title,text:(l.synopsis||`score ${l.score}`)+(l.via?` \u2014 via ${l.via}`:"")});return{title:`${t} Federated Search`,story:s}},Ie=(t,e)=>{let{mode:n,specs:s,threshold:i,limit:l,force:c,ghostUrl:w,label:k}=ht(e?.text||"");if(n==="ghost")t.html(`
|
|
35
|
+
<style>${z}</style>
|
|
35
36
|
<div class="similarity" data-id="${e.id}" data-mode="${n}" data-scope="${s.join(",")||""}">
|
|
36
37
|
<div class="sim-form">
|
|
37
|
-
<button class="sim-btn">${
|
|
38
|
+
<button class="sim-btn">${k||"Open"}</button>
|
|
38
39
|
</div>
|
|
39
40
|
<div class="sim-status"></div>
|
|
40
41
|
</div>`);else if(n==="build")t.html(`
|
|
41
|
-
<style>${
|
|
42
|
+
<style>${z}</style>
|
|
42
43
|
<div class="similarity" data-id="${e.id}" data-mode="${n}" data-scope="${s.join(",")||""}">
|
|
43
44
|
<div class="sim-form">
|
|
44
|
-
<button class="sim-btn">${
|
|
45
|
+
<button class="sim-btn">${k||`Index ${s.length?s.join(", "):"*"}${c?" (force)":""}`}</button>
|
|
45
46
|
</div>
|
|
46
47
|
<div class="sim-status"></div>
|
|
47
|
-
</div>`);else if(n==="list"){let
|
|
48
|
-
<style>${
|
|
48
|
+
</div>`);else if(n==="list"){let g=s.length?s.join(", "):"*";t.html(`
|
|
49
|
+
<style>${z}</style>
|
|
49
50
|
<div class="similarity" data-id="${e.id}" data-mode="${n}" data-scope="${s.join(",")||""}">
|
|
50
|
-
<div class="sim-status">Loading indexed domains (${
|
|
51
|
+
<div class="sim-status">Loading indexed domains (${g})\u2026</div>
|
|
51
52
|
<div class="sim-list"></div>
|
|
52
53
|
</div>`)}else if(n==="algorithm")t.html(`
|
|
53
|
-
<style>${
|
|
54
|
+
<style>${z}</style>
|
|
54
55
|
<div class="similarity" data-id="${e.id}" data-mode="${n}" data-scope="">
|
|
55
56
|
<div class="sim-status">Reading this search algorithm\u2026</div>
|
|
56
57
|
<div class="sim-list"></div>
|
|
57
58
|
</div>`);else if(n==="status")t.html(`
|
|
58
|
-
<style>${
|
|
59
|
+
<style>${z}</style>
|
|
59
60
|
<div class="similarity" data-id="${e.id}" data-mode="${n}" data-scope="${s.join(",")||""}">
|
|
60
61
|
<div class="sim-status">Reading the state of the index\u2026</div>
|
|
61
62
|
<div class="sim-list"></div>
|
|
62
|
-
</div>`);else if(n==="similar"){let
|
|
63
|
-
<style>${
|
|
63
|
+
</div>`);else if(n==="similar"){let g=s.length?s.join(", "):"current domain";t.html(`
|
|
64
|
+
<style>${z}</style>
|
|
64
65
|
<div class="similarity" data-id="${e.id}" data-mode="${n}" data-scope="${s.join(",")||""}">
|
|
65
|
-
<div class="sim-status">Finding similar pages across ${
|
|
66
|
+
<div class="sim-status">Finding similar pages across ${g}\u2026</div>
|
|
66
67
|
<div class="sim-results"></div>
|
|
67
|
-
</div>`)}else if(n==="author"||n==="report"||n==="keyword"||n==="sites"){let
|
|
68
|
-
<style>${
|
|
68
|
+
</div>`)}else if(n==="author"||n==="report"||n==="keyword"||n==="sites"){let g=s.length?s.join(", "):"(current domain)",T=n==="report"?"Report":n==="keyword"?"Keyword":n==="sites"?"Sites":"Author",O=n==="sites"?"Where should this page go? Title + first paragraph\u2026":"Search wiki pages\u2026";t.html(`
|
|
69
|
+
<style>${z}</style>
|
|
69
70
|
<div class="similarity" data-id="${e.id}" data-mode="${n}" data-scope="${s.join(",")||""}">
|
|
70
71
|
<div class="sim-form">
|
|
71
|
-
<input class="sim-input" type="text" placeholder="${
|
|
72
|
-
<button class="sim-btn">${
|
|
72
|
+
<input class="sim-input" type="text" placeholder="${O}" />
|
|
73
|
+
<button class="sim-btn">${T}</button>
|
|
73
74
|
</div>
|
|
74
|
-
<div class="sim-status">Domains: ${
|
|
75
|
+
<div class="sim-status">Domains: ${g}</div>
|
|
75
76
|
<div class="sim-results"></div>
|
|
76
|
-
</div>`)}else{let
|
|
77
|
-
<style>${
|
|
77
|
+
</div>`)}else{let g=s.length?s.join(", "):"(current domain)";t.html(`
|
|
78
|
+
<style>${z}</style>
|
|
78
79
|
<div class="similarity" data-id="${e.id}" data-mode="${n}" data-scope="${s.join(",")||""}">
|
|
79
80
|
<div class="sim-form">
|
|
80
81
|
<input class="sim-input" type="text" placeholder="Search wiki pages\u2026" />
|
|
81
82
|
<button class="sim-btn">Search</button>
|
|
82
83
|
</div>
|
|
83
|
-
<div class="sim-status">Domains: ${
|
|
84
|
+
<div class="sim-status">Domains: ${g}</div>
|
|
84
85
|
<div class="sim-results"></div>
|
|
85
|
-
</div>`)}},
|
|
86
|
-
<p>${
|
|
86
|
+
</div>`)}},Te=(t,e)=>{let{mode:n,specs:s,rosterRefs:i,farms:l,threshold:c,limit:w,live:k,subject:g,force:T,ghostUrl:O,thresholdSet:D,batch:P,algorithm:a}=ht(e?.text||""),E=window.location.origin,r=t.find(".sim-status")[0];Bt();let h=g?It(t):null,x=k||h?null:At(e),C=Ct(s,i),X=()=>h?{seed:{site:h.site,slug:h.slug},text:h.text,excludePage:{site:h.site,slug:h.slug}}:{},Y=(u,y,d)=>{let f=y>0?`${y.toLocaleString()} pages across ${d} domains`:`${d} domain${d===1?"":"s"}`,m=[`${u} \u2014 ${f}`];return D&&m.push(`threshold ${c}`),m.push(`limit ${w}`),k&&m.push("LIVE"),m.join(" \xB7 ")};t.on("dblclick",u=>{$(u.target).closest(".sim-input").length||window.wiki.textEditor(t,e)}),t.on("click",".sim-link",function(u){u.preventDefault();let y=$(this),d=u.shiftKey?null:t.parents(".page");window.wiki.doInternalLink(y.data("title"),d,y.data("site"))});let tt=!s.length||s.length===1&&s[0]==="*"?"on farm":s.length===1?`on ${s[0]}`:"in domains",A=u=>u?` \xB7 cached ${wt(u)}`:"";if(n==="status"){let u=t.find(".sim-list")[0],y=m=>{if(!m)return"unknown";let o=(Date.now()-new Date(m))/864e5;return o<1?"today":o<2?"yesterday":`${Math.floor(o)} days ago`},d=m=>m?new Date(m).toISOString().slice(0,10):"\u2014",f=m=>{if(!m||!m.warm)return"";let o=m.warm,p=Math.round((m.bytes||0)/1e6),S=o.state==="warm"?`warm \u2014 ${o.done.toLocaleString()} sites resident in ${Math.round(o.ms/1e3)} s`:o.state==="warming"?`warming \u2014 ${o.done.toLocaleString()} of ${o.total.toLocaleString()} sites so far`:"not warmed",R=o.capped?" \xB7 <strong>cap reached</strong>, some sites re-parse per query":"";return`<tr><th>Vector store</th><td>${(m.pages||0).toLocaleString()} pages \xB7 ${p} MB of ${Math.round((m.capBytes||0)/1e6)} MB \xB7 ${S}${R}</td></tr>`};(async()=>{try{let m=()=>fetch(`${E}/system/embed.json?text=ping`).then(async v=>v.ok?{ok:!0}:{ok:!1,detail:((await v.json().catch(()=>({}))).error||`status ${v.status}`).replace(/^embedding unavailable: /,"")}).catch(v=>({ok:!1,detail:v.message})),[o,p,S]=await Promise.all([fetch(`${E}/system/similarity-health.json`).then(v=>v.json()),fetch(`${E}/system/indexed-domains.json`).then(v=>v.ok?v.json():[]),m()]),R=o.embedder||{},M=p.map(v=>v.built).filter(Boolean).sort(),b=p.reduce((v,H)=>v+(H.page_count||0),0),j=!S.ok,F=j?`<strong>Semantic search is down</strong> \u2014 a query cannot be turned into a vector right now (${S.detail}), so searches on this page will answer by keyword instead.`:"Semantic search is <strong>up</strong> \u2014 checked just now by embedding a word.",N=R.via==="url"?`delegated to <code>${R.url}</code> (from ${R.source||"config"})`:R.via==="semindex"?"in this process, via the SemIndex plugin":"in this process, in a supervised child";r.style.display="none",u.innerHTML=`<h3>The Index Behind These Answers</h3>
|
|
87
|
+
<p>${F}</p>
|
|
87
88
|
<table>
|
|
88
|
-
<tr><th>Model</th><td>${
|
|
89
|
-
<tr><th>Embedder</th><td>${
|
|
90
|
-
<tr><th>Indexed</th><td>${
|
|
91
|
-
<tr><th>Newest page vectors</th><td>${
|
|
92
|
-
<tr><th>Oldest page vectors</th><td>${
|
|
93
|
-
<tr><th>Plugin</th><td>wiki-plugin-similarity ${
|
|
94
|
-
${f(
|
|
95
|
-
${
|
|
89
|
+
<tr><th>Model</th><td>${o.model||"\u2014"} \xB7 ${o.dim||"\u2014"} dimensions</td></tr>
|
|
90
|
+
<tr><th>Embedder</th><td>${N}${j?" \u2014 <strong>currently down</strong>":""}</td></tr>
|
|
91
|
+
<tr><th>Indexed</th><td>${p.length.toLocaleString()} domains \xB7 ${b.toLocaleString()} pages</td></tr>
|
|
92
|
+
<tr><th>Newest page vectors</th><td>${d(M[M.length-1])} (${y(M[M.length-1])})</td></tr>
|
|
93
|
+
<tr><th>Oldest page vectors</th><td>${d(M[0])} (${y(M[0])})</td></tr>
|
|
94
|
+
<tr><th>Plugin</th><td>wiki-plugin-similarity ${o.version||"\u2014"}</td></tr>
|
|
95
|
+
${f(o.store)}
|
|
96
|
+
${je(o.verdicts)}
|
|
96
97
|
</table>
|
|
97
98
|
<p class="sim-count">A domain's vectors are only rebuilt when its pages change, so an old
|
|
98
99
|
date means a quiet site, not a broken one. Pages saved since the last build are findable
|
|
99
|
-
by keyword but not yet by meaning.</p>`}catch(
|
|
100
|
+
by keyword but not yet by meaning.</p>`}catch(m){r.textContent=`Index state unavailable: ${m.message}`}})()}else if(n==="algorithm")setTimeout(()=>{let u=t.find(".sim-list")[0];try{let y=yt(e.text||""),d=$t(),f=Object.entries(d).sort((o,p)=>p[1]-o[1]).slice(0,8);r.style.display="none",u.innerHTML=`<h3>This Search Algorithm</h3>
|
|
100
101
|
<table>
|
|
101
|
-
${Object.entries(
|
|
102
|
-
<tr><th>always</th><td>${
|
|
103
|
-
<tr><th>never</th><td>${
|
|
104
|
-
<tr><th>batch</th><td>${
|
|
102
|
+
${Object.entries(y.weights).map(([o,p])=>`<tr><th>weight ${o}</th><td>${p}</td></tr>`).join("")}
|
|
103
|
+
<tr><th>always</th><td>${y.always.length?y.always.join(", "):"\u2014"}</td></tr>
|
|
104
|
+
<tr><th>never</th><td>${y.never.length?y.never.join(", "):"\u2014"}</td></tr>
|
|
105
|
+
<tr><th>batch</th><td>${y.batch||50}</td></tr>
|
|
105
106
|
</table>
|
|
106
|
-
<p class="sim-count">Learned in this browser: ${f.length?f.map(([
|
|
107
|
+
<p class="sim-count">Learned in this browser: ${f.length?f.map(([o,p])=>`${o} (${p.toFixed(2)})`).join(", "):"nothing yet \u2014 sites you are near, open and click will appear here"}</p>
|
|
107
108
|
<p class="sim-count"><button class="sim-remember">Remember</button> <button class="sim-forget">Forget</button>
|
|
108
|
-
<span class="sim-remember-note"></span></p>`;let u
|
|
109
|
+
<span class="sim-remember-note"></span></p>`;let m=u.querySelector(".sim-remember-note");u.querySelector(".sim-remember").addEventListener("click",async()=>{try{let o=await _t(t.parents(".page"));m.textContent=`${o} sites written into "Sites I visit" \u2014 read the roster above, delete any line.`}catch(o){m.textContent=`Could not remember here: ${o.message}`}}),u.querySelector(".sim-forget").addEventListener("click",()=>{Nt(),m.textContent="Forgotten \u2014 the browser starts learning again from here."})}catch(y){r.textContent=`Algorithm unreadable: ${y.message}`}},0);else if(n==="list"){let u=t.find(".sim-list")[0],y=(d,f)=>{let m=d.reduce((o,p)=>o+(p.page_count||0),0);r.style.display="none",u.innerHTML=`<h3>Indexed Farm Domains</h3>
|
|
109
110
|
<table>
|
|
110
111
|
<tr><th>Domain</th><th>Pages</th></tr>
|
|
111
|
-
${
|
|
112
|
+
${d.map(({domain:o,page_count:p})=>`
|
|
112
113
|
<tr>
|
|
113
|
-
<td><img class="sim-flag remote" src="${window.wiki.site(
|
|
114
|
-
title="${
|
|
115
|
-
<td>${
|
|
114
|
+
<td><img class="sim-flag remote" src="${window.wiki.site(o).flag()}"
|
|
115
|
+
title="${o}" data-site="${o}"> ${o}</td>
|
|
116
|
+
<td>${p!=null?p.toLocaleString():"\u2014"}</td>
|
|
116
117
|
</tr>`).join("")}
|
|
117
118
|
</table>
|
|
118
|
-
<p class="sim-count">${
|
|
119
|
-
`);d=await Q(v||f.title,k)}let y=tt(d,u,{threshold:h,limit:g,excludeSlug:f.slug,excludeDomain:f.site});p(y,null),m&&(o.textContent=G(f),o.style.display=""),y.length&&!m&&W(e,{scored:y})}catch(c){o.textContent=`Error: ${c.message}`}})()}else if(n==="ghost"){let a=t.find(".sim-btn")[0],p=async()=>{if(!D){o.textContent="No URL \u2014 GHOST needs a page-json URL";return}a.disabled=!0,o.textContent="Fetching\u2026";try{let c=await fetch(D);if(!c.ok)throw new Error(`fetch failed: ${c.status}`);let f=await c.json();window.wiki.showResult(window.wiki.newPage(f),{$page:t.parents(".page")}),o.textContent=""}catch(c){o.textContent=`Error: ${c.message}`}finally{a.disabled=!1}};a.addEventListener("click",p)}else if(n==="build"){let a=t.find(".sim-btn")[0],p=async()=>{a.disabled=!0,o.textContent="Building index\u2026 (may take a while for large scopes)";try{let c=encodeURIComponent((s.length?s:["*"]).join(",")),f=await fetch(`${k}/system/build-index.json?domains=${c}&force=${P?1:0}`);if(f.status===501){let i=await f.json().catch(()=>({}));throw new Error(i.hint||"indexing runs on the farm indexer, not this server")}if(!f.ok)throw new Error(`build-index failed: ${f.status}`);let u=await f.json();window.wiki.showResult(window.wiki.newPage(u),{$page:t.parents(".page")}),o.textContent=""}catch(c){o.textContent=`Error: ${c.message}`}finally{a.disabled=!1}};a.addEventListener("click",p)}else if(n==="report"){let a=t.find(".sim-input")[0],p=t.find(".sim-btn")[0],c=`Domains: ${s.length?s.join(", "):"*"}`;m&&!a.value&&(a.value=m.title),(async()=>{try{let b=await E,C=await K(b.length?b:["*"],k),U=C.reduce((A,S)=>A+(S.page_count||0),0);if(c=V("Report ready",U,C.length),M!=null?M>0:b.some(A=>A.toUpperCase()==="GALAXY"))try{let S=(await fetch(`${k}/system/similarity-health.json`).then(O=>O.json())).siteIndex||{},q=b.filter(O=>/\./.test(O)&&!/[*?]/.test(O)&&!["PUBLIC","LOCAL","PRIVATE","GALAXY"].includes(O.toUpperCase())),B=[`Ready \u2014 ${S.count?`ranks ${S.count.toLocaleString()} sites from the Site Index${S.via==="peer"?" (a peer's copy)":""}`:"no Site Index on this farm \u2014 batches ask its peers"}`];q.length&&B.push(`${q.length} named site${q.length>1?"s":""} searched wherever held`),R&&B.push(`threshold ${h}`),B.push(`limit ${g}`),c=B.join(" \xB7 ")}catch{}l.length&&(c+=` \xB7 +${l.length} peer farm${l.length>1?"s":""}`),m&&(c=`${G(m)} \xB7 ${c}`),o.textContent=c}catch(b){o.textContent=`Domain listing unavailable: ${b.message}`}})();let f=new Set([502,503]),u=async(b,C)=>{let U=encodeURIComponent((C.length?C:["*"]).join(",")),H=l.length?`&farms=${encodeURIComponent(l.join(","))}`:"",A=await fetch(`${k}/system/farm-search.json?q=${encodeURIComponent(b)}&pattern=${U}&limit=${g}${H}`);if(!A.ok)throw new Error(`keyword search also failed: ${A.status}`);let S=await A.json();window.wiki.showResult(window.wiki.newPage(S),{$page:t.parents(".page")})},i=b=>M!=null?M>0:b.some(C=>C.toUpperCase()==="GALAXY"),d=t.find(".sim-results")[0],y=async(b,C,U)=>{let H=await Ot(j,k),A=C.filter(O=>!Z(O)&&O!=="*"&&!X(O)),S=null,q=O=>{let I=ye(b,O);S&&S.remove(),window.wiki.showResult(window.wiki.newPage(I),{$page:t.parents(".page")}),S=t.parents(".page").next(".page")},_=(O,I)=>{let Yt=O.slice(0,g),Jt=I.running?`Searched ${I.searched.toLocaleString()} of ${I.total.toLocaleString()} sites \u2014 ${O.length} results \u2014 searching\u2026`:I.converged?`Searched ${I.searched.toLocaleString()} of ${I.total.toLocaleString()} sites \u2014 ${O.length} results \u2014 the top ${g} stopped changing`:`Searched ${I.searched.toLocaleString()} of ${I.total.toLocaleString()} sites \u2014 ${O.length} results`,wt=I.total-I.searched,$t=Object.entries(I.answered||{}).filter(([,T])=>T>0).map(([T,F])=>`${T==="local"?"this farm":T} (${F.toLocaleString()} site${F===1?"":"s"})`),Kt=I.siteIndex?.builtAt?` \xB7 site index built ${Vt(I.siteIndex.builtAt)}`:"",Wt=`<small class="sim-tier">semantic \xB7 nearest sites first${$t.length?` \xB7 answered by ${$t.join(", ")}`:""}${Kt}</small>`,Xt=T=>{let F=I.siteInfo?.get(T.site),z=[];return T.via&&z.push(`via ${T.via}`),F?.indexedAt&&z.push(`indexed ${Vt(F.indexedAt*1e3)}${F.placedBy?` by ${F.placedBy}`:""}`),z.length?` <small class="sim-src">${z.join(" \xB7 ")}</small>`:""};d.innerHTML=`<h3>${Jt}</h3>${Wt}<ul>${Yt.map(T=>`<li>${rt(T.site,T.slug,T.title,T.semantic)}`+Xt(T)+(T.siblings?.length?` <small>+${T.siblings.length}</small>`:"")+(T.movedFrom?` <small>moved here from ${T.movedFrom}</small>`:"")+(T.gone?` <small>site ${T.gone}${T.movedTo?`, probably now ${T.movedTo}`:""}</small>`:"")+"</li>").join("")}</ul><p class="sim-count sim-batch-controls">`+(I.running?'<button class="sim-stop">Stop</button>':"")+(!I.running&&wt>0?`<button class="sim-more">Search the remaining ${wt.toLocaleString()} sites</button>`:"")+(!I.running&&O.length?'<button class="sim-open">Open as page</button>':"")+(I.unindexed?.length&&!I.running?`<br><small>Sites that look relevant but carry no page vectors yet: ${I.unindexed.slice(0,8).join(", ")}</small>`:"")+"</p>",d.querySelector(".sim-stop")?.addEventListener("click",()=>I.stop()),d.querySelector(".sim-more")?.addEventListener("click",()=>I.continueAll()),d.querySelector(".sim-open")?.addEventListener("click",()=>q(O)),d.querySelectorAll(".sim-link").forEach(T=>T.addEventListener("click",F=>{F.preventDefault();let{site:z,slug:Se,title:Zt}=T.dataset;it(z,"clicked"),window.wiki.pageHandler.context={site:window.location.hostname,slug:"search-tool"},window.wiki.doInternalLink(Zt,t.parents(".page"),z)}))};o.textContent="";let B=await At({origin:k,query:b,seeded:U,specs:C.length?C:["GALAXY"],limit:g,threshold:h,thresholdSet:R,batch:M||50,roster:A,algorithm:H,render:_,status:O=>{o.textContent=O}});return o.textContent=c,B},v=async()=>{let b=a.value.trim();if(b){p.disabled=!0,o.textContent=l.length?"Generating report (asking peer farms)\u2026":"Generating report\u2026";try{let C=await E;if(i(C)){let _=m&&b===m.title?J():{};await y(b,C,_);return}let U=m&&b===m.title?J():{},H={query:b,domains:C.length?C:["*"],limit:g,live:L,...U};l.length&&(H.farms=l),R&&(H.threshold=h);let A=await fetch(`${k}/system/search-report.json`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(H)});if(f.has(A.status)){let B=((await A.json().catch(()=>({}))).error||`status ${A.status}`).replace(/^embedding unavailable: /,"");o.textContent="Semantic search unavailable \u2014 searching by keyword instead\u2026",await u(b,C),o.textContent=`Semantic search unavailable (${B}) \u2014 these are keyword matches, ranked by word not meaning.`;return}if(!A.ok)throw new Error(`search-report failed: ${A.status}`);let S=await A.json(),q=window.wiki.newPage(S);window.wiki.showResult(q,{$page:t.parents(".page")}),o.textContent=c}catch(C){o.textContent=`Error: ${C.message}`}finally{p.disabled=!1}}};if(p.addEventListener("click",v),a.addEventListener("keydown",b=>{b.key==="Enter"&&v()}),n==="report"&&s.join(",")==="*"){let b=zt();b&&(a.value=b,v())}}else if(n==="sites"){let a=t.find(".sim-input")[0],p=t.find(".sim-btn")[0],c=`Domains: ${s.length?s.join(", "):"*"}`;m&&!a.value&&(a.value=m.title),(async()=>{try{let u=await E,i=await K(u.length?u:["*"],k),d=i.reduce((y,v)=>y+(v.page_count||0),0);c=V("Site report ready",d,i.length),m&&(c=`${G(m)} \xB7 ${c}`),o.textContent=c}catch(u){o.textContent=`Domain listing unavailable: ${u.message}`}})();let f=async()=>{let u=a.value.trim();if(u){p.disabled=!0,o.textContent="Ranking sites\u2026";try{let i=await E,d=m&&u===m.title?J():{},y=await fetch(`${k}/system/site-report.json`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({query:u,domains:i.length?i:["*"],limit:g,...d})});if(!y.ok)throw new Error(`site-report failed: ${y.status}`);let v=await y.json();window.wiki.showResult(window.wiki.newPage(v),{$page:t.parents(".page")}),o.textContent=c}catch(i){o.textContent=`Error: ${i.message}`}finally{p.disabled=!1}}};p.addEventListener("click",f),a.addEventListener("keydown",u=>{u.key==="Enter"&&f()})}else if(n==="keyword"){let a=t.find(".sim-input")[0],p=t.find(".sim-btn")[0];m&&!a.value&&(a.value=m.title),o.textContent=(m?`${G(m)} \xB7 `:"")+`Keyword search ready \u2014 domains: ${s.length?s.join(", "):"*"} \xB7 limit ${g}`;let c=async()=>{let f=a.value.trim();if(f){p.disabled=!0,o.textContent="Searching live site indexes\u2026";try{let u=await E,i=encodeURIComponent((u.length?u:["*"]).join(",")),d=l.length?`&farms=${encodeURIComponent(l.join(","))}`:"",y=await fetch(`${k}/system/farm-search.json?q=${encodeURIComponent(f)}&pattern=${i}&limit=${g}${d}`);if(!y.ok)throw new Error(`farm-search failed: ${y.status}`);let v=await y.json();window.wiki.showResult(window.wiki.newPage(v),{$page:t.parents(".page")}),o.textContent=`Keyword search ready \u2014 domains: ${s.length?s.join(", "):"*"} \xB7 limit ${g}`}catch(u){o.textContent=`Error: ${u.message}`}finally{p.disabled=!1}}};p.addEventListener("click",c),a.addEventListener("keydown",f=>{f.key==="Enter"&&c()})}else if(n==="author"){let a=t.find(".sim-input")[0],p=t.find(".sim-btn")[0],c=t.find(".sim-results")[0],f=null;m&&!a.value&&(a.value=m.title),(async()=>{try{w||(o.textContent="Resolving domains\u2026"),f=await et(await E,k);let i=f.reduce((d,y)=>d+y.pages.length,0);o.textContent=(m?`${G(m)} \xB7 `:"")+V("Author ready",i,f.length)}catch(i){o.textContent=`Load error: ${i.message}`}})();let u=async()=>{let i=a.value.trim();if(!(!i||!f)){p.disabled=!0,o.textContent="Embedding query\u2026",c.innerHTML="";try{let d=await Q(i,k),y=tt(d,f,{threshold:h,limit:g,excludeSlug:null,excludeDomain:null}),v=new Set,b=[];for(let{title:S}of y)v.has(S)||(v.add(S),b.push(S));let C=()=>Math.floor(Math.random()*18446744073709552e3).toString(16).padStart(16,"0"),U=b.map(S=>`- [[${S}]]`).join(`
|
|
120
|
-
`),
|
|
119
|
+
<p class="sim-count">${d.length} domains \u2014 ${m.toLocaleString()} pages${A(f)}</p>`};x?.domains?y(x.domains,x.ts):(async()=>{try{let d=await C,f=d.length?d.join(","):"*",m=`${E}/system/indexed-domains.json?pattern=${encodeURIComponent(f)}&limit=${w}`,o=await fetch(m);if(!o.ok)throw new Error(`indexed-domains failed: ${o.status}`);let p=await o.json();if(!p.length){r.textContent="No indexed domains found";return}y(p,null),Z(e,{domains:p})}catch(d){r.textContent=`Error: ${d.message}`}})()}else if(n==="similar"){let u=t.find(".sim-results")[0],y=(d,f)=>{if(!d.length){r.textContent=`No similar pages found above threshold ${c}`;return}u.innerHTML=`<h3>Similar Pages</h3><ul>${d.map(({domain:m,slug:o,title:p,score:S})=>`<li>${lt(m,o,p,S)}</li>`).join("")}</ul><p class="sim-count">${d.length} found ${tt}${A(f)}</p>`,r.style.display="none"};x?.scored?y(x.scored,x.ts):(async()=>{try{let d=t.parents(".page"),f=h||(()=>{let b=d.find(".title").text().trim()||document.title;return{slug:Et(b),site:window.location.hostname,title:b,text:null,isSelf:!0}})(),m=await C;r.textContent=(h?`${V(f)} \xB7 `:"")+"Asking for pages like this one\u2026";let o=f.text||d.find(".item").map((b,j)=>$(j).text().trim()).get().filter(Boolean).join(`
|
|
120
|
+
`).slice(0,2e3),p={query:f.title,domains:m.length?m:["*"],limit:w,flat:!0,seed:{site:f.site,slug:f.slug},text:o||f.title,excludePage:{site:f.site,slug:f.slug}};D&&(p.threshold=c);let S=await fetch(`${E}/system/search-report.json`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(p)});if(!S.ok)throw new Error(`search-report failed: ${S.status}`);let M=((await S.json()).results||[]).map(b=>({domain:b.site,slug:b.slug,title:b.title,score:b.semantic??b.score}));y(M,null),h&&(r.textContent=V(f),r.style.display=""),M.length&&!h&&Z(e,{scored:M})}catch(d){r.textContent=`Error: ${d.message}`}})()}else if(n==="ghost"){let u=t.find(".sim-btn")[0],y=async()=>{if(!O){r.textContent="No URL \u2014 GHOST needs a page-json URL";return}u.disabled=!0,r.textContent="Fetching\u2026";try{let d=await fetch(O);if(!d.ok)throw new Error(`fetch failed: ${d.status}`);let f=await d.json();window.wiki.showResult(window.wiki.newPage(f),{$page:t.parents(".page")}),r.textContent=""}catch(d){r.textContent=`Error: ${d.message}`}finally{u.disabled=!1}};u.addEventListener("click",y)}else if(n==="build"){let u=t.find(".sim-btn")[0],y=async()=>{u.disabled=!0,r.textContent="Building index\u2026 (may take a while for large scopes)";try{let d=encodeURIComponent((s.length?s:["*"]).join(",")),f=await fetch(`${E}/system/build-index.json?domains=${d}&force=${T?1:0}`);if(f.status===501){let o=await f.json().catch(()=>({}));throw new Error(o.hint||"indexing runs on the farm indexer, not this server")}if(!f.ok)throw new Error(`build-index failed: ${f.status}`);let m=await f.json();window.wiki.showResult(window.wiki.newPage(m),{$page:t.parents(".page")}),r.textContent=""}catch(d){r.textContent=`Error: ${d.message}`}finally{u.disabled=!1}};u.addEventListener("click",y)}else if(n==="report"){let u=t.find(".sim-input")[0],y=t.find(".sim-btn")[0],d=`Domains: ${s.length?s.join(", "):"*"}`;h&&!u.value&&(u.value=h.title),(async()=>{try{let b=await C,j=await Q(b.length?b:["*"],E),F=j.reduce((v,H)=>v+(H.page_count||0),0);if(d=Y("Report ready",F,j.length),P!=null?P>0:b.some(v=>v.toUpperCase()==="GALAXY"))try{let H=(await fetch(`${E}/system/similarity-health.json`).then(W=>W.json())).siteIndex||{},B=b.filter(W=>/\./.test(W)&&!/[*?]/.test(W)&&!["PUBLIC","LOCAL","PRIVATE","GALAXY"].includes(W.toUpperCase())),G=[`Ready \u2014 ${H.count?`ranks ${H.count.toLocaleString()} sites from the Site Index${H.via==="peer"?" (a peer's copy)":""}`:"no Site Index on this farm \u2014 batches ask its peers"}`];B.length&&G.push(`${B.length} named site${B.length>1?"s":""} searched wherever held`),D&&G.push(`threshold ${c}`),G.push(`limit ${w}`),d=G.join(" \xB7 ")}catch{}l.length&&(d+=` \xB7 +${l.length} peer farm${l.length>1?"s":""}`),h&&(d=`${V(h)} \xB7 ${d}`),r.textContent=d}catch(b){r.textContent=`Domain listing unavailable: ${b.message}`}})();let f=new Set([502,503]),m=async(b,j)=>{let F=encodeURIComponent((j.length?j:["*"]).join(",")),N=l.length?`&farms=${encodeURIComponent(l.join(","))}`:"",v=await fetch(`${E}/system/farm-search.json?q=${encodeURIComponent(b)}&pattern=${F}&limit=${w}${N}`);if(!v.ok)throw new Error(`keyword search also failed: ${v.status}`);let H=await v.json();window.wiki.showResult(window.wiki.newPage(H),{$page:t.parents(".page")})},o=b=>P!=null?P>0:b.some(j=>j.toUpperCase()==="GALAXY"),p=t.find(".sim-results")[0],S=async(b,j,F,N=null)=>{let v=await Mt(a,E),H=j.filter(U=>!nt(U)&&U!=="*"&&!et(U)),B=null,J=U=>{let L=Ce(b,U);B&&B.remove(),window.wiki.showResult(window.wiki.newPage(L),{$page:t.parents(".page")}),B=t.parents(".page").next(".page")},G=(U,L)=>{let Jt=L.shown&&L.shown.length?L.shown:U.slice(0,w),Wt=L.running?`Searched ${L.searched.toLocaleString()} of ${L.total.toLocaleString()} sites \u2014 ${U.length} results \u2014 searching\u2026`:L.converged?`Searched ${L.searched.toLocaleString()} of ${L.total.toLocaleString()} sites \u2014 ${U.length} results \u2014 the top ${w} stopped changing`:`Searched ${L.searched.toLocaleString()} of ${L.total.toLocaleString()} sites \u2014 ${U.length} results`,xt=L.total-L.searched,St=Object.entries(L.answered||{}).filter(([,I])=>I>0).map(([I,_])=>`${I==="local"?"this farm":I} (${_.toLocaleString()} site${_===1?"":"s"})`),Xt=L.siteIndex?.builtAt?` \xB7 site index built ${Yt(L.siteIndex.builtAt)}`:"",Zt=`<small class="sim-tier">semantic \xB7 nearest sites first${St.length?` \xB7 answered by ${St.join(", ")}`:""}${Xt}</small>`,Qt=I=>{let _=L.siteInfo?.get(I.site),K=[];return I.via&&K.push(`via ${I.via}`),_?.indexedAt&&K.push(`indexed ${Yt(_.indexedAt*1e3)}${_.placedBy?` by ${_.placedBy}`:""}`),K.length?` <small class="sim-src">${K.join(" \xB7 ")}</small>`:""},vt=L.opened||new Set,te=L.pending?`<li class="sim-pending"><small>${L.pending} new result${L.pending>1?"s":""} rank${L.pending>1?"":"s"} above \u2014 <button class="sim-fold">fold in</button></small></li>`:"",ee=L.resumed&&!L.running&&x?.batched?.ts?` \xB7 ${wt(x.batched.ts)}`:"";p.innerHTML=`<h3>${Wt}${ee}</h3>${Zt}<ul>${te}${Jt.map(I=>`<li${vt.has(q(I))?' class="sim-opened"':""}>${lt(I.site,I.slug,I.title,I.semantic)}`+Qt(I)+(vt.has(q(I))?' <small class="sim-mark">opened</small>':"")+(I.siblings?.length?` <small>+${I.siblings.length}</small>`:"")+(I.movedFrom?` <small>moved here from ${I.movedFrom}</small>`:"")+(I.gone?` <small>site ${I.gone}${I.movedTo?`, probably now ${I.movedTo}`:""}</small>`:"")+"</li>").join("")}</ul><p class="sim-count sim-batch-controls">`+(L.running?'<button class="sim-stop">Stop</button>':"")+(!L.running&&xt>0?`<button class="sim-more">Search the remaining ${xt.toLocaleString()} sites</button>`:"")+(!L.running&&U.length?'<button class="sim-open">Open as page</button>':"")+(L.unindexed?.length&&!L.running?`<br><small>Sites that look relevant but carry no page vectors yet: ${L.unindexed.slice(0,8).join(", ")}</small>`:"")+"</p>",p.querySelector(".sim-stop")?.addEventListener("click",()=>L.stop()),p.querySelector(".sim-more")?.addEventListener("click",()=>L.continueAll()),p.querySelector(".sim-open")?.addEventListener("click",()=>J(U)),p.querySelector(".sim-fold")?.addEventListener("click",()=>L.foldIn()),p.querySelectorAll(".sim-link").forEach(I=>I.addEventListener("click",_=>{_.preventDefault();let{site:K,slug:ne,title:se}=I.dataset;at(K,"clicked"),L.markOpened(q({site:K,slug:ne})),window.wiki.pageHandler.context={site:window.location.hostname,slug:"search-tool"},window.wiki.doInternalLink(se,t.parents(".page"),K)}))};r.textContent="";let W=await Ot({origin:E,query:b,seeded:F,specs:j.length?j:["GALAXY"],limit:w,threshold:c,thresholdSet:D,batch:P||100,roster:H,algorithm:v,render:G,resume:N,status:U=>{r.textContent=U},save:U=>Z(e,{batched:{...U,ts:Date.now()}})});return r.textContent=d,W},R=async()=>{let b=u.value.trim();if(b){y.disabled=!0,r.textContent=l.length?"Generating report (asking peer farms)\u2026":"Generating report\u2026";try{let j=await C;if(o(j)){let J=h&&b===h.title?X():{};await S(b,j,J);return}let F=h&&b===h.title?X():{},N={query:b,domains:j.length?j:["*"],limit:w,live:k,...F};l.length&&(N.farms=l),D&&(N.threshold=c);let v=await fetch(`${E}/system/search-report.json`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(N)});if(f.has(v.status)){let G=((await v.json().catch(()=>({}))).error||`status ${v.status}`).replace(/^embedding unavailable: /,"");r.textContent="Semantic search unavailable \u2014 searching by keyword instead\u2026",await m(b,j),r.textContent=`Semantic search unavailable (${G}) \u2014 these are keyword matches, ranked by word not meaning.`;return}if(!v.ok)throw new Error(`search-report failed: ${v.status}`);let H=await v.json(),B=window.wiki.newPage(H);window.wiki.showResult(B,{$page:t.parents(".page")}),r.textContent=d}catch(j){r.textContent=`Error: ${j.message}`}finally{y.disabled=!1}}};y.addEventListener("click",R),u.addEventListener("keydown",b=>{b.key==="Enter"&&R()});let M=!1;if(n==="report"&&s.join(",")==="*"){let b=Vt();b&&(u.value=b,R(),M=!0)}if(!M&&n==="report"&&x?.batched?.query){let b=x.batched;u.value=b.query,(async()=>{try{let j=await C;if(!o(j))return;let F=h&&b.query===h.title?X():{};await S(b.query,j,F,b)}catch(j){r.textContent=`Remembered list unavailable: ${j.message}`}})()}}else if(n==="sites"){let u=t.find(".sim-input")[0],y=t.find(".sim-btn")[0],d=`Domains: ${s.length?s.join(", "):"*"}`;h&&!u.value&&(u.value=h.title),(async()=>{try{let m=await C,o=await Q(m.length?m:["*"],E),p=o.reduce((S,R)=>S+(R.page_count||0),0);d=Y("Site report ready",p,o.length),h&&(d=`${V(h)} \xB7 ${d}`),r.textContent=d}catch(m){r.textContent=`Domain listing unavailable: ${m.message}`}})();let f=async()=>{let m=u.value.trim();if(m){y.disabled=!0,r.textContent="Ranking sites\u2026";try{let o=await C,p=h&&m===h.title?X():{},S=await fetch(`${E}/system/site-report.json`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({query:m,domains:o.length?o:["*"],limit:w,...p})});if(!S.ok)throw new Error(`site-report failed: ${S.status}`);let R=await S.json();window.wiki.showResult(window.wiki.newPage(R),{$page:t.parents(".page")}),r.textContent=d}catch(o){r.textContent=`Error: ${o.message}`}finally{y.disabled=!1}}};y.addEventListener("click",f),u.addEventListener("keydown",m=>{m.key==="Enter"&&f()})}else if(n==="keyword"){let u=t.find(".sim-input")[0],y=t.find(".sim-btn")[0];h&&!u.value&&(u.value=h.title),r.textContent=(h?`${V(h)} \xB7 `:"")+`Keyword search ready \u2014 domains: ${s.length?s.join(", "):"*"} \xB7 limit ${w}`;let d=async()=>{let f=u.value.trim();if(f){y.disabled=!0,r.textContent="Searching live site indexes\u2026";try{let m=await C,o=encodeURIComponent((m.length?m:["*"]).join(",")),p=l.length?`&farms=${encodeURIComponent(l.join(","))}`:"",S=await fetch(`${E}/system/farm-search.json?q=${encodeURIComponent(f)}&pattern=${o}&limit=${w}${p}`);if(!S.ok)throw new Error(`farm-search failed: ${S.status}`);let R=await S.json();window.wiki.showResult(window.wiki.newPage(R),{$page:t.parents(".page")}),r.textContent=`Keyword search ready \u2014 domains: ${s.length?s.join(", "):"*"} \xB7 limit ${w}`}catch(m){r.textContent=`Error: ${m.message}`}finally{y.disabled=!1}}};y.addEventListener("click",d),u.addEventListener("keydown",f=>{f.key==="Enter"&&d()})}else if(n==="author"){let u=t.find(".sim-input")[0],y=t.find(".sim-btn")[0],d=t.find(".sim-results")[0],f=null;h&&!u.value&&(u.value=h.title),(async()=>{try{x||(r.textContent="Resolving domains\u2026"),f=await gt(await C,E);let o=f.reduce((p,S)=>p+S.pages.length,0);r.textContent=(h?`${V(h)} \xB7 `:"")+Y("Author ready",o,f.length)}catch(o){r.textContent=`Load error: ${o.message}`}})();let m=async()=>{let o=u.value.trim();if(!(!o||!f)){y.disabled=!0,r.textContent="Embedding query\u2026",d.innerHTML="";try{let p=await ft(o,E),S=pt(p,f,{threshold:c,limit:w,excludeSlug:null,excludeDomain:null}),R=new Set,M=[];for(let{title:v}of S)R.has(v)||(R.add(v),M.push(v));let b=()=>Math.floor(Math.random()*18446744073709552e3).toString(16).padStart(16,"0"),j=M.map(v=>`- [[${v}]]`).join(`
|
|
121
|
+
`),F=[{type:"markdown",id:b(),text:`# Similar Pages
|
|
121
122
|
|
|
122
|
-
${
|
|
123
|
+
${j}`},{type:"markdown",id:b(),text:"# Reference Links"},...S.map(({domain:v,slug:H,title:B,score:J})=>({type:"reference",id:b(),site:v,slug:H,title:B,text:`score ${J.toFixed(3)}`}))],N=window.wiki.newPage({title:`${o} Results`,story:F,journal:[]});window.wiki.showResult(N,{$page:t.parents(".page")}),r.textContent=`${S.length} pages found`,Z(e,{scored:S,query:o})}catch(p){r.textContent=`Error: ${p.message}`}finally{y.disabled=!1}}};y.addEventListener("click",m),u.addEventListener("keydown",o=>{o.key==="Enter"&&m()})}else{let u=t.find(".sim-input")[0],y=t.find(".sim-btn")[0],d=t.find(".sim-results")[0],f=null;x?.scored&&(u.value=x.query||"",d.innerHTML=x.scored.map(({domain:o,slug:p,title:S,score:R})=>`<div class="sim-result">${lt(o,p,S,R)}</div>`).join("")+`<p class="sim-count">Top ${x.scored.length} for "${x.query||""}"${A(x.ts)}</p>`,r.textContent=""),h&&!u.value&&(u.value=h.title),(async()=>{try{x||(r.textContent="Resolving domains\u2026"),f=await gt(await C,E);let o=f.reduce((p,S)=>p+S.pages.length,0);r.textContent=(h?`${V(h)} \xB7 `:"")+Y("Search ready",o,f.length)}catch(o){r.textContent=`Load error: ${o.message}`}})();let m=async()=>{let o=u.value.trim();if(!(!o||!f)){y.disabled=!0,r.textContent="Embedding query\u2026",d.innerHTML="";try{let p=await ft(o,E),S=pt(p,f,{threshold:c,limit:w,excludeSlug:null,excludeDomain:null});d.innerHTML=S.map(({domain:R,slug:M,title:b,score:j})=>`<div class="sim-result">${lt(R,M,b,j)}</div>`).join("")+`<p class="sim-count">Top ${S.length} for "${o}"</p>`,r.textContent="",Z(e,{scored:S,query:o})}catch(p){r.textContent=`Error: ${p.message}`}finally{y.disabled=!1}}};y.addEventListener("click",m),u.addEventListener("keydown",o=>{o.key==="Enter"&&m()})}};typeof window<"u"&&(window.plugins=window.plugins||{},window.plugins.similarity={emit:Ie,bind:Te,editor:(t,e)=>window.wiki.textEditor(t,e)},Kt());})();
|
|
123
124
|
//# sourceMappingURL=similarity.js.map
|