scorpiox-bot-plugin-memory
←ui.htmlhtml
384 lines · 18 KBHEADBlameDownload
1<!DOCTYPE html>
2<html lang="en"><head><meta charset="utf-8">
3<meta name="viewport" content="width=device-width,initial-scale=1">
4<title>memory - knowledge graph</title>
5<!--TOKENS-->
6<style>
7*{box-sizing:border-box}
8html,body{margin:0;height:100%}
9body{background:var(--bg);color:var(--text);font-family:var(--font);font-size:14px;
10 line-height:1.5;-webkit-font-smoothing:antialiased;overflow:hidden}
11#app{display:grid;grid-template-columns:320px minmax(0,1fr);height:100%;min-height:0}
12.pane{min-width:0;min-height:0;display:flex;flex-direction:column}
13#side{background:var(--bg-2);border-right:1px solid var(--line)}
14.hd{display:flex;align-items:center;gap:8px;padding:11px 12px 9px}
15.brand{font-size:12px;font-weight:700;letter-spacing:.09em;text-transform:uppercase;
16 color:var(--text-2);flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
17.icon{display:inline-flex;align-items:center;justify-content:center;background:var(--surface-2);
18 border:1px solid var(--line);color:var(--text);border-radius:9px;width:30px;height:30px;
19 padding:0;cursor:pointer;font-family:inherit;font-size:13px;font-weight:600;flex:none}
20.icon:hover{background:var(--surface-hover);border-color:var(--line-2)}
21.icon:active{transform:scale(.96)}
22.icon svg{width:15px;height:15px;display:block;fill:none;stroke:currentColor;stroke-width:1.8;
23 stroke-linecap:round;stroke-linejoin:round}
24.search{margin:0 12px 8px;padding:8px 11px;border-radius:9px;border:1px solid var(--line);
25 background:var(--input-bg);color:var(--text);font-family:inherit;font-size:13px;width:calc(100% - 24px)}
26.search:focus{outline:2px solid var(--blue);outline-offset:-1px}
27.chips{display:flex;flex-wrap:wrap;gap:5px;padding:0 12px 8px}
28.chip{background:var(--surface-2);border:1px solid var(--line);color:var(--text-2);
29 border-radius:980px;padding:2px 9px;font-size:11px;font-family:inherit;cursor:pointer;
30 max-width:100%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
31.chip.on{background:var(--blue);border-color:var(--blue);color:#fff}
32.list{flex:1 1 auto;overflow-y:auto;padding:0 8px 8px}
33.item{display:block;width:100%;text-align:left;background:transparent;border:0;border-radius:10px;
34 padding:8px 10px;color:var(--text);cursor:pointer;font-family:inherit;margin-bottom:2px}
35.item:hover{background:var(--surface-hover)}
36.item.sel{background:var(--surface-2);box-shadow:inset 2px 0 0 var(--blue)}
37.irow{display:flex;align-items:baseline;gap:7px}
38.iname{font-weight:600;flex:1;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
39.icount{font-size:11px;color:var(--text-2);font-variant-numeric:tabular-nums;flex:none}
40.itype{display:inline-block;margin-top:2px;font-size:10.5px;letter-spacing:.03em;text-transform:uppercase;
41 color:var(--teal);background:var(--surface-2);border-radius:5px;padding:1px 6px;max-width:100%;
42 overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
43#main{overflow-y:auto}
44.mainhd{display:flex;align-items:center;gap:8px;padding:11px 14px;border-bottom:1px solid var(--line);
45 position:sticky;top:0;background:var(--bg);z-index:2;flex-wrap:wrap}
46.title{font-size:15px;font-weight:700;flex:1;min-width:0;overflow-wrap:anywhere}
47.btn{background:var(--blue);color:#fff;border:0;border-radius:980px;padding:6px 13px;font-weight:600;
48 font-size:12.5px;cursor:pointer;font-family:inherit;white-space:nowrap}
49.btn:active{transform:scale(.98)}
50.btn.ghost{background:var(--surface-2);color:var(--text);border:1px solid var(--line)}
51.btn.danger{background:transparent;color:var(--red);border:1px solid var(--line);padding:3px 9px;
52 font-size:11.5px;font-weight:600}
53.body{padding:14px;display:flex;flex-direction:column;gap:16px}
54.card{background:var(--bg-card);border:1px solid var(--line);border-radius:14px;padding:13px 14px}
55.card h3{margin:0 0 9px;font-size:11px;font-weight:700;letter-spacing:.08em;text-transform:uppercase;
56 color:var(--text-2)}
57.obs{list-style:none;margin:0;padding:0;display:flex;flex-direction:column;gap:6px}
58.obs li{display:flex;gap:9px;align-items:flex-start;background:var(--surface-2);border-radius:9px;
59 padding:7px 10px;font-size:13px}
60.obs li span{flex:1;min-width:0;overflow-wrap:anywhere}
61.rel{display:flex;gap:9px;align-items:baseline;padding:6px 0;border-top:1px solid var(--line);font-size:13px}
62.rel:first-of-type{border-top:0;padding-top:0}
63.rel .arrow{color:var(--text-2);flex:none;font-size:12px}
64.rel b{font-weight:600;overflow-wrap:anywhere}
65.rel .rt{color:var(--orange);font-size:11.5px;font-weight:600;overflow-wrap:anywhere}
66.row{display:flex;gap:8px;flex-wrap:wrap;align-items:center}
67input,select{background:var(--input-bg);border:1px solid var(--line);color:var(--text);
68 border-radius:9px;padding:7px 10px;font-family:inherit;font-size:13px;min-width:0;flex:1 1 130px}
69input:focus,select:focus{outline:2px solid var(--blue);outline-offset:-1px}
70.hint{font-size:11.5px;color:var(--text-2);margin:0}
71.empty{text-align:center;padding:56px 22px;color:var(--text-2)}
72.empty b{display:block;color:var(--text);font-size:15px;margin-bottom:6px;font-weight:600}
73.svgwrap{margin:0 auto 12px;width:74px;height:74px;opacity:.85}
74.svgwrap svg{width:100%;height:100%;fill:none;stroke:var(--blue);stroke-width:1.6}
75.status{font-size:11.5px;color:var(--text-2);padding:0 14px 12px;min-height:16px;overflow-wrap:anywhere}
76.status.bad{color:var(--red)}
77.status.ok{color:var(--green)}
78.ft{border-top:1px solid var(--line);display:flex;align-items:center;gap:8px;padding:8px 12px;
79 font-size:11px;color:var(--text-2)}
80.ft .grow{flex:1}
81.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}
82@media (max-width:820px){
83 #app{grid-template-columns:1fr}
84 #side{border-right:0;border-bottom:1px solid var(--line);max-height:46vh}
85 body[data-view="detail"] #side{display:none}
86 body:not([data-view="detail"]) #main{display:none}
87}
88</style>
89</head><body>
90<div id="app">
91 <section class="pane" id="side">
92 <div class="hd">
93 <span class="brand">knowledge graph</span>
94 <button class="icon" id="btnTheme" title="Theme" aria-label="Toggle theme">
95 <svg viewBox="0 0 24 24"><circle cx="12" cy="12" r="4.2"/><path d="M12 3v2M12 19v2M3 12h2M19 12h2M5.6 5.6l1.4 1.4M17 17l1.4 1.4M18.4 5.6L17 7M7 17l-1.4 1.4"/></svg>
96 </button>
97 </div>
98 <input class="search" id="q" type="search" placeholder="Search names, types, observations" aria-label="Search the graph">
99 <div class="chips" id="chips"></div>
100 <div class="list" id="list"></div>
101 <div class="ft">
102 <span class="grow" id="tally">loading</span>
103 <button class="icon" id="btnNew" title="New entity" aria-label="New entity">
104 <svg viewBox="0 0 24 24"><path d="M12 5v14M5 12h14"/></svg>
105 </button>
106 </div>
107 </section>
108
109 <section class="pane" id="main">
110 <div class="mainhd">
111 <button class="icon" id="btnBack" title="Back to list" aria-label="Back to list" style="display:none">
112 <svg viewBox="0 0 24 24"><path d="M14 6l-6 6 6 6"/></svg>
113 </button>
114 <span class="title" id="title">Select an entity</span>
115 <button class="btn ghost" id="btnReload" title="Reload">Reload</button>
116 </div>
117 <div id="detail" class="body"></div>
118 <div class="status" id="status"></div>
119 </section>
120</div>
121
122<script>
123(function(){
124"use strict";
125var q=new URLSearchParams(location.search);
126var node=q.get('node')||'';
127var pwd=q.get('pwd')||(node&&localStorage.getItem('bot_pwd_'+node))||
128 localStorage.getItem('bot_active_pwd')||'';
129/* Two ways to reach us: the bot's hub proxies /plugins?plugin=plugin_memory,
130 * while a directly-opened node route is /plugin_memory. Hitting the wrong one
131 * renders the plugin-LIST JSON instead of the page, so branch on the URL
132 * rather than assuming the hub. */
133var ROUTE=q.has('plugin')?'plugins':'plugin_memory';
134
135function url(action,extra){
136 var p=[];
137 if(q.has('plugin'))p.push('plugin=plugin_memory');
138 p.push('action='+encodeURIComponent(action));
139 if(node)p.push('node='+encodeURIComponent(node));
140 if(pwd)p.push('pwd='+encodeURIComponent(pwd));
141 if(extra)for(var k in extra)p.push(encodeURIComponent(k)+'='+encodeURIComponent(extra[k]));
142 return '/'+ROUTE+'?'+p.join('&');
143}
144function hdr(x){var h={};for(var k in x)h[k]=x[k];if(pwd)h['X-Bot-Password']=pwd;return h;}
145function api(action,extra){
146 return fetch(url(action,extra),{headers:hdr({})}).then(function(r){
147 if(!r.ok)throw new Error('HTTP '+r.status);
148 return r.json();
149 });
150}
151function post(action,obj){
152 return fetch(url(action),{method:'POST',headers:hdr({'Content-Type':'application/json'}),
153 body:JSON.stringify(obj)}).then(function(r){return r.json().then(function(j){j.__s=r.status;return j;});});
154}
155
156var el=function(id){return document.getElementById(id);};
157var listEl=el('list'),titleEl=el('title'),detailEl=el('detail'),statusEl=el('status'),
158 tallyEl=el('tally'),chipsEl=el('chips'),qEl=el('q');
159var entities=[],sel=null,typeFilter='',timer=null;
160
161function say(msg,cls){statusEl.textContent=msg||'';statusEl.className='status'+(cls?' '+cls:'');}
162
163function esc(s){return String(s==null?'':s).replace(/[&<>"']/g,function(c){
164 return {'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c];});}
165
166function graphThen(fn){
167 api('graph',{limit:'2000'}).then(function(j){
168 if(j.ok===false){say(j.error,'bad');return;}
169 entities=j.entities||[];
170 fn&&fn();
171 }).catch(function(e){say('could not read the graph: '+e.message,'bad');});
172}
173
174function tally(){
175 var n=0,i;
176 for(i=0;i<entities.length;i++)n+=entities[i].observations.length;
177 tallyEl.textContent=entities.length+' entities, '+n+' observations';
178}
179
180function chips(){
181 var seen={},out=[],i;
182 for(i=0;i<entities.length;i++){var t=entities[i].entityType;if(!seen[t]){seen[t]=1;out.push(t);}}
183 out.sort(function(a,b){return a.toLowerCase()<b.toLowerCase()?-1:1;});
184 var h='<button class="chip'+(typeFilter===''?' on':'')+'" data-t="">all</button>';
185 for(i=0;i<out.length&&i<40;i++)
186 h+='<button class="chip'+(typeFilter===out[i]?' on':'')+'" data-t="'+esc(out[i])+'">'+esc(out[i])+'</button>';
187 chipsEl.innerHTML=h;
188 Array.prototype.forEach.call(chipsEl.querySelectorAll('.chip'),function(b){
189 b.onclick=function(){typeFilter=b.getAttribute('data-t')||'';renderList();};
190 });
191}
192
193function renderList(){
194 var term=(qEl.value||'').toLowerCase().trim(),rows=[],i,j,hit;
195 for(i=0;i<entities.length;i++){
196 var e=entities[i];
197 if(typeFilter&&e.entityType!==typeFilter)continue;
198 if(term){
199 hit=e.name.toLowerCase().indexOf(term)>=0||
200 e.entityType.toLowerCase().indexOf(term)>=0;
201 for(j=0;!hit&&j<e.observations.length;j++)
202 if(e.observations[j].toLowerCase().indexOf(term)>=0)hit=true;
203 if(!hit)continue;
204 }
205 rows.push(e);
206 }
207 if(!rows.length){
208 listEl.innerHTML='<div class="empty"><b>Nothing here</b>'+
209 (entities.length?'No entity matches this filter.':'Add your first entity with the plus below.')+
210 '</div>';
211 chips();tally();return;
212 }
213 listEl.innerHTML=rows.map(function(e){
214 return '<button class="item'+(sel&&sel===e.name?' sel':'')+'" data-n="'+esc(e.name)+'">'+
215 '<span class="irow"><span class="iname">'+esc(e.name)+'</span>'+
216 '<span class="icount">'+e.observations.length+'</span></span>'+
217 '<span class="itype">'+esc(e.entityType)+'</span></button>';
218 }).join('');
219 Array.prototype.forEach.call(listEl.querySelectorAll('.item'),function(b){
220 b.onclick=function(){open(b.getAttribute('data-n'));};
221 });
222 chips();tally();
223}
224
225function relationRows(rels,dir){
226 if(!rels.length)return '<p class="hint">None yet.</p>';
227 return rels.map(function(r){
228 var other=dir==='out'?r.to:r.from;
229 return '<div class="rel"><b>'+esc(dir==='out'?r.from:r.to)+'</b>'+
230 '<span class="arrow">'+(dir==='out'?'to':'from')+'</span>'+
231 '<span class="rt">'+esc(r.relationType)+'</span>'+
232 '<span style="flex:1"></span>'+
233 '<button class="btn danger" data-delrel="'+esc(r.from)+'|'+esc(r.to)+'|'+esc(r.relationType)+'">remove</button>'+
234 '</div>';
235 }).join('');
236}
237
238function open(name){
239 sel=name;
240 document.body.setAttribute('data-view','detail');
241 el('btnBack').style.display='';
242 renderList();
243 api('entity',{name:name}).then(function(j){
244 if(j.ok===false){say(j.error,'bad');return;}
245 var ents=j.entities||[],rels=j.relations||[];
246 if(!ents.length){titleEl.textContent=name;detailEl.innerHTML=
247 '<div class="empty"><b>Gone</b>That entity is no longer in the graph.</div>';return;}
248 var e=ents[0],i,out=[],inn=[];
249 for(i=0;i<rels.length;i++){
250 if(rels[i].from===e.name)out.push(rels[i]);
251 if(rels[i].to===e.name)inn.push(rels[i]);
252 }
253 titleEl.textContent=e.name;
254 detailEl.innerHTML=
255 '<div class="card"><h3>'+esc(e.entityType)+'</h3><div class="row">'+
256 '<button class="btn danger" data-dele="'+esc(e.name)+'">Delete entity</button>'+
257 '<span class="hint">'+e.observations.length+' observation(s), '+rels.length+' relation(s)</span>'+
258 '</div></div>'+
259 '<div class="card"><h3>Observations</h3><ul class="obs">'+
260 (e.observations.length?e.observations.map(function(o){
261 return '<li><span>'+esc(o)+'</span><button class="btn danger" data-delo="'+
262 esc(e.name)+'|'+esc(o)+'">remove</button></li>';}).join('')
263 :'<li><span class="hint">No observations yet.</span></li>')+
264 '</ul><div class="row" style="margin-top:10px">'+
265 '<input id="newObs" placeholder="Add an observation" aria-label="New observation">'+
266 '<button class="btn" id="addObs">Add</button></div></div>'+
267 '<div class="card"><h3>Relations out</h3>'+relationRows(out,'out')+'</div>'+
268 '<div class="card"><h3>Relations in</h3>'+relationRows(inn,'in')+'</div>'+
269 '<div class="card"><h3>New relation from '+esc(e.name)+'</h3><div class="row">'+
270 '<select id="relTo"><option value="">target entity...</option>'+
271 entities.filter(function(x){return x.name!==e.name;}).map(function(x){
272 return '<option value="'+esc(x.name)+'">'+esc(x.name)+'</option>';}).join('')+
273 '</select><input id="relType" placeholder="relation type (active voice)" style="flex:1 1 160px">'+
274 '<button class="btn" id="addRel">Link</button></div>'+
275 '<p class="hint">Both ends must already exist. The link is skipped if it is already there.</p></div>';
276
277 Array.prototype.forEach.call(detailEl.querySelectorAll('[data-delo]'),function(b){
278 b.onclick=function(){
279 var p=b.getAttribute('data-delo').split('|'),nm=p[0];
280 post('delete_obs',{name:nm,observations:[p.slice(1).join('|')]}).then(function(r){
281 if(r.ok===false){say(r.error,'bad');return;}
282 say('observation removed','ok');open(nm);});
283 };
284 });
285 Array.prototype.forEach.call(detailEl.querySelectorAll('[data-delrel]'),function(b){
286 b.onclick=function(){
287 var p=b.getAttribute('data-delrel').split('|');
288 post('delete_relation',{from:p[0],to:p[1],relationType:p.slice(2).join('|')}).then(function(r){
289 if(r.ok===false){say(r.error,'bad');return;}
290 say(r.message||'relation removed','ok');refreshAll();});
291 };
292 });
293 Array.prototype.forEach.call(detailEl.querySelectorAll('[data-dele]'),function(b){
294 b.onclick=function(){
295 var nm=b.getAttribute('data-dele');
296 if(!window.confirm('Delete '+nm+' and every relation touching it? This cannot be undone.'))return;
297 post('delete_entity',{name:nm}).then(function(r){
298 if(r.ok===false){say(r.error,'bad');return;}
299 sel=null;document.body.removeAttribute('data-view');
300 el('btnBack').style.display='none';
301 say(r.message||'deleted','ok');refreshAll();});
302 };
303 });
304 var ao=el('addObs');
305 if(ao)ao.onclick=function(){
306 var v=el('newObs').value.trim();
307 if(!v)return;
308 post('add_obs',{name:e.name,contents:[v]}).then(function(r){
309 if(r.ok===false){say(r.error,'bad');return;}
310 el('newObs').value='';say('observation added','ok');open(e.name);});
311 };
312 var nmo=el('newObs');
313 if(nmo)nmo.onkeydown=function(ev){if(ev.key==='Enter')ao.onclick();};
314 var ar=el('addRel');
315 if(ar)ar.onclick=function(){
316 var to=el('relTo').value,ty=el('relType').value.trim();
317 if(!to){say('pick a target entity','bad');return;}
318 if(!ty){say('a relation needs a type','bad');return;}
319 post('add_relation',{from:e.name,to:to,relationType:ty}).then(function(r){
320 if(r.ok===false){say(r.error,'bad');return;}
321 el('relType').value='';say('linked','ok');
322 refreshAll(function(){open(e.name);});});
323 };
324 }).catch(function(e){say('could not read the entity: '+e.message,'bad');});
325}
326
327function back(){
328 sel=null;
329 document.body.removeAttribute('data-view');
330 el('btnBack').style.display='none';
331 titleEl.textContent='Select an entity';
332 detailEl.innerHTML='<div class="empty"><div class="svgwrap"><svg viewBox="0 0 64 64">'+
333 '<circle cx="16" cy="18" r="6"/><circle cx="47" cy="14" r="5"/>'+
334 '<circle cx="34" cy="38" r="7"/><circle cx="14" cy="50" r="5"/><circle cx="51" cy="46" r="6"/>'+
335 '<path d="M21.5 21.5 28 33M42.5 17.5 39 31M28.5 43 18.5 47.5M40.5 41.5 46 44"/></svg></div>'+
336 '<b>No entity selected</b>Pick one on the left, or search to find it by keyword.</div>';
337 renderList();
338}
339
340function newEntity(){
341 var nm=window.prompt('Entity name');
342 if(!nm||!nm.trim())return;
343 var ty=window.prompt('Entity type','note');
344 if(ty===null)return;
345 post('add_entity',{name:nm.trim(),entityType:(ty||'note').trim(),observations:[]})
346 .then(function(r){
347 if(r.ok===false){say(r.error,'bad');return;}
348 say('created '+nm.trim()+'','ok');
349 refreshAll(function(){open(nm.trim());});
350 });
351}
352
353function refreshAll(fn){
354 api('stats').then(function(s){
355 if(s.ok!==false){
356 tallyEl.textContent=s.entities+' entities, '+s.observations+' observations, '+
357 s.relations+' relations';
358 }
359 });
360 graphThen(function(){renderList();if(fn)fn();});
361}
362
363el('btnReload').onclick=function(){say('');refreshAll();};
364el('btnBack').onclick=back;
365el('btnNew').onclick=newEntity;
366qEl.oninput=function(){clearTimeout(timer);timer=setTimeout(renderList,140);};
367qEl.onkeydown=function(e){
368 if(e.key==='Escape'){qEl.value='';renderList();}
369 if(e.key==='Enter'){
370 for(var i=0;i<entities.length;i++)
371 if(entities[i].name.toLowerCase()===(qEl.value||'').toLowerCase().trim()){
372 open(entities[i].name);return;}
373 }
374};
375
376var tb=el('btnTheme');
377if(tb&&window.sxApplyTheme){
378 tb.onclick=function(){window.sxApplyTheme(window.sxTheme&&window.sxTheme()==='dark'?'light':'dark');};
379}
380
381refreshAll();
382})();
383</script>
384</body></html>