dsh-memory-engine 0.6.1 → 0.7.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +106 -14
- package/lib/client.js +981 -26
- package/lib/client.src.js +706 -0
- package/lib/config.js +35 -2
- package/lib/index.js +4 -5
- package/lib/map.js +23 -11
- package/lib/onboard.js +18 -0
- package/lib/render/canvas.js +348 -0
- package/lib/render/layout.mjs +43 -0
- package/lib/render/render.mjs +36 -175
- package/lib/routes.js +141 -0
- package/lib/store.js +47 -2
- package/package.json +2 -1
- package/scripts/build-client.mjs +22 -0
- package/scripts/lib/strip-export.mjs +7 -0
- package/lib/serve.js +0 -150
package/lib/client.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// 由 scripts/build-client.mjs 生成,别直接改这个文件
|
|
1
2
|
window.__ModuleLoader__.load({
|
|
2
3
|
id: "dsh-memory-engine",
|
|
3
4
|
factory: function (require) {
|
|
@@ -12,26 +13,955 @@ window.__ModuleLoader__.load({
|
|
|
12
13
|
// 不同 DSH 版本暴露的插槽名不一样:新版本用 header.actions,
|
|
13
14
|
// 老版本没有这个槽时退回 views.actions(会出现在「更多视图」菜单里,虽然不显眼但能用)。
|
|
14
15
|
var SLOT_FALLBACKS = ["conversation.session.header.actions", "conversation.session.header.views.actions"];
|
|
15
|
-
var VIEW_URL = "http://127.0.0.1:18886/index.html";
|
|
16
16
|
|
|
17
|
-
function
|
|
17
|
+
function createRenderer(canvas, data, options = {}) {
|
|
18
|
+
const document = canvas.ownerDocument;
|
|
19
|
+
const window = document.defaultView;
|
|
20
|
+
const performance = window.performance;
|
|
21
|
+
const ctx = canvas.getContext('2d', { alpha: false });
|
|
22
|
+
if (!ctx) throw new Error('Canvas 2D 不可用');
|
|
23
|
+
const colors = { '人格':[255,215,106], '印象':[143,216,255], '知识':[126,231,178], '日记':[190,160,255], '对话':[255,160,140], '情绪':[255,90,90] };
|
|
24
|
+
const fallback = [145,205,255];
|
|
25
|
+
let nodes = [], links = [], hubs = [], linkTwinkle = [];
|
|
26
|
+
const byId = new Map(), adjacency = new Map(), sprites = new Map();
|
|
27
|
+
const pulses = [], firing = new Map(), selections = new Set();
|
|
28
|
+
if (typeof options.onSelect === 'function') selections.add(options.onSelect);
|
|
29
|
+
let seed = 987654321;
|
|
30
|
+
const random = () => ((seed = seed * 16807 % 2147483647) - 1) / 2147483646;
|
|
31
|
+
let width=0,height=0,dpr=1,rotX=0.3,rotY=0.15,zoom=data.nodes.length<50?1.6:1,autoSpin=true;
|
|
32
|
+
let spinSpeed=data.spinSpeed??0.35,resumeAt=0;
|
|
33
|
+
let drag=null,nextFire=0,lastTime=0,rafId=null,running=false,destroyed=false,generation=0,observer=null;
|
|
34
|
+
|
|
35
|
+
function glowSprite(rgb) {
|
|
36
|
+
const off = document.createElement('canvas');
|
|
37
|
+
off.width = off.height = 96;
|
|
38
|
+
const brush = off.getContext('2d');
|
|
39
|
+
const gradient = brush.createRadialGradient(48,48,1,48,48,48);
|
|
40
|
+
gradient.addColorStop(0,'rgba('+rgb.join(',')+',0.7)');
|
|
41
|
+
gradient.addColorStop(0.4,'rgba('+rgb.join(',')+',0.19)');
|
|
42
|
+
gradient.addColorStop(1,'rgba('+rgb.join(',')+',0)');
|
|
43
|
+
brush.fillStyle = gradient; brush.fillRect(0,0,96,96);
|
|
44
|
+
return off;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 字符串 → 32 位无符号哈希。给连线的闪烁参数做种子:
|
|
48
|
+
// 用 id 而不是 Math.random,保证同一条线在每次打开时节奏一致(不会每次刷新都跳),
|
|
49
|
+
// 但不同连线之间互不同步。
|
|
50
|
+
function hashString(text) {
|
|
51
|
+
let hash = 2166136261;
|
|
52
|
+
for (let i = 0; i < text.length; i++) {
|
|
53
|
+
hash ^= text.charCodeAt(i);
|
|
54
|
+
hash = Math.imul(hash, 16777619);
|
|
55
|
+
}
|
|
56
|
+
return hash >>> 0;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function setData(nextData) {
|
|
60
|
+
if (destroyed) return;
|
|
61
|
+
spinSpeed=nextData.spinSpeed??0.35;
|
|
62
|
+
nodes = nextData.nodes.map(node => ({ ...node, pos: { ...node.pos } }));
|
|
63
|
+
links = nextData.links.map(link => [...link]);
|
|
64
|
+
byId.clear(); adjacency.clear(); sprites.clear(); pulses.length = 0; firing.clear();
|
|
65
|
+
seed = 987654321; nextFire = 0;
|
|
66
|
+
for (const node of nextData.nodes) byId.set(node.id, node);
|
|
67
|
+
for (const node of nodes) adjacency.set(node.id, []);
|
|
68
|
+
for (const [left,right] of links) {
|
|
69
|
+
if (adjacency.has(left) && adjacency.has(right)) { adjacency.get(left).push(right); adjacency.get(right).push(left); }
|
|
70
|
+
}
|
|
71
|
+
hubs = nodes.filter(star => star.degree > 0).sort((left,right) => right.degree-left.degree).slice(0,6);
|
|
72
|
+
// 每条连线一份自己的闪烁参数:相位和频率都不一样,整体才不会"齐刷刷"地一起亮。
|
|
73
|
+
// 用连线两端 id 的哈希做种子,保证同一条线每次打开图的节奏一致(不会跳变),
|
|
74
|
+
// 但线与线之间互不同步。
|
|
75
|
+
linkTwinkle = links.map(([left, right]) => {
|
|
76
|
+
const h = hashString(left + '|' + right);
|
|
77
|
+
return {
|
|
78
|
+
// 频率调低:周期从 4~18 秒放慢到 18~55 秒,变成"慢慢呼吸"而不是"闪"
|
|
79
|
+
freq: 0.000114 + (h % 1000) / 1000 * 0.000235,
|
|
80
|
+
phase: (Math.floor(h / 1000) % 6283) / 1000, // 0 ~ 6.28,彼此错开
|
|
81
|
+
amp: 0.05 + (Math.floor(h / 7e6) % 100) / 100 * 0.12,
|
|
82
|
+
// 弯曲:一半往左弯一半往右弯,弯度 8%~24%。
|
|
83
|
+
// 之前所有线都朝同一侧弯,视觉上像漩涡,边界区域就显空。
|
|
84
|
+
bow: (h % 2 === 0 ? 1 : -1) * (0.08 + (Math.floor(h / 13) % 100) / 100 * 0.16),
|
|
85
|
+
// 端点偏移:同一个节点被很多线连时,让它们从不同角度进来,散成扇面而不是叠成一条
|
|
86
|
+
jitterStart: ((Math.floor(h / 31) % 100) / 100 - 0.5) * 1.6,
|
|
87
|
+
jitterEnd: ((Math.floor(h / 977) % 100) / 100 - 0.5) * 1.6,
|
|
88
|
+
};
|
|
89
|
+
});
|
|
90
|
+
for (const star of nodes) {
|
|
91
|
+
star._rgb = colors[star.kind] || fallback;
|
|
92
|
+
const count = Math.min(9, 4 + Math.round(Math.sqrt(star.degree) * 1.2));
|
|
93
|
+
star._dendrites = Array.from({length:count},(_,index) => ({
|
|
94
|
+
angle:index/count*Math.PI*2+random()*0.7,
|
|
95
|
+
length:0.9+random()*1.5, bend:(random()-0.5)*0.9, width:0.5+random()*0.7,
|
|
96
|
+
fork:random()<0.55 ? (random()-0.5)*1.5 : null
|
|
97
|
+
}));
|
|
98
|
+
}
|
|
99
|
+
for (const star of nodes) {
|
|
100
|
+
const key = star._rgb.join(',');
|
|
101
|
+
if (!sprites.has(key)) sprites.set(key,glowSprite(star._rgb));
|
|
102
|
+
star._glow = sprites.get(key);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function resize() {
|
|
108
|
+
if (destroyed) return;
|
|
109
|
+
const bounds = canvas.getBoundingClientRect();
|
|
110
|
+
width=bounds.width; height=bounds.height; dpr=Math.min(1.5,window.devicePixelRatio||1);
|
|
111
|
+
canvas.width=Math.round(width*dpr); canvas.height=Math.round(height*dpr);
|
|
112
|
+
ctx.setTransform(dpr,0,0,dpr,0,0);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function project(pos) {
|
|
116
|
+
const cy=Math.cos(rotY),sy=Math.sin(rotY),cx=Math.cos(rotX),sx=Math.sin(rotX);
|
|
117
|
+
const x=pos.x*cy-pos.z*sy,z=pos.x*sy+pos.z*cy;
|
|
118
|
+
const y=pos.y*cx-z*sx,depth=pos.y*sx+z*cx+250;
|
|
119
|
+
if (depth<=12) return null;
|
|
120
|
+
const scale=760/depth*zoom;
|
|
121
|
+
return {x:width/2+x*scale,y:height/2+y*scale,scale,depth};
|
|
122
|
+
}
|
|
123
|
+
function fire(id,now) {
|
|
124
|
+
firing.set(id,1);
|
|
125
|
+
const neighbours=adjacency.get(id)||[];
|
|
126
|
+
// 不再"点亮整个扇面":只随机挑 2~4 条邻居。
|
|
127
|
+
// 一次把几十条边同时点亮,看上去就是全图一起闪。
|
|
128
|
+
const count=Math.min(neighbours.length,2+Math.floor(Math.random()*3));
|
|
129
|
+
const picked=new Set();
|
|
130
|
+
let guard=0;
|
|
131
|
+
while (picked.size<count && neighbours.length && guard++<200) {
|
|
132
|
+
picked.add(neighbours[Math.floor(Math.random()*neighbours.length)]);
|
|
133
|
+
}
|
|
134
|
+
for (const other of picked) {
|
|
135
|
+
// 邻居亮度也压低:原来 0.65 会让整片区域同时亮起来
|
|
136
|
+
firing.set(other,Math.max(0.26,firing.get(other)||0));
|
|
137
|
+
pulses.push({from:id,to:other,born:now,life:700+Math.random()*2400});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function select(x,y) {
|
|
142
|
+
let best=null,bestScore=Infinity;
|
|
143
|
+
for (const star of nodes) {
|
|
144
|
+
if (!star._screen) continue;
|
|
145
|
+
const value=Math.hypot(star._screen.x-x,star._screen.y-y);
|
|
146
|
+
const tolerance=Math.max(14,star._screen.r*1.15);
|
|
147
|
+
if (value>tolerance) continue;
|
|
148
|
+
const score=value-star._screen.r*0.22;
|
|
149
|
+
if (score<bestScore) {bestScore=score;best=star;}
|
|
150
|
+
}
|
|
151
|
+
if (!best) return;
|
|
152
|
+
fire(best.id,performance.now());
|
|
153
|
+
for (const callback of selections) callback(byId.get(best.id));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function onPointerDown(event) {
|
|
157
|
+
resumeAt=Infinity;
|
|
158
|
+
autoSpin=false; drag={x:event.clientX,y:event.clientY,moved:0,pointerId:event.pointerId};
|
|
159
|
+
canvas.setPointerCapture(event.pointerId);
|
|
160
|
+
}
|
|
161
|
+
function onPointerMove(event) {
|
|
162
|
+
if (!drag) return;
|
|
163
|
+
const dx=event.clientX-drag.x,dy=event.clientY-drag.y;
|
|
164
|
+
drag.x=event.clientX; drag.y=event.clientY; drag.moved+=Math.abs(dx)+Math.abs(dy);
|
|
165
|
+
rotY+=dx*0.006; rotX=Math.max(-1.5,Math.min(1.5,rotX+dy*0.006));
|
|
166
|
+
}
|
|
167
|
+
function releaseDrag() {
|
|
168
|
+
if (drag && running) resumeAt=performance.now()+2000;
|
|
169
|
+
if (drag && canvas.hasPointerCapture(drag.pointerId)) canvas.releasePointerCapture(drag.pointerId);
|
|
170
|
+
drag=null;
|
|
171
|
+
}
|
|
172
|
+
function onPointerUp(event) {
|
|
173
|
+
const clicked = drag && drag.moved<6;
|
|
174
|
+
releaseDrag();
|
|
175
|
+
if (clicked) {
|
|
176
|
+
const bounds = canvas.getBoundingClientRect();
|
|
177
|
+
select(event.clientX-bounds.left,event.clientY-bounds.top);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function onWheel(event) {
|
|
181
|
+
event.preventDefault();
|
|
182
|
+
zoom=Math.max(0.3,Math.min(4.5,zoom*(event.deltaY>0?0.92:1.08)));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function frame(now) {
|
|
186
|
+
const delta=Math.min(50,now-lastTime||16.7); lastTime=now;
|
|
187
|
+
if (!autoSpin && !drag && now>=resumeAt) autoSpin=true;
|
|
188
|
+
if (autoSpin) rotY+=0.0011*spinSpeed*3*delta/16.7;
|
|
189
|
+
if (now>nextFire) {
|
|
190
|
+
// 心跳节奏别太机械:间隔随机,偶尔连发两个,偶尔安静久一点
|
|
191
|
+
const burst=Math.random()<0.22 ? 2 : 1;
|
|
192
|
+
for (let i=0;i<burst && hubs.length;i++) {
|
|
193
|
+
fire(hubs[Math.floor(Math.random()*hubs.length)].id,now);
|
|
194
|
+
}
|
|
195
|
+
nextFire=now+1400+Math.random()*3200;
|
|
196
|
+
}
|
|
197
|
+
ctx.globalCompositeOperation='source-over'; ctx.fillStyle='#03060c'; ctx.fillRect(0,0,width,height);
|
|
198
|
+
ctx.strokeStyle='rgba(40,90,140,0.055)';ctx.lineWidth=1;
|
|
199
|
+
for (let index=0;index<20;index++) {
|
|
200
|
+
const y=index/20*height;
|
|
201
|
+
ctx.beginPath();ctx.moveTo(0,y);ctx.bezierCurveTo(width*.3,y+Math.sin(now*.00007+index)*30,width*.7,y-Math.cos(now*.00006+index)*30,width,y);ctx.stroke();
|
|
202
|
+
}
|
|
203
|
+
const points=[];
|
|
204
|
+
for (const star of nodes) {
|
|
205
|
+
const p=project(star.pos);
|
|
206
|
+
if (!p || p.x< -100 || p.x>width+100 || p.y< -100 || p.y>height+100) {star._screen=null;continue;}
|
|
207
|
+
points.push({star,p});
|
|
208
|
+
}
|
|
209
|
+
points.sort((left,right)=>left.star.isCore?1:right.star.isCore?-1:right.p.depth-left.p.depth);
|
|
210
|
+
const screen=new Map(points.map(point=>[point.star.id,point]));
|
|
211
|
+
ctx.globalCompositeOperation='lighter';
|
|
212
|
+
for (let linkIndex=0;linkIndex<links.length;linkIndex++) {
|
|
213
|
+
const [left,right]=links[linkIndex];
|
|
214
|
+
const first=screen.get(left),second=screen.get(right);
|
|
215
|
+
if (!first||!second) continue;
|
|
216
|
+
const act=Math.max(firing.get(left)||0,firing.get(right)||0);
|
|
217
|
+
// 连线自己也会呼吸:每条线有独立相位和频率,不再"齐刷刷"地一起闪。
|
|
218
|
+
const twinkle=linkTwinkle[linkIndex];
|
|
219
|
+
const breathe=twinkle ? (0.5+0.5*Math.sin(now*twinkle.freq+twinkle.phase))*twinkle.amp : 0;
|
|
220
|
+
// 度数补偿:枢纽节点周围几十条线会糊成一块光斑,
|
|
221
|
+
// 按两端度数把这类线整体压暗,让"一条线"和"一堆线"的观感接近。
|
|
222
|
+
const degSum=(first.star.degree||0)+(second.star.degree||0);
|
|
223
|
+
const damp=1/(1+0.07*Math.max(0,degSum-6));
|
|
224
|
+
const alpha=Math.min(.85,(.1*Math.min(first.star.retention,second.star.retention)+.12*act+breathe)*damp);
|
|
225
|
+
if (alpha<.01) continue;
|
|
226
|
+
// 端点沿法线错开,多条线进入同一节点时散成扇形
|
|
227
|
+
const dx=second.p.x-first.p.x, dy=second.p.y-first.p.y;
|
|
228
|
+
const len=Math.hypot(dx,dy)||1;
|
|
229
|
+
const nx=-dy/len, ny=dx/len;
|
|
230
|
+
const r1=Math.max(2,first.star.radius*first.p.scale*.55);
|
|
231
|
+
const r2=Math.max(2,second.star.radius*second.p.scale*.55);
|
|
232
|
+
const sx=first.p.x+nx*r1*0.9*(twinkle?twinkle.jitterStart:0);
|
|
233
|
+
const sy=first.p.y+ny*r1*0.9*(twinkle?twinkle.jitterStart:0);
|
|
234
|
+
const ex=second.p.x+nx*r2*0.9*(twinkle?twinkle.jitterEnd:0);
|
|
235
|
+
const ey=second.p.y+ny*r2*0.9*(twinkle?twinkle.jitterEnd:0);
|
|
236
|
+
// 弯曲方向和弯度都按连线各自的哈希来,避免所有线朝同一侧弯成漩涡
|
|
237
|
+
const bow=twinkle?twinkle.bow:0.18;
|
|
238
|
+
const mx=(sx+ex)/2+nx*len*bow*0.5;
|
|
239
|
+
const my=(sy+ey)/2+ny*len*bow*0.5;
|
|
240
|
+
ctx.strokeStyle='rgba(110,190,255,'+alpha+')';ctx.lineWidth=.7+act*1.6+breathe*1.4;
|
|
241
|
+
ctx.beginPath();ctx.moveTo(sx,sy);ctx.quadraticCurveTo(mx,my,ex,ey);ctx.stroke();
|
|
242
|
+
}
|
|
243
|
+
for (let index=pulses.length-1;index>=0;index--) {
|
|
244
|
+
const pulse=pulses[index],fraction=(now-pulse.born)/pulse.life;
|
|
245
|
+
if (fraction>=1) {pulses.splice(index,1);continue;}
|
|
246
|
+
const first=screen.get(pulse.from),second=screen.get(pulse.to);
|
|
247
|
+
if (!first||!second) continue;
|
|
248
|
+
const mx=(first.p.x+second.p.x)/2+(first.p.y-second.p.y)*.18,my=(first.p.y+second.p.y)/2+(second.p.x-first.p.x)*.18;
|
|
249
|
+
const inverse=1-fraction;
|
|
250
|
+
const x=inverse*inverse*first.p.x+2*inverse*fraction*mx+fraction*fraction*second.p.x;
|
|
251
|
+
const y=inverse*inverse*first.p.y+2*inverse*fraction*my+fraction*fraction*second.p.y;
|
|
252
|
+
ctx.fillStyle='rgba(205,245,255,'+(.9*inverse)+')';ctx.beginPath();ctx.arc(x,y,2.5+2*inverse,0,Math.PI*2);ctx.fill();
|
|
253
|
+
}
|
|
254
|
+
for (const {star,p} of points) {
|
|
255
|
+
const act=firing.get(star.id)||0,ret=Math.max(.05,Math.min(1.2,star.retention));
|
|
256
|
+
const intensity=Math.min(1,.22+ret*.72)*(1+act*1.1);
|
|
257
|
+
// 每颗星自己的呼吸频率和相位:从 id 哈希来,避免全图同一个节拍
|
|
258
|
+
if (star._pulse===undefined) {
|
|
259
|
+
const h=hashString(star.id);
|
|
260
|
+
star._pulse={freq:0.00018+(h%1000)/1000*0.00045,phase:(Math.floor(h/1000)%6283)/1000,amp:0.035+(Math.floor(h/7e6)%100)/100*0.045};
|
|
261
|
+
}
|
|
262
|
+
const pulse=star._pulse;
|
|
263
|
+
const breathe=Math.sin(now*pulse.freq+pulse.phase)*pulse.amp;
|
|
264
|
+
const radius=Math.max(2,star.radius*p.scale*.55)*(1+breathe)*(1+act*.5);
|
|
265
|
+
const rgb=star._rgb.join(',');
|
|
266
|
+
if (star.isCore) {
|
|
267
|
+
const halo=ctx.createRadialGradient(p.x,p.y,radius*.75,p.x,p.y,radius*2.2);
|
|
268
|
+
halo.addColorStop(0,'rgba('+rgb+',0.12)');
|
|
269
|
+
halo.addColorStop(.65,'rgba('+rgb+',0.04)');
|
|
270
|
+
halo.addColorStop(1,'rgba('+rgb+',0)');
|
|
271
|
+
ctx.fillStyle=halo;ctx.beginPath();ctx.arc(p.x,p.y,radius*2.2,0,Math.PI*2);ctx.fill();
|
|
272
|
+
ctx.strokeStyle='rgba('+rgb+',0.38)';ctx.lineWidth=.7;ctx.setLineDash([3,5]);ctx.lineDashOffset=-now*.008;
|
|
273
|
+
ctx.beginPath();ctx.arc(p.x,p.y,radius*1.6,0,Math.PI*2);ctx.stroke();
|
|
274
|
+
ctx.setLineDash([]);ctx.lineDashOffset=0;
|
|
275
|
+
}
|
|
276
|
+
ctx.strokeStyle='rgba('+rgb+','+Math.min(1,.3*intensity)+')';
|
|
277
|
+
if (radius>2.5) for (const branch of star._dendrites) {
|
|
278
|
+
const length=radius*2.2*branch.length,dx=Math.cos(branch.angle),dy=Math.sin(branch.angle);
|
|
279
|
+
const ex=p.x+dx*length,ey=p.y+dy*length;
|
|
280
|
+
const cx=p.x+dx*length*.55-dy*length*branch.bend*.5;
|
|
281
|
+
const cy=p.y+dy*length*.55+dx*length*branch.bend*.5;
|
|
282
|
+
ctx.lineWidth=branch.width;ctx.beginPath();ctx.moveTo(p.x,p.y);ctx.quadraticCurveTo(cx,cy,ex,ey);ctx.stroke();
|
|
283
|
+
if (branch.fork!==null) {
|
|
284
|
+
const angle=branch.angle+branch.fork;
|
|
285
|
+
ctx.lineWidth=branch.width*.65;ctx.beginPath();ctx.moveTo(cx,cy);ctx.quadraticCurveTo(cx+Math.cos(angle)*length*.2,cy+Math.sin(angle)*length*.2,cx+Math.cos(angle)*length*.34,cy+Math.sin(angle)*length*.34);ctx.stroke();
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
const glowRadius=radius*3.6;
|
|
289
|
+
ctx.globalAlpha=Math.min(1,.4+intensity*.7);ctx.drawImage(star._glow,p.x-glowRadius,p.y-glowRadius,glowRadius*2,glowRadius*2);ctx.globalAlpha=1;
|
|
290
|
+
ctx.fillStyle='rgba('+rgb+','+Math.min(1,.42*intensity)+')';ctx.beginPath();ctx.arc(p.x,p.y,radius,0,Math.PI*2);ctx.fill();
|
|
291
|
+
ctx.strokeStyle='rgba('+rgb+','+Math.min(1,.75*intensity)+')';ctx.lineWidth=1;ctx.stroke();
|
|
292
|
+
ctx.fillStyle='rgba(255,255,255,'+Math.min(1,.6*intensity)+')';ctx.beginPath();ctx.arc(p.x,p.y,radius*.42,0,Math.PI*2);ctx.fill();
|
|
293
|
+
if (star.retention<.25) {
|
|
294
|
+
ctx.strokeStyle='rgba(255,110,110,'+Math.max(0,.45-star.retention)+')';ctx.setLineDash([3,4]);
|
|
295
|
+
ctx.beginPath();ctx.arc(p.x,p.y,radius*3.2,0,Math.PI*2);ctx.stroke();ctx.setLineDash([]);
|
|
296
|
+
}
|
|
297
|
+
star._screen={x:p.x,y:p.y,r:Math.max(8,radius*2.6)};
|
|
298
|
+
}
|
|
299
|
+
for (const [id,value] of firing) {
|
|
300
|
+
const next=value*Math.pow(.965,delta/16.7);
|
|
301
|
+
if (next<.02) firing.delete(id); else firing.set(id,next);
|
|
302
|
+
}
|
|
303
|
+
ctx.globalCompositeOperation='source-over';
|
|
304
|
+
const vignette=ctx.createRadialGradient(width/2,height/2,Math.min(width,height)*.34,width/2,height/2,Math.max(width,height)*.8);
|
|
305
|
+
vignette.addColorStop(0,'rgba(0,0,0,0)');vignette.addColorStop(1,'rgba(0,0,0,.78)');
|
|
306
|
+
ctx.fillStyle=vignette;ctx.fillRect(0,0,width,height);
|
|
307
|
+
}
|
|
308
|
+
function start() {
|
|
309
|
+
if (running || destroyed) return;
|
|
310
|
+
running=true; lastTime=0; nextFire=0; autoSpin=true; resumeAt=0;
|
|
311
|
+
const currentGeneration = ++generation;
|
|
312
|
+
canvas.addEventListener('pointerdown',onPointerDown);
|
|
313
|
+
canvas.addEventListener('pointermove',onPointerMove);
|
|
314
|
+
canvas.addEventListener('pointerup',onPointerUp);
|
|
315
|
+
canvas.addEventListener('pointercancel',releaseDrag);
|
|
316
|
+
canvas.addEventListener('wheel',onWheel,{passive:false});
|
|
317
|
+
window.addEventListener('resize',resize);
|
|
318
|
+
if (window.ResizeObserver) {
|
|
319
|
+
observer = new window.ResizeObserver(resize);
|
|
320
|
+
observer.observe(canvas);
|
|
321
|
+
}
|
|
322
|
+
resize();
|
|
323
|
+
function tick(now) {
|
|
324
|
+
if (!running || destroyed || currentGeneration !== generation) return;
|
|
325
|
+
rafId=null;
|
|
326
|
+
frame(now);
|
|
327
|
+
if (running && !destroyed && currentGeneration === generation) rafId=window.requestAnimationFrame(tick);
|
|
328
|
+
}
|
|
329
|
+
rafId=window.requestAnimationFrame(tick);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function stop() {
|
|
333
|
+
running=false; generation++;
|
|
334
|
+
if (rafId !== null) window.cancelAnimationFrame(rafId);
|
|
335
|
+
rafId=null;
|
|
336
|
+
observer?.disconnect(); observer=null;
|
|
337
|
+
canvas.removeEventListener('pointerdown',onPointerDown);
|
|
338
|
+
canvas.removeEventListener('pointermove',onPointerMove);
|
|
339
|
+
canvas.removeEventListener('pointerup',onPointerUp);
|
|
340
|
+
canvas.removeEventListener('pointercancel',releaseDrag);
|
|
341
|
+
canvas.removeEventListener('wheel',onWheel);
|
|
342
|
+
window.removeEventListener('resize',resize);
|
|
343
|
+
releaseDrag();
|
|
344
|
+
pulses.length=0; firing.clear(); lastTime=0; nextFire=0;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function destroy() {
|
|
348
|
+
if (destroyed) return;
|
|
349
|
+
stop(); destroyed=true;
|
|
350
|
+
selections.clear(); byId.clear(); adjacency.clear(); sprites.clear();
|
|
351
|
+
nodes=[]; links=[]; hubs=[];
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
setData(data);
|
|
355
|
+
return {
|
|
356
|
+
start, stop, resize, setData, destroy,
|
|
357
|
+
fireNeuron(id) { if (!destroyed && byId.has(id)) fire(id,performance.now()); },
|
|
358
|
+
onSelect(callback) {
|
|
359
|
+
if (!destroyed) selections.add(callback);
|
|
360
|
+
return () => selections.delete(callback);
|
|
361
|
+
},
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
var defaultSettings = { hideButton: false, showGuideOnEmpty: true };
|
|
368
|
+
var actionStyle = { padding: "7px 12px", borderRadius: "8px", border: "1px solid #6999b8", background: "#102236", color: "#e2f4ff", font: "inherit", cursor: "pointer" };
|
|
369
|
+
var fieldStyle = { boxSizing: "border-box", padding: "10px 12px", borderRadius: "8px", border: "1px solid #35536b", background: "#091422", color: "#e2f4ff", font: "inherit", minWidth: 0 };
|
|
370
|
+
var kinds = ["知识", "印象", "对话", "事件", "日记"];
|
|
371
|
+
|
|
372
|
+
function requestApi(url, options) {
|
|
373
|
+
return fetch(url, Object.assign({ cache: "no-store" }, options)).then(function (response) {
|
|
374
|
+
return response.json().then(function (result) {
|
|
375
|
+
if (!response.ok || !result || result.ok !== true) throw new Error(result && result.error || "HTTP " + response.status);
|
|
376
|
+
if (!result.data || typeof result.data !== "object") throw new Error("接口返回格式无效");
|
|
377
|
+
return result.data;
|
|
378
|
+
});
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function readApi(url, signal) { return requestApi(url, { signal: signal }); }
|
|
383
|
+
|
|
384
|
+
function loadSettings() {
|
|
385
|
+
var controller = new AbortController();
|
|
386
|
+
var timer = setTimeout(function () { controller.abort(); }, 5000);
|
|
387
|
+
return Promise.resolve().then(function () {
|
|
388
|
+
return readApi("/memory-engine/api/config", controller.signal);
|
|
389
|
+
}).then(function (settings) {
|
|
390
|
+
if (typeof settings.hideButton !== "boolean" || typeof settings.showGuideOnEmpty !== "boolean") throw new Error("按钮配置格式无效");
|
|
391
|
+
return settings;
|
|
392
|
+
}).finally(function () { clearTimeout(timer); });
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function copyText(text) {
|
|
396
|
+
function fallback() {
|
|
397
|
+
var area = document.createElement("textarea");
|
|
398
|
+
var active = document.activeElement;
|
|
399
|
+
area.value = text;
|
|
400
|
+
area.style.cssText = "position:fixed;left:-9999px;top:0";
|
|
401
|
+
document.body.appendChild(area);
|
|
402
|
+
area.select();
|
|
403
|
+
try {
|
|
404
|
+
if (!document.execCommand("copy")) throw new Error("复制失败");
|
|
405
|
+
} finally {
|
|
406
|
+
area.remove();
|
|
407
|
+
if (active && typeof active.focus === "function") active.focus();
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return Promise.resolve().then(function () {
|
|
411
|
+
if (navigator.clipboard && navigator.clipboard.writeText) return navigator.clipboard.writeText(text);
|
|
412
|
+
return fallback();
|
|
413
|
+
}).catch(function () { return fallback(); });
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function GuideCard(props) {
|
|
417
|
+
var copiedState = React.useState("复制这段提示词");
|
|
418
|
+
var copyLabel = copiedState[0];
|
|
419
|
+
var setCopyLabel = copiedState[1];
|
|
420
|
+
var card = React.useRef(null);
|
|
421
|
+
React.useEffect(function () { if (card.current) card.current.focus(); }, []);
|
|
422
|
+
function trapTab(event) {
|
|
423
|
+
if (event.key !== "Tab" || !card.current) return;
|
|
424
|
+
var buttons = Array.from(card.current.querySelectorAll("button"));
|
|
425
|
+
var position = buttons.indexOf(document.activeElement);
|
|
426
|
+
if (event.shiftKey && position <= 0) { event.preventDefault(); buttons[buttons.length - 1].focus(); }
|
|
427
|
+
else if (!event.shiftKey && (position === -1 || position === buttons.length - 1)) { event.preventDefault(); buttons[0].focus(); }
|
|
428
|
+
}
|
|
429
|
+
var data = props.data;
|
|
430
|
+
return React.createElement("div", {
|
|
431
|
+
"data-memory-engine-guide-layer": "true",
|
|
432
|
+
style: { position: "absolute", inset: 0, zIndex: 2, display: "grid", placeItems: "center", padding: "16px", background: "rgba(3,6,12,.7)" },
|
|
433
|
+
}, React.createElement("section", {
|
|
434
|
+
ref: card, role: "dialog", "aria-modal": "true", "aria-label": "使用指南", tabIndex: -1,
|
|
435
|
+
"data-memory-engine-guide": "true", onKeyDown: trapTab,
|
|
436
|
+
style: { boxSizing: "border-box", width: "min(620px,100%)", maxHeight: "calc(100vh - 32px)", overflow: "auto", padding: "24px", border: "1px solid #3976a0", borderRadius: "16px", background: "#07111f", color: "#dbeaf8", font: "14px/1.65 system-ui,sans-serif", boxShadow: "0 16px 80px #0008" },
|
|
437
|
+
}, React.createElement("h2", { style: { margin: "0 0 16px", fontSize: "22px", color: "#b7e9ff" } }, "怎么用"),
|
|
438
|
+
props.error ? React.createElement("div", { role: "alert" },
|
|
439
|
+
React.createElement("p", null, "引导加载失败,请重试。"),
|
|
440
|
+
React.createElement("button", { type: "button", onClick: props.onRetry, style: actionStyle }, "重试"))
|
|
441
|
+
: !data ? React.createElement("p", { role: "status" }, "正在加载使用指南……")
|
|
442
|
+
: React.createElement(React.Fragment, null,
|
|
443
|
+
data.empty ? React.createElement("p", { style: { color: "#9fb7ce" } }, "还没有记忆,从下面任一步开始。") : null,
|
|
444
|
+
data.steps.map(function (step, index) {
|
|
445
|
+
return React.createElement("div", { key: index, style: { borderLeft: "3px solid #28577e", paddingLeft: "14px", marginBottom: "18px" } },
|
|
446
|
+
React.createElement("h3", { style: { fontSize: "15px", color: "#9fd0f5", margin: "0 0 6px" } }, "第 " + (index + 1) + " 步 " + step.title),
|
|
447
|
+
React.createElement("p", { style: { margin: "0 0 8px" } }, step.body),
|
|
448
|
+
index === 0 ? React.createElement(React.Fragment, null,
|
|
449
|
+
React.createElement("pre", { style: { whiteSpace: "pre-wrap", overflowWrap: "anywhere", maxHeight: "180px", overflow: "auto", margin: "8px 0", padding: "12px", background: "#030a13", borderRadius: "8px", font: "12px/1.6 ui-monospace,Consolas,monospace" } }, data.prompt),
|
|
450
|
+
React.createElement("button", { type: "button", "data-memory-engine-copy": "true", style: actionStyle,
|
|
451
|
+
onClick: function () { copyText(data.prompt).then(function () { setCopyLabel("已复制 ✓"); }, function () { setCopyLabel("复制失败,请手动复制"); }); },
|
|
452
|
+
}, copyLabel)) : null,
|
|
453
|
+
index === 2 ? React.createElement("button", { type: "button", onClick: props.onRefresh, style: actionStyle }, "刷新图谱") : null);
|
|
454
|
+
}),
|
|
455
|
+
React.createElement("p", { style: { fontSize: "12px", color: "#9fb7ce", overflowWrap: "anywhere" } }, "数据目录:", data.dataDir)),
|
|
456
|
+
React.createElement("div", { style: { display: "flex", justifyContent: "flex-end", marginTop: "16px" } },
|
|
457
|
+
React.createElement("button", { type: "button", onClick: props.onClose, style: actionStyle }, "知道了"))));
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
function MemoryDetails(props) {
|
|
461
|
+
var node = props.node;
|
|
462
|
+
var text = typeof node.text === "string" ? node.text : "";
|
|
463
|
+
if (node.isCore) text = Array.from(text).slice(0, 4000).join("");
|
|
464
|
+
var defaultCopyLabel = node.isCore ? "复制全文" : "复制正文";
|
|
465
|
+
var title = String(node.title || "").trim() || Array.from(text).slice(0, 20).join("");
|
|
466
|
+
var date = new Date(node.ts);
|
|
467
|
+
var day = Number.isFinite(date.getTime()) ? date.getFullYear() + "-" + String(date.getMonth() + 1).padStart(2, "0") + "-" + String(date.getDate()).padStart(2, "0") : "未知";
|
|
468
|
+
var heat = { 1: "热", 2: "温", 3: "冷" }[node.heat];
|
|
469
|
+
var rows = [
|
|
470
|
+
["重要度", heat ? heat + "(" + node.heat + ")" : "未知"],
|
|
471
|
+
["留存", Number.isFinite(node.retention) ? node.retention.toFixed(2) : "—"],
|
|
472
|
+
["连接数", node.degree == null ? 0 : node.degree],
|
|
473
|
+
["时间", day],
|
|
474
|
+
];
|
|
475
|
+
var emotion = Array.isArray(node.emotion) ? node.emotion.filter(function (item) { return item && item.tag; }).map(function (item) { return item.tag; }).join(" / ") : "";
|
|
476
|
+
if (emotion) rows.push(["情绪", emotion]);
|
|
477
|
+
var copiedState = React.useState(defaultCopyLabel);
|
|
478
|
+
var copyLabel = copiedState[0];
|
|
479
|
+
var setCopyLabel = copiedState[1];
|
|
480
|
+
var timer = React.useRef(null);
|
|
481
|
+
var copyVersion = React.useRef(0);
|
|
482
|
+
var body = React.useRef(null);
|
|
483
|
+
React.useEffect(function () {
|
|
484
|
+
copyVersion.current++;
|
|
485
|
+
clearTimeout(timer.current);
|
|
486
|
+
timer.current = null;
|
|
487
|
+
setCopyLabel(defaultCopyLabel);
|
|
488
|
+
if (body.current) body.current.scrollTop = 0;
|
|
489
|
+
return function () { copyVersion.current++; clearTimeout(timer.current); timer.current = null; };
|
|
490
|
+
}, [node.id, text, defaultCopyLabel]);
|
|
491
|
+
function copyBody() {
|
|
492
|
+
var version = ++copyVersion.current;
|
|
493
|
+
clearTimeout(timer.current);
|
|
494
|
+
copyText(text).then(function () {
|
|
495
|
+
if (version !== copyVersion.current) return;
|
|
496
|
+
setCopyLabel("已复制 ✓");
|
|
497
|
+
timer.current = setTimeout(function () {
|
|
498
|
+
if (version === copyVersion.current) setCopyLabel(defaultCopyLabel);
|
|
499
|
+
timer.current = null;
|
|
500
|
+
}, 1800);
|
|
501
|
+
}, function () {
|
|
502
|
+
if (version === copyVersion.current) setCopyLabel("复制失败,请重试");
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
return React.createElement("section", {
|
|
506
|
+
role: "region", "aria-label": "记忆详情", "data-memory-engine-detail": "true", "data-memory-engine-node": node.id,
|
|
507
|
+
style: { position: "absolute", top: "72px", right: "18px", zIndex: 1, width: "min(350px,calc(100% - 36px))", maxHeight: "calc(100% - 96px)", boxSizing: "border-box", display: "flex", flexDirection: "column", overflow: "hidden", padding: "18px", border: "1px solid #3976a0", borderRadius: "14px", background: "#07111fee", color: "#dbeaf8", font: "14px/1.6 system-ui,sans-serif", boxShadow: "0 0 40px #1763a033", backdropFilter: "blur(8px)", animation: "dsh-memory-detail-in 180ms ease-out" },
|
|
508
|
+
}, React.createElement("div", { style: { display: "flex", alignItems: "center", justifyContent: "space-between", gap: "12px", flexShrink: 0 } },
|
|
509
|
+
React.createElement("span", { "data-memory-engine-detail-kind": "true", style: { color: "#a4e6ff", background: "#15344b", borderRadius: "6px", padding: "2px 8px", overflowWrap: "anywhere" } }, node.kind || "未分类"),
|
|
510
|
+
React.createElement("button", { type: "button", "aria-label": "关闭记忆详情", onClick: props.onClose, style: { border: 0, padding: "2px 6px", background: "transparent", color: "#a4bad0", fontSize: "22px", cursor: "pointer" } }, "×")),
|
|
511
|
+
React.createElement("h2", { "data-memory-engine-detail-title": "true", style: { margin: "10px 0", fontSize: "18px", color: "#b7e9ff", overflowWrap: "anywhere", whiteSpace: "normal", maxHeight: "18vh", overflowY: "auto", flexShrink: 0 } }, title),
|
|
512
|
+
node.isCore ? null : React.createElement("dl", { style: { display: "grid", gridTemplateColumns: "64px minmax(0,1fr)", gap: "5px 10px", margin: "0 0 12px", fontSize: "12px", color: "#9fb7ce", maxHeight: "25vh", overflowY: "auto", flexShrink: 0 } },
|
|
513
|
+
rows.map(function (row) { return React.createElement(React.Fragment, { key: row[0] },
|
|
514
|
+
React.createElement("dt", { style: { margin: 0 } }, row[0]),
|
|
515
|
+
React.createElement("dd", { style: { margin: 0, overflowWrap: "anywhere" } }, row[1])); })),
|
|
516
|
+
React.createElement("div", { ref: body, "data-memory-engine-detail-text": "true", style: { minHeight: 0, flex: "1 1 auto", overflowY: "auto", whiteSpace: "pre-wrap", overflowWrap: "anywhere", borderTop: "1px solid #3976a055", borderBottom: "1px solid #3976a055", padding: "12px 0" } }, text),
|
|
517
|
+
React.createElement("div", { style: { display: "flex", flexWrap: "wrap", gap: "8px", marginTop: "12px", flexShrink: 0 } },
|
|
518
|
+
node.isCore ? null : React.createElement("button", { type: "button", "data-memory-engine-highlight": "true", style: actionStyle, onClick: props.onHighlight }, "在图里高亮它"),
|
|
519
|
+
React.createElement("button", { type: "button", "data-memory-engine-copy-body": "true", style: actionStyle, onClick: copyBody }, copyLabel)));
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function MemoryCanvas(props) {
|
|
523
|
+
var canvas = React.useRef(null);
|
|
524
|
+
var dataState = React.useState(null);
|
|
525
|
+
var data = dataState[0];
|
|
526
|
+
var setData = dataState[1];
|
|
527
|
+
var errorState = React.useState("");
|
|
528
|
+
var error = errorState[0];
|
|
529
|
+
var setError = errorState[1];
|
|
530
|
+
var retryState = React.useState(0);
|
|
531
|
+
var retry = retryState[0];
|
|
532
|
+
var setRetry = retryState[1];
|
|
533
|
+
React.useEffect(function () {
|
|
534
|
+
var controller = new AbortController();
|
|
535
|
+
var active = true;
|
|
536
|
+
var renderer;
|
|
537
|
+
setError("");
|
|
538
|
+
readApi("/memory-engine/api/map", controller.signal).then(function (nextData) {
|
|
539
|
+
if (!active) return;
|
|
540
|
+
renderer = createRenderer(canvas.current, nextData);
|
|
541
|
+
props.rendererRef.current = renderer;
|
|
542
|
+
renderer.onSelect(function (node) { props.selectionVersion.current++; props.onSelect(node); });
|
|
543
|
+
renderer.start();
|
|
544
|
+
setData(nextData);
|
|
545
|
+
}).catch(function (failure) {
|
|
546
|
+
if (!active) return;
|
|
547
|
+
if (renderer) renderer.destroy();
|
|
548
|
+
if (props.rendererRef.current === renderer) props.rendererRef.current = null;
|
|
549
|
+
console.error("[dsh-memory-engine] Canvas 图谱加载失败:", failure);
|
|
550
|
+
setError(failure.message);
|
|
551
|
+
});
|
|
552
|
+
return function () {
|
|
553
|
+
active = false;
|
|
554
|
+
controller.abort();
|
|
555
|
+
if (renderer) renderer.destroy();
|
|
556
|
+
if (props.rendererRef.current === renderer) props.rendererRef.current = null;
|
|
557
|
+
};
|
|
558
|
+
}, [retry, props.rendererRef, props.selectionVersion, props.onSelect]);
|
|
559
|
+
return React.createElement(React.Fragment, null,
|
|
560
|
+
React.createElement("canvas", {
|
|
561
|
+
ref: canvas, "data-memory-engine-canvas": "true", "aria-label": "记忆神经网络",
|
|
562
|
+
style: { display: "block", width: "100%", height: "100%", touchAction: "none", cursor: "grab" },
|
|
563
|
+
}),
|
|
564
|
+
React.createElement("div", {
|
|
565
|
+
"data-memory-engine-summary": "true",
|
|
566
|
+
style: { position: "absolute", top: "16px", left: "18px", pointerEvents: "none", color: "#d8e9f8", font: "13px/1.65 system-ui,sans-serif", textShadow: "0 0 12px #3388bb88" },
|
|
567
|
+
}, React.createElement("strong", { style: { color: "#a4e6ff", fontSize: "16px" } }, "记忆神经网络"),
|
|
568
|
+
React.createElement("div", null, data ? data.nodes.length + " 个神经元 · " + data.links.length + " 条突触" : "正在加载图谱……"),
|
|
569
|
+
React.createElement("div", { style: { color: "#7491a8", fontSize: "12px" } }, "大小=重要度和连接数 · 亮度=留存")),
|
|
570
|
+
data ? React.createElement("section", {
|
|
571
|
+
"aria-label": "复习清单",
|
|
572
|
+
style: { position: "absolute", bottom: "16px", left: "18px", width: "min(400px,calc(100% - 36px))", boxSizing: "border-box", maxHeight: "32%", overflow: "auto", padding: "15px", background: "#07111fee", border: "1px solid #3976a055", borderRadius: "14px", boxShadow: "0 0 40px #1763a033", backdropFilter: "blur(8px)", font: "12px/1.65 system-ui,sans-serif", color: "#b1c6d9" },
|
|
573
|
+
}, React.createElement("h2", { style: { fontSize: "14px", color: "#ffdb82", margin: "0 0 8px" } }, "复习清单 · 留存最低的记忆"),
|
|
574
|
+
data.review.map(function (item, index) {
|
|
575
|
+
return React.createElement("div", { key: item.id || index, style: { display: "flex", gap: "7px", alignItems: "center", margin: "5px 0" } },
|
|
576
|
+
React.createElement("span", { style: { flex: 1, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" } }, (index + 1) + ". [" + item.kind + "] " + item.title),
|
|
577
|
+
React.createElement("span", { style: { width: "52px", height: "5px", borderRadius: "5px", background: "#335066", overflow: "hidden" } },
|
|
578
|
+
React.createElement("i", { style: { display: "block", height: "100%", background: "#6bc9fa", width: Math.min(100, Math.max(0, item.retention * 100)) + "%" } })));
|
|
579
|
+
})) : null,
|
|
580
|
+
React.createElement("div", { style: { position: "absolute", right: "18px", bottom: "18px", pointerEvents: "none", color: "#7b99ad", font: "13px system-ui,sans-serif" } }, "拖拽旋转 · 滚轮缩放 · 点击选中神经元"),
|
|
581
|
+
error ? React.createElement("div", { role: "alert", style: { position: "absolute", inset: "40% 20% auto", textAlign: "center", color: "#d8e9f8" } },
|
|
582
|
+
React.createElement("p", null, "图谱加载失败:" + error),
|
|
583
|
+
React.createElement("button", { type: "button", style: actionStyle, onClick: function () { setRetry(function (value) { return value + 1; }); } }, "重试")) : null);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
function ConfirmationDialog(props) {
|
|
587
|
+
var valueState = React.useState("");
|
|
588
|
+
var value = valueState[0], setValue = valueState[1];
|
|
589
|
+
var errorState = React.useState("");
|
|
590
|
+
var error = errorState[0], setError = errorState[1];
|
|
591
|
+
var form = React.useRef(null);
|
|
592
|
+
React.useEffect(function () {
|
|
593
|
+
var active = document.activeElement;
|
|
594
|
+
var target = form.current && form.current.querySelector("input,button");
|
|
595
|
+
if (target) target.focus();
|
|
596
|
+
return function () { if (active && active.isConnected) active.focus(); };
|
|
597
|
+
}, []);
|
|
598
|
+
function onKey(event) {
|
|
599
|
+
if (event.key === "Escape") {
|
|
600
|
+
event.preventDefault(); event.stopPropagation();
|
|
601
|
+
if (!props.busy) props.onClose();
|
|
602
|
+
}
|
|
603
|
+
if (event.key === "Tab" && form.current) {
|
|
604
|
+
var controls = Array.from(form.current.querySelectorAll("input,button")).filter(function (control) { return !control.disabled; });
|
|
605
|
+
if (!controls.length) { event.preventDefault(); return; }
|
|
606
|
+
var index = controls.indexOf(document.activeElement);
|
|
607
|
+
if (event.shiftKey && index <= 0) { event.preventDefault(); controls[controls.length - 1].focus(); }
|
|
608
|
+
else if (!event.shiftKey && (index === -1 || index === controls.length - 1)) { event.preventDefault(); controls[0].focus(); }
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
return React.createElement("div", { "data-memory-engine-confirm": "true", onKeyDown: onKey,
|
|
612
|
+
style: { position: "fixed", inset: 0, zIndex: 3, display: "grid", placeItems: "center", padding: "24px", background: "#03060ccc" } },
|
|
613
|
+
React.createElement("form", { ref: form, role: "dialog", "aria-modal": "true", "aria-label": props.title,
|
|
614
|
+
onSubmit: function (event) {
|
|
615
|
+
event.preventDefault();
|
|
616
|
+
if (props.busy) return;
|
|
617
|
+
if (props.requireDelete && value !== "DELETE") { setError("请输入大写 DELETE,其他内容不会执行清空。"); return; }
|
|
618
|
+
setError(""); props.onConfirm();
|
|
619
|
+
},
|
|
620
|
+
style: { width: "min(460px,100%)", padding: "24px", border: "1px solid #8b505c", borderRadius: "16px", background: "#101a29", color: "#e2f4ff", boxShadow: "0 16px 80px #0008" } },
|
|
621
|
+
React.createElement("h2", { style: { margin: "0 0 12px", fontSize: "20px" } }, props.title),
|
|
622
|
+
React.createElement("p", { style: { color: "#c2cedc" } }, props.message),
|
|
623
|
+
props.requireDelete ? React.createElement("label", null, "输入 DELETE 确认",
|
|
624
|
+
React.createElement("input", { "aria-label": "输入 DELETE 确认", autoComplete: "off", value: value, disabled: props.busy,
|
|
625
|
+
onChange: function (event) { setValue(event.target.value); setError(""); }, style: Object.assign({}, fieldStyle, { display: "block", width: "100%", marginTop: "8px" }) })) : null,
|
|
626
|
+
error || props.error ? React.createElement("p", { role: "alert", style: { color: "#ffb8b8" } }, error || props.error) : null,
|
|
627
|
+
React.createElement("div", { style: { display: "flex", justifyContent: "flex-end", gap: "10px", marginTop: "24px" } },
|
|
628
|
+
React.createElement("button", { type: "button", disabled: props.busy, onClick: props.onClose, style: actionStyle }, "取消"),
|
|
629
|
+
React.createElement("button", { type: "submit", disabled: props.busy, style: Object.assign({}, actionStyle, { borderColor: "#c86b7a", background: "#572a38" }) }, props.busy ? "正在处理…" : props.submitLabel))));
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
function MemoryRow(props) {
|
|
633
|
+
var item = props.item;
|
|
634
|
+
var expandedState = React.useState(false);
|
|
635
|
+
var expanded = expandedState[0], setExpanded = expandedState[1];
|
|
636
|
+
var detailState = React.useState(item);
|
|
637
|
+
var detail = detailState[0], setDetail = detailState[1];
|
|
638
|
+
var editState = React.useState(null);
|
|
639
|
+
var edit = editState[0], setEdit = editState[1];
|
|
640
|
+
var confirmState = React.useState(false);
|
|
641
|
+
var confirming = confirmState[0], setConfirming = confirmState[1];
|
|
642
|
+
var errorState = React.useState("");
|
|
643
|
+
var error = errorState[0], setError = errorState[1];
|
|
644
|
+
var loadingState = React.useState(false);
|
|
645
|
+
var loading = loadingState[0], setLoading = loadingState[1];
|
|
646
|
+
var busyState = React.useState(false);
|
|
647
|
+
var busy = busyState[0], setBusy = busyState[1];
|
|
648
|
+
var mutation = React.useRef(null);
|
|
649
|
+
React.useEffect(function () { return function () { if (mutation.current) mutation.current.abort(); }; }, []);
|
|
650
|
+
React.useEffect(function () {
|
|
651
|
+
if (!expanded) return;
|
|
652
|
+
var controller = new AbortController();
|
|
653
|
+
setLoading(true); setError("");
|
|
654
|
+
readApi("/memory-engine/api/item/" + encodeURIComponent(item.id), controller.signal).then(function (result) {
|
|
655
|
+
if (!controller.signal.aborted) setDetail(result);
|
|
656
|
+
}).catch(function (failure) {
|
|
657
|
+
if (!controller.signal.aborted) setError("读取详情失败:" + failure.message);
|
|
658
|
+
}).finally(function () { if (!controller.signal.aborted) setLoading(false); });
|
|
659
|
+
return function () { controller.abort(); };
|
|
660
|
+
}, [expanded, item.id]);
|
|
661
|
+
function mutate(method) {
|
|
662
|
+
if (mutation.current) return;
|
|
663
|
+
var controller = new AbortController();
|
|
664
|
+
mutation.current = controller;
|
|
665
|
+
setBusy(true); setError("");
|
|
666
|
+
var options = { method: method, signal: controller.signal };
|
|
667
|
+
if (method === "PUT") { options.headers = { "Content-Type": "application/json" }; options.body = JSON.stringify(edit); }
|
|
668
|
+
requestApi("/memory-engine/api/item/" + encodeURIComponent(item.id), options).then(function () {
|
|
669
|
+
if (controller.signal.aborted) return;
|
|
670
|
+
setEdit(null); setExpanded(false); setConfirming(false); props.onChanged();
|
|
671
|
+
}).catch(function (failure) {
|
|
672
|
+
if (!controller.signal.aborted) setError((method === "PUT" ? "保存失败:" : "删除失败:") + failure.message);
|
|
673
|
+
}).finally(function () {
|
|
674
|
+
if (!controller.signal.aborted) { mutation.current = null; setBusy(false); }
|
|
675
|
+
});
|
|
676
|
+
}
|
|
677
|
+
function change(field, value) { setEdit(function (previous) { var next = Object.assign({}, previous); next[field] = value; return next; }); }
|
|
678
|
+
var choices = kinds.includes(detail.kind) ? kinds : [detail.kind].concat(kinds);
|
|
679
|
+
return React.createElement("article", { "data-memory-engine-row": item.id,
|
|
680
|
+
style: { marginBottom: "12px", border: "1px solid " + (expanded ? "#4c85a4" : "#263f54"), borderRadius: "12px", background: "#0b1624", overflowWrap: "anywhere" } },
|
|
681
|
+
React.createElement("button", { type: "button", "data-memory-engine-expand": item.id, "aria-expanded": expanded, disabled: busy,
|
|
682
|
+
onClick: function () { setExpanded(!expanded); setEdit(null); setError(""); },
|
|
683
|
+
style: { width: "100%", display: "block", padding: "18px 20px", border: 0, borderRadius: "12px", background: "transparent", color: "inherit", textAlign: "left", font: "inherit", cursor: "pointer" } },
|
|
684
|
+
React.createElement("span", { style: { display: "flex", alignItems: "center", gap: "12px", flexWrap: "wrap" } },
|
|
685
|
+
React.createElement("span", { style: { color: "#a4e6ff", background: "#15344b", padding: "2px 8px", borderRadius: "6px", flexShrink: 0 } }, item.kind),
|
|
686
|
+
React.createElement("strong", { style: { flex: "1 1 180px", overflowWrap: "anywhere" } }, item.title),
|
|
687
|
+
React.createElement("span", { style: { color: "#8db0c7", fontSize: "12px", whiteSpace: "nowrap" } }, "留存 " + item.retention.toFixed(2) + " · " + item.degree + " 个连接")),
|
|
688
|
+
!expanded ? React.createElement("span", { style: { display: "block", color: "#9fb7ce", marginTop: "8px", overflowWrap: "anywhere" } }, item.preview) : null),
|
|
689
|
+
expanded ? React.createElement("div", { style: { padding: "0 20px 18px" } },
|
|
690
|
+
loading ? React.createElement("p", { role: "status" }, "正在读取全文…") : null,
|
|
691
|
+
edit ? React.createElement("form", { "data-memory-engine-editor": item.id, onSubmit: function (event) { event.preventDefault(); mutate("PUT"); } },
|
|
692
|
+
React.createElement("textarea", { "aria-label": "记忆正文", value: edit.text, disabled: busy,
|
|
693
|
+
onChange: function (event) { change("text", event.target.value); },
|
|
694
|
+
style: Object.assign({}, fieldStyle, { width: "100%", minHeight: "150px", maxHeight: "40vh", resize: "vertical", lineHeight: 1.7 }) }),
|
|
695
|
+
React.createElement("div", { style: { display: "flex", gap: "12px", flexWrap: "wrap", margin: "12px 0" } },
|
|
696
|
+
React.createElement("label", null, "分类 ", React.createElement("select", { "aria-label": "编辑分类", value: edit.kind, disabled: busy, style: fieldStyle,
|
|
697
|
+
onChange: function (event) { change("kind", event.target.value); } }, choices.map(function (value) { return React.createElement("option", { key: value, value: value }, value); }))),
|
|
698
|
+
React.createElement("label", null, "重要度 ", React.createElement("select", { "aria-label": "编辑重要度", value: edit.heat, disabled: busy, style: fieldStyle,
|
|
699
|
+
onChange: function (event) { change("heat", Number(event.target.value)); } },
|
|
700
|
+
[[1, "热(1)"], [2, "温(2)"], [3, "冷(3)"]].map(function (entry) { return React.createElement("option", { key: entry[0], value: entry[0] }, entry[1]); }))),
|
|
701
|
+
React.createElement("label", null, "相关人 ", React.createElement("input", { "aria-label": "编辑相关人", value: edit.who, disabled: busy,
|
|
702
|
+
onChange: function (event) { change("who", event.target.value); }, style: Object.assign({}, fieldStyle, { width: "140px" }) }))),
|
|
703
|
+
React.createElement("div", { style: { display: "flex", gap: "8px" } },
|
|
704
|
+
React.createElement("button", { type: "submit", disabled: busy, "data-memory-engine-save": "true", style: actionStyle }, busy ? "正在保存…" : "保存"),
|
|
705
|
+
React.createElement("button", { type: "button", disabled: busy, "data-memory-engine-cancel-edit": "true", style: actionStyle,
|
|
706
|
+
onClick: function () { setEdit(null); setExpanded(false); setError(""); } }, "取消")))
|
|
707
|
+
: React.createElement(React.Fragment, null,
|
|
708
|
+
React.createElement("div", { "data-memory-engine-full-text": "true", style: { whiteSpace: "pre-wrap", maxHeight: "40vh", overflowY: "auto", color: "#c2d3e3", marginBottom: "14px" } }, detail.text),
|
|
709
|
+
React.createElement("div", { style: { display: "flex", gap: "8px" } },
|
|
710
|
+
React.createElement("button", { type: "button", "data-memory-engine-edit": item.id, disabled: loading || busy, style: actionStyle,
|
|
711
|
+
onClick: function () { setError(""); setEdit({ text: detail.text, kind: detail.kind, heat: detail.heat, who: detail.who || "" }); } }, "编辑"),
|
|
712
|
+
React.createElement("button", { type: "button", "data-memory-engine-delete": item.id, disabled: loading || busy,
|
|
713
|
+
onClick: function () { setError(""); setConfirming(true); }, style: Object.assign({}, actionStyle, { color: "#ffb8b8", borderColor: "#8b505c" }) }, "删除"))),
|
|
714
|
+
error ? React.createElement("p", { role: "alert", style: { color: "#ffb8b8" } }, error) : null) : null,
|
|
715
|
+
confirming ? React.createElement(ConfirmationDialog, { title: "删除这条记忆", message: "确定删除这条记忆?不可撤销。操作前会保留备份。", submitLabel: "确定删除",
|
|
716
|
+
busy: busy, error: error, onClose: function () { setConfirming(false); }, onConfirm: function () { mutate("DELETE"); } }) : null);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
function MemoryList(props) {
|
|
720
|
+
var searchState = React.useState("");
|
|
721
|
+
var search = searchState[0], setSearch = searchState[1];
|
|
722
|
+
var queryState = React.useState("");
|
|
723
|
+
var query = queryState[0], setQuery = queryState[1];
|
|
724
|
+
var kindState = React.useState("");
|
|
725
|
+
var kind = kindState[0], setKind = kindState[1];
|
|
726
|
+
var sortState = React.useState("recent");
|
|
727
|
+
var sort = sortState[0], setSort = sortState[1];
|
|
728
|
+
var offsetState = React.useState(0);
|
|
729
|
+
var offset = offsetState[0], setOffset = offsetState[1];
|
|
730
|
+
var pageState = React.useState({ items: [], total: 0, allTotal: 0 });
|
|
731
|
+
var page = pageState[0], setPage = pageState[1];
|
|
732
|
+
var loadingState = React.useState(true);
|
|
733
|
+
var loading = loadingState[0], setLoading = loadingState[1];
|
|
734
|
+
var errorState = React.useState("");
|
|
735
|
+
var error = errorState[0], setError = errorState[1];
|
|
736
|
+
var retryState = React.useState(0);
|
|
737
|
+
var retry = retryState[0], setRetry = retryState[1];
|
|
738
|
+
function refresh() {
|
|
739
|
+
setOffset(0); setRetry(function (value) { return value + 1; });
|
|
740
|
+
if (props.onChanged) props.onChanged();
|
|
741
|
+
}
|
|
742
|
+
React.useEffect(function () {
|
|
743
|
+
if (search === query) return;
|
|
744
|
+
var timer = setTimeout(function () { setOffset(0); setQuery(search); }, 250);
|
|
745
|
+
return function () { clearTimeout(timer); };
|
|
746
|
+
}, [search]);
|
|
747
|
+
React.useEffect(function () {
|
|
748
|
+
var controller = new AbortController();
|
|
749
|
+
var active = true;
|
|
750
|
+
setLoading(true); setError("");
|
|
751
|
+
var params = new URLSearchParams({ q: query, kind: kind, sort: sort, limit: "50", offset: String(offset) });
|
|
752
|
+
readApi("/memory-engine/api/list?" + params, controller.signal).then(function (result) {
|
|
753
|
+
if (!active) return;
|
|
754
|
+
setPage(function (previous) { return Object.assign({}, result, { items: offset === 0 ? result.items : previous.items.concat(result.items) }); });
|
|
755
|
+
}).catch(function (failure) {
|
|
756
|
+
if (active) setError("加载记忆失败:" + failure.message);
|
|
757
|
+
}).finally(function () { if (active) setLoading(false); });
|
|
758
|
+
return function () { active = false; controller.abort(); };
|
|
759
|
+
}, [query, kind, sort, offset, retry]);
|
|
760
|
+
return React.createElement("section", {
|
|
761
|
+
"aria-label": "记忆列表", "data-memory-engine-list": "true",
|
|
762
|
+
style: { position: "absolute", inset: "76px 0 0", display: "flex", flexDirection: "column", color: "#dbeaf8", font: "14px/1.6 system-ui,sans-serif" },
|
|
763
|
+
}, React.createElement("div", { style: { width: "min(980px,100%)", margin: "0 auto", padding: "0 24px 16px", display: "flex", gap: "12px", flexWrap: "wrap" } },
|
|
764
|
+
React.createElement("input", { type: "search", "aria-label": "搜索记忆", placeholder: "搜索正文或标题…", value: search,
|
|
765
|
+
onChange: function (event) { setSearch(event.target.value); }, style: Object.assign({}, fieldStyle, { flex: "1 1 220px" }) }),
|
|
766
|
+
React.createElement("select", { "aria-label": "分类筛选", value: kind, style: fieldStyle,
|
|
767
|
+
onChange: function (event) { setOffset(0); setKind(event.target.value); } },
|
|
768
|
+
[""].concat(kinds).map(function (value) { return React.createElement("option", { key: value, value: value }, value || "全部分类"); })),
|
|
769
|
+
React.createElement("select", { "aria-label": "记忆排序", value: sort, style: fieldStyle,
|
|
770
|
+
onChange: function (event) { setOffset(0); setSort(event.target.value); } },
|
|
771
|
+
[["recent", "最近"], ["oldest", "最老"], ["fading", "快忘了"], ["degree", "连接最多"]].map(function (entry) { return React.createElement("option", { key: entry[0], value: entry[0] }, entry[1]); }))),
|
|
772
|
+
React.createElement("div", { "data-memory-engine-list-scroll": "true", style: { flex: 1, minHeight: 0, overflowY: "auto", padding: "0 24px 24px" } },
|
|
773
|
+
React.createElement("div", { style: { maxWidth: "932px", margin: "0 auto" }, "aria-busy": loading },
|
|
774
|
+
error ? React.createElement("div", { role: "alert", style: { color: "#ffb8b8", padding: "16px" } }, error,
|
|
775
|
+
React.createElement("button", { type: "button", style: actionStyle, onClick: function () { setRetry(function (value) { return value + 1; }); } }, "重试")) : null,
|
|
776
|
+
page.items.map(function (item) {
|
|
777
|
+
return React.createElement(MemoryRow, { key: item.id, item: item, onChanged: refresh });
|
|
778
|
+
}),
|
|
779
|
+
!loading && !error && !page.items.length ? React.createElement("p", { role: "status", style: { padding: "24px", textAlign: "center", color: "#9fb7ce" } }, page.allTotal ? "没有匹配的记忆,试试其他关键词或分类。" : "还没有记忆,可以点右上角 ? 查看导入方法。") : null)),
|
|
780
|
+
React.createElement("div", { "data-memory-engine-list-total": "true", style: { padding: "14px", textAlign: "center", color: "#9fb7ce", flexShrink: 0, borderTop: "1px solid #263f54" } },
|
|
781
|
+
"共 " + page.total + " 条 · 已显示 " + page.items.length + " 条",
|
|
782
|
+
loading ? React.createElement("span", { role: "status" }, " · 正在加载…") : null,
|
|
783
|
+
page.items.length < page.total ? React.createElement("button", { type: "button", "data-memory-engine-more": "true", disabled: loading || search !== query || !!error,
|
|
784
|
+
onClick: function () { setOffset(page.items.length); }, style: Object.assign({}, actionStyle, { marginLeft: "12px", opacity: loading ? 0.5 : 1 }) }, "加载更多") : null));
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
function MemorySettings(props) {
|
|
788
|
+
var statsState = React.useState(null);
|
|
789
|
+
var stats = statsState[0], setStats = statsState[1];
|
|
790
|
+
var errorState = React.useState("");
|
|
791
|
+
var error = errorState[0], setError = errorState[1];
|
|
792
|
+
var noticeState = React.useState("");
|
|
793
|
+
var notice = noticeState[0], setNotice = noticeState[1];
|
|
794
|
+
var retryState = React.useState(0);
|
|
795
|
+
var retry = retryState[0], setRetry = retryState[1];
|
|
796
|
+
var busyState = React.useState(false);
|
|
797
|
+
var busy = busyState[0], setBusy = busyState[1];
|
|
798
|
+
var confirmState = React.useState(false);
|
|
799
|
+
var confirming = confirmState[0], setConfirming = confirmState[1];
|
|
800
|
+
var mutation = React.useRef(null);
|
|
801
|
+
var download = React.useRef(null);
|
|
802
|
+
var timer = React.useRef(null);
|
|
803
|
+
var active = React.useRef(true);
|
|
804
|
+
React.useEffect(function () {
|
|
805
|
+
active.current = true;
|
|
806
|
+
return function () {
|
|
807
|
+
active.current = false;
|
|
808
|
+
if (mutation.current) mutation.current.abort();
|
|
809
|
+
clearTimeout(timer.current);
|
|
810
|
+
if (download.current) URL.revokeObjectURL(download.current);
|
|
811
|
+
};
|
|
812
|
+
}, []);
|
|
813
|
+
React.useEffect(function () {
|
|
814
|
+
var controller = new AbortController();
|
|
815
|
+
setError("");
|
|
816
|
+
readApi("/memory-engine/api/stats", controller.signal).then(function (result) {
|
|
817
|
+
if (!controller.signal.aborted) setStats(result);
|
|
818
|
+
}).catch(function (failure) {
|
|
819
|
+
if (!controller.signal.aborted) setError("读取设置失败:" + failure.message);
|
|
820
|
+
});
|
|
821
|
+
return function () { controller.abort(); };
|
|
822
|
+
}, [retry]);
|
|
823
|
+
function manage(clearing) {
|
|
824
|
+
if (mutation.current) return;
|
|
825
|
+
var controller = new AbortController();
|
|
826
|
+
mutation.current = controller; setBusy(true); setError(""); setNotice("");
|
|
827
|
+
var options = { signal: controller.signal };
|
|
828
|
+
if (clearing) { options.method = "POST"; options.headers = { "Content-Type": "application/json" }; options.body = JSON.stringify({ confirm: "DELETE" }); }
|
|
829
|
+
requestApi("/memory-engine/api/" + (clearing ? "clear" : "export"), options).then(function (result) {
|
|
830
|
+
if (controller.signal.aborted) return;
|
|
831
|
+
if (clearing) { props.onCleared(); return; }
|
|
832
|
+
if (download.current) URL.revokeObjectURL(download.current);
|
|
833
|
+
clearTimeout(timer.current);
|
|
834
|
+
download.current = URL.createObjectURL(new Blob([JSON.stringify({ ok: true, data: result }, null, 2) + "\n"], { type: "application/json;charset=utf-8" }));
|
|
835
|
+
var anchor = document.createElement("a");
|
|
836
|
+
var date = new Date();
|
|
837
|
+
anchor.href = download.current;
|
|
838
|
+
anchor.download = "memories-" + date.getFullYear() + "-" + String(date.getMonth() + 1).padStart(2, "0") + "-" + String(date.getDate()).padStart(2, "0") + ".json";
|
|
839
|
+
document.body.appendChild(anchor); anchor.click(); anchor.remove();
|
|
840
|
+
timer.current = setTimeout(function () { URL.revokeObjectURL(download.current); download.current = null; timer.current = null; }, 1000);
|
|
841
|
+
setNotice("已开始下载,共 " + result.total + " 条记忆。");
|
|
842
|
+
}).catch(function (failure) {
|
|
843
|
+
if (!controller.signal.aborted) setError((clearing ? "清空失败:" : "导出失败:") + failure.message);
|
|
844
|
+
}).finally(function () {
|
|
845
|
+
if (!controller.signal.aborted) { mutation.current = null; setBusy(false); }
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
var directory = stats ? stats.file.replace(/[\\/][^\\/]*$/, "") : "";
|
|
849
|
+
return React.createElement("section", { "aria-label": "记忆设置", "data-memory-engine-settings": "true",
|
|
850
|
+
style: { position: "absolute", inset: "76px 0 0", overflowY: "auto", padding: "0 24px 32px", color: "#dbeaf8", font: "14px/1.7 system-ui,sans-serif" } },
|
|
851
|
+
React.createElement("div", { style: { maxWidth: "900px", margin: "0 auto", padding: "28px", border: "1px solid #263f54", borderRadius: "14px", background: "#0b1624" } },
|
|
852
|
+
React.createElement("h2", { style: { margin: "0 0 20px", fontSize: "20px", color: "#b7e9ff" } }, "数据管理"),
|
|
853
|
+
stats ? React.createElement(React.Fragment, null,
|
|
854
|
+
React.createElement("div", { style: { display: "flex", alignItems: "center", flexWrap: "wrap", gap: "12px" } },
|
|
855
|
+
React.createElement("span", { style: { flex: "1 1 360px", overflowWrap: "anywhere" } }, "数据目录:", React.createElement("code", { "data-memory-engine-directory": "true" }, directory)),
|
|
856
|
+
React.createElement("button", { type: "button", "data-memory-engine-copy-path": "true", style: actionStyle,
|
|
857
|
+
onClick: function () {
|
|
858
|
+
setNotice(""); setError("");
|
|
859
|
+
copyText(directory).then(function () { if (active.current) setNotice("数据目录已复制 ✓"); }, function () { if (active.current) setError("复制路径失败,请手动复制上方目录。"); });
|
|
860
|
+
} }, "复制路径")),
|
|
861
|
+
React.createElement("p", { "data-memory-engine-storage-stats": "true", style: { color: "#9fb7ce", margin: "16px 0 24px" } }, "记忆条数:" + stats.total + " 文件大小:" + (stats.bytes / 1024).toFixed(1) + " KB"))
|
|
862
|
+
: React.createElement("p", { role: "status" }, error ? "暂时无法读取数据目录。" : "正在读取数据目录…"),
|
|
863
|
+
error ? React.createElement("div", { role: "alert", style: { color: "#ffb8b8", marginBottom: "16px" } }, error,
|
|
864
|
+
!stats ? React.createElement("button", { type: "button", style: actionStyle, onClick: function () { setRetry(function (value) { return value + 1; }); } }, "重试") : null) : null,
|
|
865
|
+
notice ? React.createElement("p", { role: "status", style: { color: "#9fe5c4" } }, notice) : null,
|
|
866
|
+
React.createElement("div", { style: { padding: "24px 0", borderTop: "1px solid #263f54", borderBottom: "1px solid #263f54" } },
|
|
867
|
+
React.createElement("button", { type: "button", "data-memory-engine-export": "true", disabled: busy, onClick: function () { manage(false); }, style: actionStyle }, "导出全部记忆(JSON)"),
|
|
868
|
+
React.createElement("p", { style: { margin: "10px 0 0", fontSize: "12px", color: "#9fb7ce" } }, "下载包含全部原始条目的 JSON 文件。导入已有记忆请使用 memory_import 工具。")),
|
|
869
|
+
React.createElement("h3", { style: { color: "#ffb8b8", marginTop: "24px" } }, "危险操作"),
|
|
870
|
+
React.createElement("p", { style: { color: "#9fb7ce" } }, "清空前会自动备份,最近五份操作备份保留在数据目录。"),
|
|
871
|
+
React.createElement("button", { type: "button", "data-memory-engine-clear": "true", disabled: busy,
|
|
872
|
+
onClick: function () { setError(""); setNotice(""); setConfirming(true); }, style: Object.assign({}, actionStyle, { color: "#ffb8b8", borderColor: "#8b505c", background: "#341c28" }) }, "清空全部记忆")),
|
|
873
|
+
confirming ? React.createElement(ConfirmationDialog, { title: "清空全部记忆", message: "这会删除当前记忆库的全部条目。请确认已导出需要保留的内容。", requireDelete: true,
|
|
874
|
+
submitLabel: "确认清空", busy: busy, error: error, onClose: function () { setConfirming(false); }, onConfirm: function () { manage(true); } }) : null);
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
function MemoryButton(props) {
|
|
878
|
+
var settings = props.settings;
|
|
18
879
|
var state = React.useState(false);
|
|
19
880
|
var open = state[0];
|
|
20
881
|
var setOpen = state[1];
|
|
21
|
-
var
|
|
882
|
+
var guideState = React.useState(false);
|
|
883
|
+
var guideOpen = guideState[0];
|
|
884
|
+
var setGuideOpen = guideState[1];
|
|
885
|
+
var dataState = React.useState(null);
|
|
886
|
+
var guideData = dataState[0];
|
|
887
|
+
var setGuideData = dataState[1];
|
|
888
|
+
var errorState = React.useState("");
|
|
889
|
+
var guideError = errorState[0];
|
|
890
|
+
var setGuideError = errorState[1];
|
|
891
|
+
var retryState = React.useState(0);
|
|
892
|
+
var retry = retryState[0];
|
|
893
|
+
var setRetry = retryState[1];
|
|
894
|
+
var selectedState = React.useState(null);
|
|
895
|
+
var selected = selectedState[0];
|
|
896
|
+
var setSelected = selectedState[1];
|
|
897
|
+
var tabState = React.useState("map");
|
|
898
|
+
var tab = tabState[0], setTab = tabState[1];
|
|
899
|
+
var renderer = React.useRef(null);
|
|
900
|
+
var selectionVersion = React.useRef(0);
|
|
901
|
+
var pointer = React.useRef(null);
|
|
902
|
+
var help = React.useRef(null);
|
|
903
|
+
var dismissed = React.useRef(false);
|
|
904
|
+
function closeGuide() {
|
|
905
|
+
dismissed.current = true;
|
|
906
|
+
setGuideOpen(false);
|
|
907
|
+
if (help.current) help.current.focus();
|
|
908
|
+
}
|
|
909
|
+
function openMap() {
|
|
910
|
+
dismissed.current = false;
|
|
911
|
+
setGuideOpen(false);
|
|
912
|
+
setSelected(null);
|
|
913
|
+
setTab("map");
|
|
914
|
+
setOpen(Date.now());
|
|
915
|
+
}
|
|
916
|
+
function closeMap() { setSelected(null); setOpen(false); }
|
|
917
|
+
function startPointer(event) {
|
|
918
|
+
if (event.target.closest("[data-memory-engine-detail], [data-memory-engine-guide-layer], [data-memory-engine-controls]")) { pointer.current = null; return; }
|
|
919
|
+
pointer.current = { x: event.clientX, y: event.clientY, moved: 0, version: selectionVersion.current };
|
|
920
|
+
}
|
|
921
|
+
function movePointer(event) {
|
|
922
|
+
if (!pointer.current) return;
|
|
923
|
+
pointer.current.moved += Math.abs(event.clientX - pointer.current.x) + Math.abs(event.clientY - pointer.current.y);
|
|
924
|
+
pointer.current.x = event.clientX; pointer.current.y = event.clientY;
|
|
925
|
+
}
|
|
926
|
+
function dismissOutside() {
|
|
927
|
+
var gesture = pointer.current;
|
|
928
|
+
pointer.current = null;
|
|
929
|
+
if (gesture && gesture.moved < 6 && gesture.version === selectionVersion.current) setSelected(null);
|
|
930
|
+
}
|
|
931
|
+
React.useEffect(function () {
|
|
932
|
+
if (!open) return undefined;
|
|
933
|
+
var controller = new AbortController();
|
|
934
|
+
var active = true;
|
|
935
|
+
setGuideData(null);
|
|
936
|
+
setGuideError("");
|
|
937
|
+
readApi("/memory-engine/api/onboard", controller.signal).then(function (data) {
|
|
938
|
+
if (typeof data.empty !== "boolean" || typeof data.prompt !== "string" || typeof data.dataDir !== "string"
|
|
939
|
+
|| !Array.isArray(data.steps) || data.steps.length !== 3
|
|
940
|
+
|| data.steps.some(function (step) { return !step || typeof step.title !== "string" || typeof step.body !== "string"; })) throw new Error("引导数据格式无效");
|
|
941
|
+
if (!active) return;
|
|
942
|
+
setGuideData(data);
|
|
943
|
+
if (data.empty && settings.showGuideOnEmpty && !dismissed.current) setGuideOpen(true);
|
|
944
|
+
}).catch(function (error) {
|
|
945
|
+
if (active) setGuideError(error.message);
|
|
946
|
+
});
|
|
947
|
+
return function () { active = false; controller.abort(); };
|
|
948
|
+
}, [open, retry, settings.showGuideOnEmpty]);
|
|
22
949
|
React.useEffect(function () {
|
|
23
950
|
if (!open) return undefined;
|
|
24
|
-
function
|
|
25
|
-
function
|
|
26
|
-
if (event.
|
|
951
|
+
function onEscape() { if (guideOpen) closeGuide(); else if (selected) setSelected(null); else closeMap(); }
|
|
952
|
+
function onKey(event) {
|
|
953
|
+
if (event.key !== "Escape") return;
|
|
954
|
+
if (event.target && event.target.closest && event.target.closest("[data-memory-engine-confirm]")) return;
|
|
955
|
+
event.preventDefault();
|
|
956
|
+
event.stopPropagation();
|
|
957
|
+
if (!event.repeat) onEscape();
|
|
27
958
|
}
|
|
28
|
-
window.addEventListener("keydown", onKey);
|
|
29
|
-
window.
|
|
30
|
-
|
|
31
|
-
}, [open]);
|
|
959
|
+
window.addEventListener("keydown", onKey, true);
|
|
960
|
+
return function () { window.removeEventListener("keydown", onKey, true); };
|
|
961
|
+
}, [open, guideOpen, selected]);
|
|
32
962
|
var button = React.createElement("button", {
|
|
33
963
|
type: "button",
|
|
34
|
-
onClick:
|
|
964
|
+
onClick: openMap,
|
|
35
965
|
"data-memory-engine-map": "true",
|
|
36
966
|
"aria-label": "打开记忆图谱",
|
|
37
967
|
title: "记忆图谱",
|
|
@@ -42,24 +972,41 @@ window.__ModuleLoader__.load({
|
|
|
42
972
|
role: "dialog",
|
|
43
973
|
"aria-modal": "true",
|
|
44
974
|
"aria-label": "记忆图谱",
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
975
|
+
onPointerDownCapture: startPointer, onPointerMoveCapture: movePointer,
|
|
976
|
+
onPointerCancelCapture: function () { pointer.current = null; }, onClick: dismissOutside,
|
|
977
|
+
style: { position: "fixed", inset: 0, maxWidth: "none", maxHeight: "none", zIndex: 9999, background: "#03060c" },
|
|
978
|
+
}, tab === "map" ? React.createElement(MemoryCanvas, { key: open, rendererRef: renderer, selectionVersion: selectionVersion, onSelect: setSelected })
|
|
979
|
+
: tab === "memories" ? React.createElement(MemoryList, { onChanged: function () { setRetry(function (value) { return value + 1; }); } })
|
|
980
|
+
: React.createElement(MemorySettings, { onCleared: function () { openMap(); setGuideOpen(true); } }),
|
|
981
|
+
tab !== "map" ? React.createElement("strong", { style: { position: "absolute", top: "23px", left: "24px", font: "600 18px system-ui,sans-serif", color: "#b7e9ff" } }, "记忆管理") : null,
|
|
982
|
+
React.createElement("style", null, "@keyframes dsh-memory-detail-in{from{opacity:0;transform:translateX(24px)}to{opacity:1;transform:translateX(0)}}@media(prefers-reduced-motion:reduce){[data-memory-engine-detail]{animation:none!important}}"),
|
|
983
|
+
React.createElement("div", {
|
|
984
|
+
"data-memory-engine-controls": "true",
|
|
985
|
+
style: { position: "absolute", top: "16px", right: "18px", zIndex: 1, display: "flex", gap: "8px" },
|
|
986
|
+
}, React.createElement("div", { role: "tablist", "aria-label": "记忆面板", style: { display: "flex", gap: "4px", marginRight: "8px" } },
|
|
987
|
+
[["map", "图谱"], ["memories", "记忆"], ["settings", "设置"]].map(function (entry) {
|
|
988
|
+
return React.createElement("button", { key: entry[0], type: "button", role: "tab", "aria-selected": tab === entry[0], "data-memory-engine-tab": entry[0],
|
|
989
|
+
onClick: function () { setSelected(null); setTab(entry[0]); },
|
|
990
|
+
style: Object.assign({}, actionStyle, { background: tab === entry[0] ? "#204762" : "#091422", borderColor: tab === entry[0] ? "#8acded" : "#35536b" }) }, entry[1]);
|
|
991
|
+
})), React.createElement("button", {
|
|
992
|
+
ref: help, type: "button", onClick: function () { setGuideOpen(true); },
|
|
993
|
+
"aria-label": "查看使用指南", title: "使用指南", style: actionStyle,
|
|
994
|
+
}, "?"), React.createElement("button", {
|
|
54
995
|
type: "button",
|
|
55
|
-
onClick:
|
|
996
|
+
onClick: closeMap,
|
|
56
997
|
"aria-label": "关闭记忆图谱",
|
|
57
|
-
style:
|
|
58
|
-
}, "关闭 ×"))
|
|
998
|
+
style: actionStyle,
|
|
999
|
+
}, "关闭 ×")), selected ? React.createElement(MemoryDetails, {
|
|
1000
|
+
node: selected, onClose: function () { setSelected(null); },
|
|
1001
|
+
onHighlight: function () { if (renderer.current) renderer.current.fireNeuron(selected.id); },
|
|
1002
|
+
}) : null, guideOpen ? React.createElement(GuideCard, {
|
|
1003
|
+
data: guideData, error: guideError, onClose: closeGuide, onRefresh: openMap,
|
|
1004
|
+
onRetry: function () { setRetry(function (value) { return value + 1; }); },
|
|
1005
|
+
}) : null);
|
|
59
1006
|
return React.createElement(React.Fragment, null, button, ReactDOM.createPortal(overlay, document.body));
|
|
60
1007
|
}
|
|
61
1008
|
|
|
62
|
-
function registerOn(slots, name) {
|
|
1009
|
+
function registerOn(slots, name, settings) {
|
|
63
1010
|
var disposable = null;
|
|
64
1011
|
slots.inject(name, function () {
|
|
65
1012
|
try {
|
|
@@ -68,7 +1015,7 @@ window.__ModuleLoader__.load({
|
|
|
68
1015
|
id: "memory-engine-map",
|
|
69
1016
|
order: 50,
|
|
70
1017
|
label: function () { return "记忆图谱"; },
|
|
71
|
-
}, MemoryButton);
|
|
1018
|
+
}, function ConfiguredMemoryButton() { return React.createElement(MemoryButton, { settings: settings }); });
|
|
72
1019
|
} catch (error) {
|
|
73
1020
|
try { console.error("[dsh-memory-engine] 注册到 " + name + " 失败:", error); } catch (ignored) {}
|
|
74
1021
|
}
|
|
@@ -87,7 +1034,15 @@ window.__ModuleLoader__.load({
|
|
|
87
1034
|
var target = ctx.slots && typeof ctx.slots.has === "function" && !ctx.slots.has(SLOT_FALLBACKS[0])
|
|
88
1035
|
? SLOT_FALLBACKS[1]
|
|
89
1036
|
: SLOT_NAME;
|
|
90
|
-
|
|
1037
|
+
function mount(settings) {
|
|
1038
|
+
if (!settings.hideButton) registerOn(ctx.slots, target, settings);
|
|
1039
|
+
}
|
|
1040
|
+
function fallback(error) {
|
|
1041
|
+
try { console.warn("[dsh-memory-engine] 配置读取失败,按默认显示按钮:", error); } catch (ignored) {}
|
|
1042
|
+
mount(defaultSettings);
|
|
1043
|
+
}
|
|
1044
|
+
try { loadSettings().then(mount, fallback); }
|
|
1045
|
+
catch (error) { fallback(error); }
|
|
91
1046
|
} catch (error) {
|
|
92
1047
|
try { console.error("[dsh-memory-engine] 插槽注册失败:", error); } catch (ignored) {}
|
|
93
1048
|
}
|