@aaryanbindage/omnicontext 0.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aaryanbindage/omnicontext",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Persistent, cross-device memory for Claude: your profile, your projects, and resumable handoffs — shared by every Claude session (Claude Code plugin + MCP server + desktop app).",
5
5
  "type": "module",
6
6
  "bin": {
package/src/cli.js CHANGED
@@ -5,6 +5,7 @@ import { spawn } from 'node:child_process';
5
5
  import { Store, VERSION } from './store.js';
6
6
  import { buildContext, projectLine, handoffLine } from './context.js';
7
7
  import { resolveProjects } from './resolve.js';
8
+ import { buildGraph, neighborhood } from './graph.js';
8
9
  import { sync, pull, initSync, createGithubRemote, syncStatus } from './sync.js';
9
10
  import { installClaudeDesktop, installClaudeCode } from './install.js';
10
11
 
@@ -25,6 +26,8 @@ Usage:
25
26
  omnicontext project note <project> "<note>"
26
27
  omnicontext resolve "<reference>" e.g. omnicontext resolve "my game project"
27
28
  omnicontext handoffs [--all] List handoffs
29
+ omnicontext graph [--json] Knowledge graph: counts, hubs (or the full graph as JSON)
30
+ omnicontext related "<thing>" What's linked to a project, #tag, device or topic
28
31
 
29
32
  omnicontext sync Sync now
30
33
  omnicontext sync --github [NAME] Create a private GitHub repo (via gh) and sync to it
@@ -199,6 +202,30 @@ export async function main(argv = process.argv.slice(2)) {
199
202
  return;
200
203
  }
201
204
 
205
+ case 'graph': {
206
+ const g = buildGraph(store);
207
+ if (flags.json) return console.log(JSON.stringify(g, null, 2));
208
+ const by = {};
209
+ for (const n of g.nodes) by[n.type] = (by[n.type] || 0) + 1;
210
+ console.log(`${g.nodes.length} nodes, ${g.edges.length} links: ${Object.entries(by).map(([t, c]) => `${c} ${t}`).join(', ')}`);
211
+ console.log('Most connected:');
212
+ for (const n of g.nodes.filter((n) => n.id !== 'you').sort((a, b) => b.degree - a.degree).slice(0, 8)) console.log(` ${String(n.degree).padStart(3)} ${n.label} (${n.type})`);
213
+ return;
214
+ }
215
+
216
+ case 'related': {
217
+ const g = buildGraph(store);
218
+ const q = pos.join(' ').trim().toLowerCase();
219
+ let start = g.nodes.filter((n) => n.id !== 'you' && (n.label.toLowerCase() === q || n.id.endsWith(`:${q}`)));
220
+ if (!start.length) {
221
+ const p = resolveProjects(store.listProjects(), q, 1)[0]?.project;
222
+ if (p) start = g.nodes.filter((n) => n.id === `project:${p.slug}`);
223
+ }
224
+ if (!start.length) return console.log('No match.');
225
+ for (const n of neighborhood(g, start.map((n) => n.id))) console.log(` ${n.label} (${n.type})`);
226
+ return;
227
+ }
228
+
202
229
  case 'handoffs':
203
230
  for (const h of store.listHandoffs({ status: flags.all ? 'all' : 'open' })) console.log(handoffLine(h), `[${h.status}]`);
204
231
  return;
package/src/graph.js ADDED
@@ -0,0 +1,130 @@
1
+ // Knowledge graph over everything in memory, Obsidian-style: you, your projects, their tags,
2
+ // devices, handoffs, inbox items and profile facts, linked by structure and by mentions.
3
+ // A project named in any text (or written as [[Name]]) links to it; an unknown [[Topic]] becomes a topic node.
4
+
5
+ const esc = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
6
+
7
+ function mentionFinder(projects) {
8
+ const terms = [];
9
+ for (const p of projects) {
10
+ for (const t of [p.name, p.slug, ...(p.aliases || [])]) {
11
+ const term = String(t || '').trim();
12
+ if (term.length >= 3 && /[a-z]/i.test(term)) terms.push({ term: term.toLowerCase(), slug: p.slug });
13
+ }
14
+ }
15
+ // Longest first so "my game engine" wins over "game".
16
+ terms.sort((a, b) => b.term.length - a.term.length);
17
+ const re = terms.length ? new RegExp(`(?<![\\w-])(${terms.map((t) => esc(t.term)).join('|')})(?![\\w-])`, 'gi') : null;
18
+ const bySlug = new Map(terms.map((t) => [t.term, t.slug]));
19
+ return (text) => {
20
+ const found = new Set();
21
+ const topics = new Set();
22
+ const s = String(text || '');
23
+ for (const m of s.matchAll(/\[\[([^\]|#]{1,80})(?:[|#][^\]]*)?\]\]/g)) {
24
+ const name = m[1].trim();
25
+ const slug = bySlug.get(name.toLowerCase());
26
+ if (slug) found.add(slug);
27
+ else topics.add(name);
28
+ }
29
+ if (re) for (const m of s.matchAll(re)) found.add(bySlug.get(m[1].toLowerCase()));
30
+ return { projects: found, topics };
31
+ };
32
+ }
33
+
34
+ export function buildGraph(store) {
35
+ const nodes = new Map();
36
+ const edges = new Map();
37
+ const node = (id, type, label, extra = {}) => {
38
+ if (!nodes.has(id)) nodes.set(id, { id, type, label, ...extra });
39
+ return id;
40
+ };
41
+ const link = (a, b, kind) => {
42
+ if (a === b) return;
43
+ const key = a < b ? `${a}|${b}` : `${b}|${a}`;
44
+ if (!edges.has(key)) edges.set(key, { source: a, target: b, kind });
45
+ };
46
+
47
+ const projects = store.listProjects();
48
+ const find = mentionFinder(projects);
49
+ const mentions = (from, text) => {
50
+ const { projects: ps, topics } = find(text);
51
+ for (const slug of ps) link(from, `project:${slug}`, 'mentions');
52
+ for (const t of topics) link(from, node(`topic:${t.toLowerCase()}`, 'topic', t), 'mentions');
53
+ };
54
+
55
+ const you = node('you', 'you', 'You', { detail: store.getProfile().replace(/<!--[\s\S]*?-->/g, '').trim() });
56
+
57
+ // Profile facts: bullet lines.
58
+ let i = 0;
59
+ for (const line of store.getProfile().split('\n')) {
60
+ const m = line.match(/^\s*[-*]\s+(.+)/);
61
+ if (!m) continue;
62
+ const text = m[1].replace(/\s*_\(\d{4}-\d\d-\d\d\)_\s*$/, '');
63
+ const id = node(`fact:${i++}`, 'fact', text.length > 48 ? text.slice(0, 46) + '…' : text, { detail: text });
64
+ link(you, id, 'fact');
65
+ mentions(id, text);
66
+ }
67
+
68
+ for (const p of projects) {
69
+ const id = node(`project:${p.slug}`, 'project', p.name, {
70
+ slug: p.slug,
71
+ status: p.status || 'active',
72
+ detail: [p.description, p.aliases?.length ? `Aka: ${p.aliases.join(', ')}` : '', p.repo ? `Repo: ${p.repo}` : '']
73
+ .filter(Boolean)
74
+ .join('\n'),
75
+ notes: (p.notes || []).slice(-8).map((n) => n.text),
76
+ });
77
+ link(you, id, 'project');
78
+ for (const t of p.tags || []) link(id, node(`tag:${t.toLowerCase()}`, 'tag', `#${t}`), 'tag');
79
+ for (const d of Object.keys(p.paths || {})) link(id, node(`device:${d}`, 'device', d), 'device');
80
+ mentions(id, [p.description, ...(p.notes || []).map((n) => n.text)].join('\n'));
81
+ }
82
+
83
+ for (const h of store.listHandoffs({ status: 'all' })) {
84
+ const id = node(`handoff:${h.id}`, 'handoff', h.title, {
85
+ status: h.status,
86
+ detail: [h.summary, h.nextSteps?.length ? `Next: ${h.nextSteps.join('; ')}` : ''].filter(Boolean).join('\n\n'),
87
+ });
88
+ if (h.project && nodes.has(`project:${h.project}`)) link(id, `project:${h.project}`, 'handoff');
89
+ else link(id, you, 'handoff');
90
+ if (h.device) link(id, node(`device:${h.device}`, 'device', h.device), 'device');
91
+ mentions(id, `${h.title}\n${h.summary}\n${(h.nextSteps || []).join('\n')}`);
92
+ }
93
+
94
+ for (const it of store.listInbox()) {
95
+ const id = node(`inbox:${it.id}`, 'inbox', it.text.length > 40 ? it.text.slice(0, 38) + '…' : it.text, { detail: it.text });
96
+ const target = it.project && nodes.has(`project:${it.project}`) ? `project:${it.project}` : you;
97
+ link(id, target, 'inbox');
98
+ mentions(id, it.text);
99
+ }
100
+
101
+ const list = [...nodes.values()];
102
+ const degree = new Map(list.map((n) => [n.id, 0]));
103
+ for (const e of edges.values()) {
104
+ degree.set(e.source, degree.get(e.source) + 1);
105
+ degree.set(e.target, degree.get(e.target) + 1);
106
+ }
107
+ for (const n of list) n.degree = degree.get(n.id);
108
+ return { nodes: list, edges: [...edges.values()] };
109
+ }
110
+
111
+ /** Everything one hop (or `depth` hops) from the nodes matching `query`, for the `related` tool. */
112
+ export function neighborhood(graph, startIds, depth = 1) {
113
+ const adj = new Map(graph.nodes.map((n) => [n.id, []]));
114
+ for (const e of graph.edges) {
115
+ adj.get(e.source).push({ id: e.target, kind: e.kind });
116
+ adj.get(e.target).push({ id: e.source, kind: e.kind });
117
+ }
118
+ const byId = new Map(graph.nodes.map((n) => [n.id, n]));
119
+ const seen = new Map(startIds.map((id) => [id, 0]));
120
+ let frontier = [...startIds];
121
+ for (let d = 1; d <= depth; d++) {
122
+ const next = [];
123
+ for (const id of frontier) for (const { id: nb } of adj.get(id) || []) if (!seen.has(nb)) { seen.set(nb, d); next.push(nb); }
124
+ frontier = next;
125
+ }
126
+ return [...seen.entries()]
127
+ .filter(([id, d]) => d > 0 && id !== 'you')
128
+ .map(([id, d]) => ({ ...byId.get(id), distance: d }))
129
+ .sort((a, b) => a.distance - b.distance || b.degree - a.degree);
130
+ }
package/src/index.js CHANGED
@@ -3,3 +3,4 @@ export { resolveProjects, scoreProject } from './resolve.js';
3
3
  export { buildContext } from './context.js';
4
4
  export { sync, pull, initSync, createGithubRemote, syncStatus } from './sync.js';
5
5
  export { installClaudeDesktop, installClaudeCode, claudeDesktopConfigPath } from './install.js';
6
+ export { buildGraph, neighborhood } from './graph.js';
package/src/mcp.js CHANGED
@@ -6,6 +6,7 @@ import { z } from 'zod';
6
6
  import { Store, VERSION, gitStateOf } from './store.js';
7
7
  import { resolveProjects } from './resolve.js';
8
8
  import { buildContext } from './context.js';
9
+ import { buildGraph, neighborhood } from './graph.js';
9
10
  import { sync, syncStatus } from './sync.js';
10
11
 
11
12
  const text = (t) => ({ content: [{ type: 'text', text: typeof t === 'string' ? t : JSON.stringify(t, null, 2) }] });
@@ -315,6 +316,35 @@ export async function startMcpServer(store = new Store()) {
315
316
  }),
316
317
  );
317
318
 
319
+ server.registerTool(
320
+ 'related',
321
+ {
322
+ title: 'Find related things',
323
+ description:
324
+ 'Walk the knowledge graph from a project, tag (e.g. "#godot"), device, or [[topic]]: returns linked projects, tags, handoffs, facts and inbox items. Use for "what else is connected to X" or "which of my projects use Y".',
325
+ inputSchema: {
326
+ query: z.string().describe('Project reference, #tag, device name, or topic'),
327
+ depth: z.number().int().min(1).max(3).optional().describe('Hops to follow (default 1)'),
328
+ },
329
+ annotations: { readOnlyHint: true },
330
+ },
331
+ guard(async ({ query, depth = 1 }) => {
332
+ const g = buildGraph(store);
333
+ const q = query.trim().toLowerCase().replace(/^\[\[|\]\]$/g, '');
334
+ let start = g.nodes.filter((n) => n.id !== 'you' && (n.label.toLowerCase() === q || n.label.toLowerCase() === `#${q}` || n.id.endsWith(`:${q}`)));
335
+ if (!start.length) {
336
+ const p = resolveProjects(store.listProjects(), query, 1)[0]?.project;
337
+ if (p) start = g.nodes.filter((n) => n.id === `project:${p.slug}`);
338
+ }
339
+ if (!start.length) return fail(`Nothing in the graph matches "${query}".`);
340
+ const hits = neighborhood(g, start.map((n) => n.id), depth);
341
+ return text({
342
+ from: start.map((n) => n.label),
343
+ related: hits.map(({ id, type, label, distance, status }) => ({ type, label, id, distance, ...(status ? { status } : {}) })),
344
+ });
345
+ }),
346
+ );
347
+
318
348
  server.registerTool(
319
349
  'capture',
320
350
  {
package/src/store.js CHANGED
@@ -13,8 +13,9 @@ import path from 'node:path';
13
13
  import os from 'node:os';
14
14
  import crypto from 'node:crypto';
15
15
  import { execFileSync } from 'node:child_process';
16
+ import { buildGraph } from './graph.js';
16
17
 
17
- export const VERSION = '0.2.0';
18
+ export const VERSION = '0.3.0';
18
19
 
19
20
  export function defaultHome() {
20
21
  return process.env.OMNICONTEXT_HOME || path.join(os.homedir(), '.omnicontext');
@@ -363,6 +364,7 @@ export class Store {
363
364
  projects,
364
365
  handoffs,
365
366
  inbox,
367
+ graph: compactGraph(buildGraph(this)),
366
368
  };
367
369
  }
368
370
  writeSnapshot() {
@@ -398,6 +400,14 @@ export class Store {
398
400
  }
399
401
  }
400
402
 
403
+ /** Graph in a compact form for the watch: nodes [id, type, label], edges [source, target, kind]. */
404
+ function compactGraph(g) {
405
+ return {
406
+ nodes: g.nodes.map((n) => [n.id, n.type, n.label]),
407
+ edges: g.edges.map((e) => [e.source, e.target, e.kind]),
408
+ };
409
+ }
410
+
401
411
  function newId() {
402
412
  const stamp = now().replace(/[-:]/g, '').replace('T', '-').slice(0, 15);
403
413
  return `${stamp}-${crypto.randomBytes(2).toString('hex')}`;