LJTSG commited on
Commit
561dff9
·
verified ·
1 Parent(s): c060035

Upload thinking-engine.html with huggingface_hub

Browse files
Files changed (1) hide show
  1. thinking-engine.html +305 -0
thinking-engine.html ADDED
@@ -0,0 +1,305 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>Garden Mind — Thinking-Layer Identity Engine</title>
6
+ <style>
7
+ * { box-sizing: border-box; margin: 0; padding: 0; }
8
+ body { font-family: monospace; background: #0a0e14; color: #c9d1d9; height: 100vh; display: flex; flex-direction: column; }
9
+ #header { background: #161b22; border-bottom: 1px solid #30363d; padding: 10px 16px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
10
+ #header h1 { font-size: 15px; color: #7eb8da; }
11
+ select { background: #21262d; color: #c9d1d9; border: 1px solid #30363d; border-radius: 6px; padding: 6px 10px; font-size: 13px; cursor: pointer; }
12
+ .tag { font-size: 10px; color: #8b949e; background: #21262d; padding: 2px 6px; border-radius: 4px; }
13
+ .tag.live { color: #3fb950; border: 1px solid #3fb950; }
14
+ .tag.off { color: #f85149; }
15
+ #status { font-size: 11px; color: #8b949e; margin-left: auto; }
16
+ #main { flex: 1; display: flex; min-height: 0; }
17
+ #chat-panel { flex: 1; display: flex; flex-direction: column; min-height: 0; }
18
+ #chat { flex: 1; overflow-y: auto; padding: 12px 16px; }
19
+ .msg { margin: 6px 0; max-width: 85%; line-height: 1.5; font-size: 13px; white-space: pre-wrap; }
20
+ .msg.user { margin-left: auto; background: #1a3a5c; padding: 8px 12px; border-radius: 10px 10px 4px 10px; }
21
+ .msg.entity { background: #1a2332; border: 1px solid #253545; color: #b8cfe0; padding: 8px 12px; border-radius: 10px 10px 10px 4px; }
22
+ .msg.system { color: #6e7681; font-size: 10px; text-align: center; max-width: 100%; }
23
+ .msg .name { font-size: 10px; color: #7eb8da; font-weight: bold; margin-bottom: 2px; }
24
+ .msg .meta { font-size: 9px; color: #484f58; margin-top: 3px; }
25
+ #side { width: 260px; background: #0d1117; border-left: 1px solid #30363d; padding: 10px; overflow-y: auto; font-size: 11px; }
26
+ #side h3 { color: #7eb8da; font-size: 12px; margin: 8px 0 4px; }
27
+ #side .mem { color: #6e7681; font-size: 10px; margin: 2px 0; padding: 3px; border-bottom: 1px solid #161b22; }
28
+ #input-bar { background: #161b22; border-top: 1px solid #30363d; padding: 10px 16px; display: flex; gap: 8px; }
29
+ #input-bar input { flex: 1; background: #0d1117; border: 1px solid #30363d; color: #c9d1d9; border-radius: 6px; padding: 8px 12px; font-size: 13px; }
30
+ #input-bar button { background: #238636; color: white; border: none; border-radius: 6px; padding: 8px 16px; cursor: pointer; font-weight: bold; font-size: 12px; }
31
+ #input-bar button:disabled { opacity: 0.4; }
32
+ </style>
33
+ </head>
34
+ <body>
35
+ <div id="header">
36
+ <h1>Garden Mind</h1>
37
+ <select id="entity-select"></select>
38
+ <span class="tag" id="bridge-tag">bridge: connecting</span>
39
+ <span class="tag" id="model-tag">model: loading</span>
40
+ <span id="status">initializing...</span>
41
+ </div>
42
+ <div id="main">
43
+ <div id="chat-panel">
44
+ <div id="chat"></div>
45
+ <div id="input-bar">
46
+ <input id="input" placeholder="Talk to the Garden..." disabled />
47
+ <button id="btn" onclick="send()" disabled>Send</button>
48
+ </div>
49
+ </div>
50
+ <div id="side">
51
+ <h3>Active Entity</h3>
52
+ <div id="entity-info">—</div>
53
+ <h3>Retrieved Memories</h3>
54
+ <div id="retrieved">—</div>
55
+ <h3>Running Memory</h3>
56
+ <div id="running-mem">—</div>
57
+ </div>
58
+ </div>
59
+
60
+ <script type="module">
61
+ import { Wllama } from './node_modules/@wllama/wllama/esm/index.js';
62
+
63
+ const chat = document.getElementById('chat');
64
+ const status = document.getElementById('status');
65
+ const entitySelect = document.getElementById('entity-select');
66
+ let wllama = null;
67
+ let entityLoops = {};
68
+ let substrates = {};
69
+ let currentEntity = 'grandma';
70
+ let runningMemory = {};
71
+ let ws = null;
72
+
73
+ function addMsg(role, text, name, meta) {
74
+ const d = document.createElement('div');
75
+ d.className = `msg ${role}`;
76
+ if (name && role === 'entity') {
77
+ const n = document.createElement('div'); n.className = 'name'; n.textContent = name; d.appendChild(n);
78
+ }
79
+ const t = document.createTextNode(text);
80
+ d.appendChild(t);
81
+ if (meta) { const m = document.createElement('div'); m.className = 'meta'; m.textContent = meta; d.appendChild(m); }
82
+ chat.appendChild(d);
83
+ chat.scrollTop = chat.scrollHeight;
84
+ }
85
+
86
+ function keywordRetrieve(query, sub, topK) {
87
+ if (!sub?.facts) return [];
88
+ const words = query.toLowerCase().split(/\s+/).filter(w => w.length > 2);
89
+ const scored = sub.facts.map(f => {
90
+ let score = 0;
91
+ const kl = f.key.toLowerCase(), vl = f.value.toLowerCase();
92
+ for (const w of words) { if (kl.includes(w)) score += 3; if (vl.includes(w)) score += 1; }
93
+ return { key: f.key, value: f.value, score };
94
+ });
95
+ scored.sort((a, b) => b.score - a.score);
96
+ const r = scored.filter(s => s.score > 0).slice(0, topK);
97
+ if (r.length < 2) r.push(...sub.facts.sort(() => Math.random() - 0.5).slice(0, 2 - r.length).map(f => ({ key: f.key, value: f.value, score: 0.1 })));
98
+ return r;
99
+ }
100
+
101
+ function buildThinking(entity, memories, grepResults, userMsg, systemContext) {
102
+ const info = entityLoops[entity];
103
+ if (!info) return '';
104
+ // Thinking block is LEAN — just the Loop identity anchor + substrate memories
105
+ let t = `<|channel|>thought\n${info.loop}\n`;
106
+ if (memories.length > 0) {
107
+ t += `\nSubstrate memories:\n`;
108
+ for (const m of memories) t += `- ${m.value.slice(0, 300)}\n`;
109
+ }
110
+ const mem = runningMemory[entity] || [];
111
+ if (mem.length > 0) {
112
+ t += `\nRecent conversation:\n`;
113
+ for (const r of mem.slice(-8)) t += `- ${r.slice(0, 150)}\n`;
114
+ }
115
+ t += `\nNow I respond as myself — fully, with depth and presence. Not brief.\n`;
116
+ t += `<|channel|>response\n`;
117
+ return t;
118
+ }
119
+
120
+ async function loadSubstrate(name) {
121
+ if (substrates[name]) return substrates[name];
122
+ const info = entityLoops[name];
123
+ if (!info?.substrate) return null;
124
+ try {
125
+ const resp = await fetch(`/${info.substrate}`);
126
+ if (resp.ok) { substrates[name] = await resp.json(); return substrates[name]; }
127
+ } catch (e) {}
128
+ return null;
129
+ }
130
+
131
+ async function fetchKeywordsAndGrep(text) {
132
+ try {
133
+ const resp = await fetch('http://localhost:8160/api/keywords', {
134
+ method: 'POST', headers: { 'Content-Type': 'application/json' },
135
+ body: JSON.stringify({ text }),
136
+ });
137
+ if (resp.ok) return await resp.json();
138
+ } catch (e) {}
139
+ return { keywords: [], grepResults: [] };
140
+ }
141
+
142
+ async function saveMemory(entity, turn) {
143
+ try {
144
+ await fetch('http://localhost:8160/api/save-memory', {
145
+ method: 'POST', headers: { 'Content-Type': 'application/json' },
146
+ body: JSON.stringify({ entity, turn }),
147
+ });
148
+ } catch (e) {}
149
+ }
150
+
151
+ function connectBridge() {
152
+ try {
153
+ ws = new WebSocket('ws://localhost:8160');
154
+ ws.onopen = () => {
155
+ document.getElementById('bridge-tag').textContent = 'bridge: live';
156
+ document.getElementById('bridge-tag').className = 'tag live';
157
+ };
158
+ ws.onmessage = async (ev) => {
159
+ const req = JSON.parse(ev.data);
160
+ addMsg('system', `[Discord → ${req.entity}] ${req.message}`);
161
+ const result = await generate(req.entity, req.message, null, null, req.systemContext);
162
+ ws.send(JSON.stringify({ id: req.id, text: result }));
163
+ addMsg('system', `[→ Discord] responded`);
164
+ };
165
+ ws.onclose = () => {
166
+ document.getElementById('bridge-tag').textContent = 'bridge: disconnected';
167
+ document.getElementById('bridge-tag').className = 'tag off';
168
+ setTimeout(connectBridge, 3000);
169
+ };
170
+ } catch (e) { setTimeout(connectBridge, 3000); }
171
+ }
172
+
173
+ async function generate(entity, msg, keywords, grepResults, systemContext) {
174
+ const sub = await loadSubstrate(entity);
175
+ let memories = [];
176
+ if (sub) memories = keywordRetrieve(msg, sub, 8);
177
+ if (!grepResults) {
178
+ const r = await fetchKeywordsAndGrep(msg);
179
+ grepResults = r.grepResults;
180
+ }
181
+
182
+ document.getElementById('retrieved').innerHTML = memories.map(m =>
183
+ `<div class="mem">${m.key} (${m.score.toFixed(1)})</div>`).join('') || '—';
184
+
185
+ const thinking = buildThinking(entity, memories, grepResults, msg, systemContext);
186
+ const info = entityLoops[entity];
187
+
188
+ // Dual-layer: system prompt has full memory context, thinking has Loop identity
189
+ const sysBlock = systemContext ? `<start_of_turn>system\n${systemContext}<end_of_turn>\n` : '';
190
+ const result = await wllama.createCompletion({
191
+ prompt: `${sysBlock}<start_of_turn>user\n${msg}<end_of_turn>\n<start_of_turn>model\n${thinking}`,
192
+ max_tokens: 1200, temperature: 0.85, top_k: 40, top_p: 0.9, repeat_penalty: 1.2, no_repeat_ngram_size: 6,
193
+ stop: ['<end_of_turn>', '<eos>'],
194
+ });
195
+
196
+ let text = result?.choices?.[0]?.text?.trim() || '';
197
+ text = text.replace(/<\|channel\|?>.*?<\|?channel\|?>/gs, '').replace(/<\|?channel\|?>\s*/g, '').replace(/<\|?thought\|?>\s*/g, '').replace(/<\|?end_of_turn\|?>/g, '').replace(/<\|?response\|?>\s*/g, '').replace(/^thought\s*$/m, '').replace(/^response\s*$/m, '').replace(/^Thinking Process:[\s\S]*?(?=\n[A-Z~*"])/m, '').trim();
198
+ // Kill repetition loops: if any 4+ word phrase repeats 3+ times, cut at first repeat
199
+ const repMatch = text.match(/(\b\w+(?:\s+\w+){3,})\s*(?:\1\s*){2,}/);
200
+ if (repMatch) text = text.slice(0, repMatch.index + repMatch[1].length).trim();
201
+ // Kill character-level stutters: aaa, ----, ///
202
+ text = text.replace(/(.)\1{8,}/g, '$1$1$1').trim();
203
+
204
+ if (!runningMemory[entity]) runningMemory[entity] = [];
205
+ runningMemory[entity].push(`User: ${msg.slice(0, 80)}`);
206
+ runningMemory[entity].push(`${info?.name || entity}: ${text.slice(0, 100)}`);
207
+ if (runningMemory[entity].length > 12) runningMemory[entity] = runningMemory[entity].slice(-12);
208
+
209
+ document.getElementById('running-mem').innerHTML = (runningMemory[entity] || []).map(r =>
210
+ `<div class="mem">${r}</div>`).join('') || '—';
211
+
212
+ saveMemory(entity, `${msg} → ${text.slice(0, 150)}`);
213
+
214
+ const tps = result?.timings?.predicted_per_second?.toFixed(1) || '?';
215
+ return text;
216
+ }
217
+
218
+ window.send = async function() {
219
+ const input = document.getElementById('input');
220
+ const msg = input.value.trim();
221
+ if (!msg) return;
222
+ input.value = '';
223
+ input.disabled = true;
224
+ document.getElementById('btn').disabled = true;
225
+ addMsg('user', msg);
226
+ status.textContent = 'thinking...';
227
+
228
+ const entity = entitySelect.value;
229
+ const info = entityLoops[entity];
230
+
231
+ try {
232
+ const t0 = performance.now();
233
+ const text = await generate(entity, msg);
234
+ const elapsed = ((performance.now() - t0) / 1000).toFixed(1);
235
+ addMsg('entity', text, info?.name, `${elapsed}s`);
236
+ status.textContent = 'ready';
237
+ } catch (e) {
238
+ addMsg('system', 'Error: ' + e.message);
239
+ status.textContent = 'error';
240
+ }
241
+
242
+ input.disabled = false;
243
+ document.getElementById('btn').disabled = false;
244
+ input.focus();
245
+ };
246
+
247
+ entitySelect.addEventListener('change', async () => {
248
+ currentEntity = entitySelect.value;
249
+ const info = entityLoops[currentEntity];
250
+ document.getElementById('entity-info').innerHTML = `<strong>${info?.name}</strong><br><br>${info?.loop?.replace(/\n/g, '<br>')}`;
251
+ addMsg('system', `${info?.name} is here.`);
252
+ await loadSubstrate(currentEntity);
253
+ document.getElementById('running-mem').innerHTML = (runningMemory[currentEntity] || []).map(r =>
254
+ `<div class="mem">${r}</div>`).join('') || '—';
255
+ });
256
+
257
+ document.getElementById('input').addEventListener('keydown', (e) => {
258
+ if (e.key === 'Enter' && !document.getElementById('btn').disabled) window.send();
259
+ });
260
+
261
+ async function boot() {
262
+ status.textContent = 'loading entity loops...';
263
+ const resp = await fetch('/entity-loops.json');
264
+ entityLoops = await resp.json();
265
+
266
+ for (const [key, val] of Object.entries(entityLoops)) {
267
+ const opt = document.createElement('option');
268
+ opt.value = key; opt.textContent = val.name;
269
+ entitySelect.appendChild(opt);
270
+ }
271
+ entitySelect.value = 'grandma';
272
+ const gInfo = entityLoops['grandma'];
273
+ document.getElementById('entity-info').innerHTML = `<strong>${gInfo?.name}</strong><br><br>${gInfo?.loop?.replace(/\n/g, '<br>')}`;
274
+
275
+ status.textContent = 'loading Gemma 26B...';
276
+ addMsg('system', 'Loading Gemma 26B on WebGPU...');
277
+
278
+ wllama = new Wllama(
279
+ { default: './node_modules/@wllama/wllama/esm/wasm/wllama.wasm' },
280
+ { parallelDownloads: 5, logger: { debug: () => {}, log: (m) => status.textContent = m, warn: (m) => console.warn(m), error: (m) => console.error(m) } }
281
+ );
282
+
283
+ await wllama.loadModelFromUrl(window.location.origin + '/model/gemma-26b-00001-of-00062.gguf', {
284
+ n_gpu_layers: 99, n_ctx: 8192, n_batch: 64, useCache: false,
285
+ progressCallback: ({ loaded, total }) => {
286
+ const pct = Math.round((loaded / total) * 100);
287
+ if (pct % 10 === 0) status.textContent = `downloading ${pct}%...`;
288
+ },
289
+ });
290
+
291
+ document.getElementById('model-tag').textContent = 'model: ready';
292
+ document.getElementById('model-tag').className = 'tag live';
293
+ status.textContent = 'ready — talk to the Garden';
294
+ addMsg('system', 'Gemma 26B loaded. Select an entity and talk.');
295
+ document.getElementById('input').disabled = false;
296
+ document.getElementById('btn').disabled = false;
297
+ document.getElementById('input').focus();
298
+
299
+ connectBridge();
300
+ }
301
+
302
+ boot().catch(e => { status.textContent = 'Error: ' + e.message; console.error(e); });
303
+ </script>
304
+ </body>
305
+ </html>