Graphs that teach > graphs that impress. Turn any code into an interactive knowledge graph you can explore, search, and ask questions about. Works with Claude Code, Codex, Cursor, Copilot, Gemini CLI, and more.
TypeScript antigravity-skillsbusiness-knowledgeclaude-codeclaude-skillscodebase-analysiscodexcodex-skillsdeveloper-tools-ai-agentgemini-cli-skillskarpathy-llm-wikiknowledge-baseknowledge-graphmemoryopencode-skillspi-agentunderstandcodevibe-coding
feat(core): add JSON persistence for knowledge graph and meta
Lum1104a5327ce64d62
diff --git a/packages/core/package.json b/packages/core/package.jsonindex 8a97a80a..46d3b937 100644--- a/packages/core/package.json+++ b/packages/core/package.json@@ -9,6 +9,7 @@"test": "vitest run"},"devDependencies": {+ "@types/node": "^25.5.0","typescript": "^5.7.0","vitest": "^3.1.0"}diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex a0c4ebf2..37327d13 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -1 +1,2 @@export * from "./types.js";+export * from "./persistence/index.js";diff --git a/packages/core/src/persistence/index.ts b/packages/core/src/persistence/index.tsnew file mode 100644index 00000000..30965c01--- /dev/null+++ b/packages/core/src/persistence/index.ts@@ -0,0 +1,37 @@+import { readFileSync, writeFileSync, mkdirSync, existsSync } from "node:fs";+import { join } from "node:path";+import type { KnowledgeGraph, AnalysisMeta } from "../types.js";++const UA_DIR = ".understand-anything";+const GRAPH_FILE = "knowledge-graph.json";+const META_FILE = "meta.json";++function ensureDir(projectRoot: string): string {+ const dir = join(projectRoot, UA_DIR);+ if (!existsSync(dir)) {+ mkdirSync(dir, { recursive: true });+ }+ return dir;+}++export function saveGraph(projectRoot: string, graph: KnowledgeGraph): void {+ const dir = ensureDir(projectRoot);+ writeFileSync(join(dir, GRAPH_FILE), JSON.stringify(graph, null, 2), "utf-8");+}++export function loadGraph(projectRoot: string): KnowledgeGraph | null {+ const filePath = join(projectRoot, UA_DIR, GRAPH_FILE);+ if (!existsSync(filePath)) return null;+ return JSON.parse(readFileSync(filePath, "utf-8")) as KnowledgeGraph;+}++export function saveMeta(projectRoot: string, meta: AnalysisMeta): void {+ const dir = ensureDir(projectRoot);+ writeFileSync(join(dir, META_FILE), JSON.stringify(meta, null, 2), "utf-8");+}++export function loadMeta(projectRoot: string): AnalysisMeta | null {+ const filePath = join(projectRoot, UA_DIR, META_FILE);+ if (!existsSync(filePath)) return null;+ return JSON.parse(readFileSync(filePath, "utf-8")) as AnalysisMeta;+}diff --git a/packages/core/src/persistence/persistence.test.ts b/packages/core/src/persistence/persistence.test.tsnew file mode 100644index 00000000..ce159d4b--- /dev/null+++ b/packages/core/src/persistence/persistence.test.ts@@ -0,0 +1,118 @@+import { describe, it, expect, beforeEach, afterEach } from "vitest";+import { mkdtempSync, rmSync, existsSync } from "node:fs";+import { join } from "node:path";+import { tmpdir } from "node:os";+import { saveGraph, loadGraph, saveMeta, loadMeta } from "./index.js";+import type { KnowledgeGraph, AnalysisMeta } from "../types.js";++describe("persistence", () => {+ let tempDir: string;++ beforeEach(() => {+ tempDir = mkdtempSync(join(tmpdir(), "ua-test-"));+ });++ afterEach(() => {+ rmSync(tempDir, { recursive: true, force: true });+ });++ const sampleGraph: KnowledgeGraph = {+ version: "1.0.0",+ project: {+ name: "test-project",+ languages: ["typescript"],+ frameworks: ["vitest"],+ description: "A test project",+ analyzedAt: "2026-03-14T00:00:00.000Z",+ gitCommitHash: "abc123",+ },+ nodes: [+ {+ id: "node-1",+ type: "file",+ name: "index.ts",+ filePath: "src/index.ts",+ lineRange: [1, 50],+ summary: "Entry point",+ tags: ["entry"],+ complexity: "simple",+ },+ ],+ edges: [+ {+ source: "node-1",+ target: "node-2",+ type: "imports",+ direction: "forward",+ weight: 0.8,+ },+ ],+ layers: [+ {+ id: "layer-1",+ name: "Core",+ description: "Core layer",+ nodeIds: ["node-1"],+ },+ ],+ tour: [+ {+ order: 1,+ title: "Start here",+ description: "Begin with the entry point",+ nodeIds: ["node-1"],+ },+ ],+ };++ const sampleMeta: AnalysisMeta = {+ lastAnalyzedAt: "2026-03-14T00:00:00.000Z",+ gitCommitHash: "abc123",+ version: "1.0.0",+ analyzedFiles: 42,+ };++ describe("saveGraph / loadGraph", () => {+ it("should write knowledge-graph.json to .understand-anything/", () => {+ saveGraph(tempDir, sampleGraph);++ const filePath = join(tempDir, ".understand-anything", "knowledge-graph.json");+ expect(existsSync(filePath)).toBe(true);+ });++ it("should read back the saved graph correctly", () => {+ saveGraph(tempDir, sampleGraph);+ const loaded = loadGraph(tempDir);++ expect(loaded).not.toBeNull();+ expect(loaded).toEqual(sampleGraph);+ });++ it("should return null when no graph exists", () => {+ const loaded = loadGraph(tempDir);+ expect(loaded).toBeNull();+ });+ });++ describe("saveMeta / loadMeta", () => {+ it("should write meta.json to .understand-anything/", () => {+ saveMeta(tempDir, sampleMeta);++ const filePath = join(tempDir, ".understand-anything", "meta.json");+ expect(existsSync(filePath)).toBe(true);+ });++ it("should read back the saved meta correctly", () => {+ saveMeta(tempDir, sampleMeta);+ const loaded = loadMeta(tempDir);++ expect(loaded).not.toBeNull();+ expect(loaded).toEqual(sampleMeta);+ });++ it("should return null when no meta exists", () => {+ const loaded = loadMeta(tempDir);+ expect(loaded).toBeNull();+ });+ });+});diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex e2e9acb8..6eb4d75d 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -13,16 +13,19 @@ importers:version: 5.9.3vitest:specifier: ^3.1.0- version: 3.2.4+ version: 3.2.4(@types/[email protected])packages/core:devDependencies:+ '@types/node':+ specifier: ^25.5.0+ version: 25.5.0typescript:specifier: ^5.7.0version: 5.9.3vitest:specifier: ^3.1.0- version: 3.2.4+ version: 3.2.4(@types/[email protected])packages:@@ -319,6 +322,9 @@ packages:'@types/[email protected]':resolution: {integrity: sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w==}+ '@types/[email protected]':+ resolution: {integrity: sha512-jp2P3tQMSxWugkCUKLRPVUpGaL5MVFwF8RDuSRztfwgN1wmqJeMSbKlnEtQqU8UrhTmzEmZdu2I6v2dpp7XIxw==}+'@vitest/[email protected]':resolution: {integrity: sha512-Io0yyORnB6sikFlt8QW5K7slY4OjqNX9jmJQ02QDda8lyM6B5oNgVWoSoKPac8/kgnCUzuHQKrSLtu/uOqqrig==}@@ -489,6 +495,9 @@ packages:engines: {node: '>=14.17'}hasBin: true+ resolution: {integrity: sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w==}+resolution: {integrity: sha512-EbKSKh+bh1E1IFxeO0pg1n4dvoOTt0UDiXMd/qn++r98+jPO1xtJilvXldeuQ8giIB5IkpjCgMleHMNEsGH6pg==}engines: {node: ^18.0.0 || ^20.0.0 || >=22.0.0}@@ -733,6 +742,10 @@ snapshots:'@types/[email protected]': {}+ '@types/[email protected]':+ dependencies:+ undici-types: 7.18.2+'@vitest/[email protected]':dependencies:'@types/chai': 5.2.3@@ -741,13 +754,13 @@ snapshots:chai: 5.3.3tinyrainbow: 2.0.0- '@vitest/[email protected]([email protected])':dependencies:'@vitest/spy': 3.2.4estree-walker: 3.0.3magic-string: 0.30.21optionalDependencies:- vite: 7.3.1+ vite: 7.3.1(@types/[email protected])'@vitest/[email protected]':dependencies:@@ -925,13 +938,15 @@ snapshots:+ [email protected]: {}++ [email protected](@types/[email protected]):dependencies:cac: 6.7.14debug: 4.4.3es-module-lexer: 1.7.0pathe: 2.0.3- vite: 7.3.1+ vite: 7.3.1(@types/[email protected])transitivePeerDependencies:- '@types/node'- jiti@@ -946,7 +961,7 @@ snapshots:- tsx- yaml+ [email protected](@types/[email protected]):dependencies:esbuild: 0.27.4fdir: 6.5.0([email protected])@@ -955,13 +970,14 @@ snapshots:rollup: 4.59.0tinyglobby: 0.2.15optionalDependencies:+ '@types/node': 25.5.0fsevents: 2.3.3+ [email protected](@types/[email protected]):dependencies:'@types/chai': 5.2.3'@vitest/expect': 3.2.4- '@vitest/mocker': 3.2.4([email protected])+ '@vitest/mocker': 3.2.4([email protected](@types/[email protected]))'@vitest/pretty-format': 3.2.4'@vitest/runner': 3.2.4'@vitest/snapshot': 3.2.4@@ -979,9 +995,11 @@ snapshots:tinyglobby: 0.2.15tinypool: 1.1.1tinyrainbow: 2.0.0- vite: 7.3.1- vite-node: 3.2.4+ vite: 7.3.1(@types/[email protected])+ vite-node: 3.2.4(@types/[email protected])why-is-node-running: 2.3.0+ optionalDependencies:+ '@types/node': 25.5.0transitivePeerDependencies:- jiti- less