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