@199-bio/engram 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,362 @@
1
+ /**
2
+ * Engram Web Interface
3
+ * Vanilla JavaScript - no build step required
4
+ */
5
+
6
+ const API_BASE = '';
7
+
8
+ // State
9
+ let currentView = 'memories';
10
+ let editingMemoryId = null;
11
+
12
+ // DOM Elements
13
+ const views = {
14
+ memories: document.getElementById('memories-view'),
15
+ entities: document.getElementById('entities-view'),
16
+ graph: document.getElementById('graph-view'),
17
+ };
18
+
19
+ const statsEl = document.getElementById('stats');
20
+ const memoriesList = document.getElementById('memories-list');
21
+ const entitiesList = document.getElementById('entities-list');
22
+ const graphContainer = document.getElementById('graph-container');
23
+ const searchInput = document.getElementById('search-input');
24
+ const entityTypeFilter = document.getElementById('entity-type-filter');
25
+
26
+ // Modal elements
27
+ const modal = document.getElementById('modal');
28
+ const modalTitle = document.getElementById('modal-title');
29
+ const modalForm = document.getElementById('modal-form');
30
+ const modalContentInput = document.getElementById('modal-content-input');
31
+ const modalSource = document.getElementById('modal-source');
32
+ const modalImportance = document.getElementById('modal-importance');
33
+ const importanceValue = document.getElementById('importance-value');
34
+
35
+ const entityModal = document.getElementById('entity-modal');
36
+ const entityModalTitle = document.getElementById('entity-modal-title');
37
+ const entityModalBody = document.getElementById('entity-modal-body');
38
+
39
+ // API helpers
40
+ async function api(path, options = {}) {
41
+ const res = await fetch(`${API_BASE}${path}`, {
42
+ headers: { 'Content-Type': 'application/json' },
43
+ ...options,
44
+ body: options.body ? JSON.stringify(options.body) : undefined,
45
+ });
46
+ return res.json();
47
+ }
48
+
49
+ // Format date
50
+ function formatDate(dateStr) {
51
+ const date = new Date(dateStr);
52
+ return date.toLocaleDateString('en-GB', {
53
+ day: 'numeric',
54
+ month: 'short',
55
+ year: 'numeric',
56
+ hour: '2-digit',
57
+ minute: '2-digit',
58
+ });
59
+ }
60
+
61
+ // Load stats
62
+ async function loadStats() {
63
+ const stats = await api('/api/stats');
64
+ statsEl.textContent = `${stats.memories} memories \u00b7 ${stats.entities} entities \u00b7 ${stats.relations} relations`;
65
+ }
66
+
67
+ // Load memories
68
+ async function loadMemories(query = '') {
69
+ const path = query ? `/api/memories?q=${encodeURIComponent(query)}` : '/api/memories';
70
+ const data = await api(path);
71
+
72
+ if (data.memories.length === 0) {
73
+ memoriesList.innerHTML = '<div class="empty-state">No memories found</div>';
74
+ return;
75
+ }
76
+
77
+ memoriesList.innerHTML = data.memories.map(m => `
78
+ <div class="list-item memory-item" data-id="${m.id}">
79
+ <div class="content">${escapeHtml(m.content)}</div>
80
+ <div class="meta">
81
+ <span>${formatDate(m.timestamp)}</span>
82
+ <span>${m.source}</span>
83
+ <span>importance: ${m.importance}</span>
84
+ ${m.score ? `<span class="score">${m.score.toFixed(4)}</span>` : ''}
85
+ </div>
86
+ <div class="actions">
87
+ <button class="edit-btn" data-id="${m.id}">Edit</button>
88
+ <button class="delete-btn" data-id="${m.id}">Delete</button>
89
+ </div>
90
+ </div>
91
+ `).join('');
92
+
93
+ // Attach event listeners
94
+ memoriesList.querySelectorAll('.edit-btn').forEach(btn => {
95
+ btn.addEventListener('click', (e) => {
96
+ e.stopPropagation();
97
+ editMemory(btn.dataset.id);
98
+ });
99
+ });
100
+
101
+ memoriesList.querySelectorAll('.delete-btn').forEach(btn => {
102
+ btn.addEventListener('click', (e) => {
103
+ e.stopPropagation();
104
+ deleteMemory(btn.dataset.id);
105
+ });
106
+ });
107
+ }
108
+
109
+ // Load entities
110
+ async function loadEntities(type = '') {
111
+ const path = type ? `/api/entities?type=${type}` : '/api/entities';
112
+ const data = await api(path);
113
+
114
+ if (data.entities.length === 0) {
115
+ entitiesList.innerHTML = '<div class="empty-state">No entities found</div>';
116
+ return;
117
+ }
118
+
119
+ entitiesList.innerHTML = data.entities.map(e => `
120
+ <div class="list-item entity-item" data-name="${escapeHtml(e.name)}">
121
+ <div class="name">${escapeHtml(e.name)}</div>
122
+ <div class="type">${e.type}</div>
123
+ </div>
124
+ `).join('');
125
+
126
+ // Attach event listeners
127
+ entitiesList.querySelectorAll('.entity-item').forEach(item => {
128
+ item.addEventListener('click', () => {
129
+ showEntityDetails(item.dataset.name);
130
+ });
131
+ });
132
+ }
133
+
134
+ // Show entity details
135
+ async function showEntityDetails(name) {
136
+ const data = await api(`/api/entities/${encodeURIComponent(name)}`);
137
+
138
+ entityModalTitle.textContent = data.name;
139
+
140
+ let html = `<p><strong>Type:</strong> ${data.type}</p>`;
141
+
142
+ if (data.observations && data.observations.length > 0) {
143
+ html += `<h3>Observations</h3><ul>`;
144
+ data.observations.forEach(o => {
145
+ html += `<li>${escapeHtml(o.content)}</li>`;
146
+ });
147
+ html += `</ul>`;
148
+ }
149
+
150
+ if (data.relationsFrom && data.relationsFrom.length > 0) {
151
+ html += `<h3>Relationships (outgoing)</h3><ul>`;
152
+ data.relationsFrom.forEach(r => {
153
+ html += `<li>${r.type} \u2192 ${escapeHtml(r.targetEntity?.name || r.to)}</li>`;
154
+ });
155
+ html += `</ul>`;
156
+ }
157
+
158
+ if (data.relationsTo && data.relationsTo.length > 0) {
159
+ html += `<h3>Relationships (incoming)</h3><ul>`;
160
+ data.relationsTo.forEach(r => {
161
+ html += `<li>${escapeHtml(r.sourceEntity?.name || r.from)} \u2192 ${r.type}</li>`;
162
+ });
163
+ html += `</ul>`;
164
+ }
165
+
166
+ entityModalBody.innerHTML = html;
167
+ entityModal.classList.remove('hidden');
168
+ }
169
+
170
+ // Load graph
171
+ async function loadGraph() {
172
+ const data = await api('/api/graph');
173
+
174
+ // Simple visualization using CSS
175
+ if (data.nodes.length === 0) {
176
+ graphContainer.innerHTML = '<div class="empty-state">No entities in graph</div>';
177
+ return;
178
+ }
179
+
180
+ // Create a simple text-based visualization
181
+ let html = '<div style="padding: 2rem; font-size: 0.875rem;">';
182
+ html += '<p style="margin-bottom: 1rem; color: var(--text-muted);">Knowledge graph visualization. Click entities to see details.</p>';
183
+
184
+ // Group by type
185
+ const byType = {};
186
+ data.nodes.forEach(n => {
187
+ if (!byType[n.type]) byType[n.type] = [];
188
+ byType[n.type].push(n);
189
+ });
190
+
191
+ for (const [type, nodes] of Object.entries(byType)) {
192
+ html += `<div style="margin-bottom: 1.5rem;">`;
193
+ html += `<h3 style="font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--text-muted); margin-bottom: 0.5rem;">${type}</h3>`;
194
+ html += `<div style="display: flex; flex-wrap: wrap; gap: 0.5rem;">`;
195
+ nodes.forEach(n => {
196
+ html += `<span class="graph-node" data-name="${escapeHtml(n.label)}" style="padding: 0.375rem 0.75rem; background: var(--bg-tertiary); cursor: pointer;">${escapeHtml(n.label)}</span>`;
197
+ });
198
+ html += `</div></div>`;
199
+ }
200
+
201
+ if (data.edges.length > 0) {
202
+ html += `<div style="margin-top: 2rem;">`;
203
+ html += `<h3 style="font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--text-muted); margin-bottom: 0.5rem;">Relationships</h3>`;
204
+ html += `<ul style="list-style: none;">`;
205
+ data.edges.forEach(e => {
206
+ const fromNode = data.nodes.find(n => n.id === e.from);
207
+ const toNode = data.nodes.find(n => n.id === e.to);
208
+ if (fromNode && toNode) {
209
+ html += `<li style="padding: 0.25rem 0; color: var(--text-secondary);">${escapeHtml(fromNode.label)} <span style="color: var(--accent);">\u2192 ${e.label} \u2192</span> ${escapeHtml(toNode.label)}</li>`;
210
+ }
211
+ });
212
+ html += `</ul></div>`;
213
+ }
214
+
215
+ html += '</div>';
216
+ graphContainer.innerHTML = html;
217
+
218
+ // Attach click handlers
219
+ graphContainer.querySelectorAll('.graph-node').forEach(node => {
220
+ node.addEventListener('click', () => {
221
+ showEntityDetails(node.dataset.name);
222
+ });
223
+ });
224
+ }
225
+
226
+ // Edit memory
227
+ async function editMemory(id) {
228
+ const data = await api('/api/memories');
229
+ const memory = data.memories.find(m => m.id === id);
230
+ if (!memory) return;
231
+
232
+ editingMemoryId = id;
233
+ modalTitle.textContent = 'Edit Memory';
234
+ modalContentInput.value = memory.content;
235
+ modalSource.value = memory.source;
236
+ modalImportance.value = memory.importance;
237
+ importanceValue.textContent = memory.importance;
238
+ modal.classList.remove('hidden');
239
+ }
240
+
241
+ // Delete memory
242
+ async function deleteMemory(id) {
243
+ if (!confirm('Delete this memory?')) return;
244
+
245
+ await api(`/api/memories/${id}`, { method: 'DELETE' });
246
+ await loadMemories(searchInput.value);
247
+ await loadStats();
248
+ }
249
+
250
+ // Save memory
251
+ async function saveMemory() {
252
+ const content = modalContentInput.value.trim();
253
+ if (!content) return;
254
+
255
+ const body = {
256
+ content,
257
+ source: modalSource.value || 'web',
258
+ importance: parseFloat(modalImportance.value),
259
+ };
260
+
261
+ if (editingMemoryId) {
262
+ await api(`/api/memories/${editingMemoryId}`, { method: 'PUT', body });
263
+ } else {
264
+ await api('/api/memories', { method: 'POST', body });
265
+ }
266
+
267
+ closeModal();
268
+ await loadMemories(searchInput.value);
269
+ await loadStats();
270
+ }
271
+
272
+ // Close modal
273
+ function closeModal() {
274
+ modal.classList.add('hidden');
275
+ editingMemoryId = null;
276
+ modalContentInput.value = '';
277
+ modalSource.value = 'web';
278
+ modalImportance.value = '0.5';
279
+ importanceValue.textContent = '0.5';
280
+ }
281
+
282
+ // Escape HTML
283
+ function escapeHtml(str) {
284
+ if (!str) return '';
285
+ return str
286
+ .replace(/&/g, '&amp;')
287
+ .replace(/</g, '&lt;')
288
+ .replace(/>/g, '&gt;')
289
+ .replace(/"/g, '&quot;')
290
+ .replace(/'/g, '&#039;');
291
+ }
292
+
293
+ // Switch view
294
+ function switchView(view) {
295
+ currentView = view;
296
+
297
+ // Update nav buttons
298
+ document.querySelectorAll('.nav-btn').forEach(btn => {
299
+ btn.classList.toggle('active', btn.dataset.view === view);
300
+ });
301
+
302
+ // Update views
303
+ Object.entries(views).forEach(([name, el]) => {
304
+ el.classList.toggle('active', name === view);
305
+ });
306
+
307
+ // Load data for view
308
+ if (view === 'memories') loadMemories(searchInput.value);
309
+ if (view === 'entities') loadEntities(entityTypeFilter.value);
310
+ if (view === 'graph') loadGraph();
311
+ }
312
+
313
+ // Event listeners
314
+ document.querySelectorAll('.nav-btn').forEach(btn => {
315
+ btn.addEventListener('click', () => switchView(btn.dataset.view));
316
+ });
317
+
318
+ document.getElementById('search-btn').addEventListener('click', () => {
319
+ loadMemories(searchInput.value);
320
+ });
321
+
322
+ searchInput.addEventListener('keypress', (e) => {
323
+ if (e.key === 'Enter') loadMemories(searchInput.value);
324
+ });
325
+
326
+ entityTypeFilter.addEventListener('change', () => {
327
+ loadEntities(entityTypeFilter.value);
328
+ });
329
+
330
+ document.getElementById('add-memory-btn').addEventListener('click', () => {
331
+ editingMemoryId = null;
332
+ modalTitle.textContent = 'Add Memory';
333
+ modal.classList.remove('hidden');
334
+ });
335
+
336
+ document.getElementById('modal-cancel').addEventListener('click', closeModal);
337
+
338
+ modalForm.addEventListener('submit', (e) => {
339
+ e.preventDefault();
340
+ saveMemory();
341
+ });
342
+
343
+ modalImportance.addEventListener('input', () => {
344
+ importanceValue.textContent = modalImportance.value;
345
+ });
346
+
347
+ document.getElementById('entity-modal-close').addEventListener('click', () => {
348
+ entityModal.classList.add('hidden');
349
+ });
350
+
351
+ // Close modals on backdrop click
352
+ modal.addEventListener('click', (e) => {
353
+ if (e.target === modal) closeModal();
354
+ });
355
+
356
+ entityModal.addEventListener('click', (e) => {
357
+ if (e.target === entityModal) entityModal.classList.add('hidden');
358
+ });
359
+
360
+ // Initialize
361
+ loadStats();
362
+ loadMemories();
@@ -0,0 +1,95 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Engram</title>
7
+ <link rel="stylesheet" href="style.css">
8
+ </head>
9
+ <body>
10
+ <header>
11
+ <h1>Engram</h1>
12
+ <nav>
13
+ <button class="nav-btn active" data-view="memories">Memories</button>
14
+ <button class="nav-btn" data-view="entities">Entities</button>
15
+ <button class="nav-btn" data-view="graph">Graph</button>
16
+ </nav>
17
+ <div class="stats" id="stats"></div>
18
+ </header>
19
+
20
+ <main>
21
+ <!-- Memories View -->
22
+ <section id="memories-view" class="view active">
23
+ <div class="search-bar">
24
+ <input type="text" id="search-input" placeholder="Search memories...">
25
+ <button id="search-btn">Search</button>
26
+ </div>
27
+
28
+ <div class="toolbar">
29
+ <button id="add-memory-btn">+ Add Memory</button>
30
+ </div>
31
+
32
+ <div id="memories-list" class="list"></div>
33
+ </section>
34
+
35
+ <!-- Entities View -->
36
+ <section id="entities-view" class="view">
37
+ <div class="filter-bar">
38
+ <select id="entity-type-filter">
39
+ <option value="">All Types</option>
40
+ <option value="person">Person</option>
41
+ <option value="organization">Organization</option>
42
+ <option value="place">Place</option>
43
+ <option value="concept">Concept</option>
44
+ <option value="event">Event</option>
45
+ </select>
46
+ </div>
47
+
48
+ <div id="entities-list" class="list"></div>
49
+ </section>
50
+
51
+ <!-- Graph View -->
52
+ <section id="graph-view" class="view">
53
+ <div id="graph-container"></div>
54
+ </section>
55
+ </main>
56
+
57
+ <!-- Modal for adding/editing -->
58
+ <div id="modal" class="modal hidden">
59
+ <div class="modal-content">
60
+ <h2 id="modal-title">Add Memory</h2>
61
+ <form id="modal-form">
62
+ <textarea id="modal-content-input" placeholder="Memory content..." rows="6"></textarea>
63
+ <div class="form-row">
64
+ <label>
65
+ Source:
66
+ <input type="text" id="modal-source" value="web">
67
+ </label>
68
+ <label>
69
+ Importance:
70
+ <input type="range" id="modal-importance" min="0" max="1" step="0.1" value="0.5">
71
+ <span id="importance-value">0.5</span>
72
+ </label>
73
+ </div>
74
+ <div class="modal-actions">
75
+ <button type="button" id="modal-cancel">Cancel</button>
76
+ <button type="submit">Save</button>
77
+ </div>
78
+ </form>
79
+ </div>
80
+ </div>
81
+
82
+ <!-- Entity Detail Modal -->
83
+ <div id="entity-modal" class="modal hidden">
84
+ <div class="modal-content">
85
+ <h2 id="entity-modal-title"></h2>
86
+ <div id="entity-modal-body"></div>
87
+ <div class="modal-actions">
88
+ <button type="button" id="entity-modal-close">Close</button>
89
+ </div>
90
+ </div>
91
+ </div>
92
+
93
+ <script src="app.js"></script>
94
+ </body>
95
+ </html>