summaryrefslogtreecommitdiffhomepage
path: root/src/vault-context.ts
blob: 6ac55ca0906ae0c01a98a074f29a5418075170d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import type { App } from "obsidian";
import vaultContextTemplate from "./context/vault-context-template.json";

/**
 * Collected vault context summary injected into the AI system prompt.
 */
export interface VaultContext {
	vaultName: string;
	totalNotes: number;
	totalFolders: number;
	folderTree: string;
	tagTaxonomy: string;
	recentFiles: string;
}

/**
 * Build a folder tree string from the vault.
 * Produces an indented tree like:
 *   /
 *   ├── folder-a/
 *   │   ├── subfolder/
 *   ├── folder-b/
 */
function buildFolderTree(app: App): string {
	const folders = app.vault.getAllFolders(true);
	// Build a map of parent → children folder names
	const tree = new Map<string, string[]>();

	for (const folder of folders) {
		if (folder.isRoot()) continue;
		const parentPath = folder.parent?.path ?? "/";
		const key = parentPath === "/" || parentPath === "" ? "/" : parentPath;
		if (!tree.has(key)) {
			tree.set(key, []);
		}
		const siblings = tree.get(key);
		if (siblings !== undefined) {
			siblings.push(folder.path);
		}
	}

	const lines: string[] = [];

	function walk(path: string, prefix: string): void {
		const children = tree.get(path) ?? [];
		children.sort();
		for (let i = 0; i < children.length; i++) {
			const child: string | undefined = children[i];
			if (child === undefined) continue;
			const isLast = i === children.length - 1;
			const connector = isLast ? "└── " : "├── ";
			const childPrefix = isLast ? "    " : "│   ";
			// Show just the folder name, not the full path
			const name = child.split("/").pop() ?? child;
			lines.push(`${prefix}${connector}${name}/`);
			walk(child, prefix + childPrefix);
		}
	}

	lines.push("/");
	walk("/", "");

	return lines.join("\n");
}

/**
 * Collect all tags in the vault with their usage counts.
 * Returns a formatted string like: #tag1 (12), #tag2 (8), ...
 */
function buildTagTaxonomy(app: App): string {
	const tagCounts = new Map<string, number>();
	const files = app.vault.getMarkdownFiles();

	for (const file of files) {
		const cache = app.metadataCache.getFileCache(file);
		if (cache === null) continue;

		// Inline tags
		if (cache.tags !== undefined) {
			for (const tagEntry of cache.tags) {
				const tag = tagEntry.tag.toLowerCase();
				tagCounts.set(tag, (tagCounts.get(tag) ?? 0) + 1);
			}
		}

		// Frontmatter tags
		if (cache.frontmatter?.tags !== undefined) {
			const fmTags = cache.frontmatter.tags;
			if (Array.isArray(fmTags)) {
				for (const raw of fmTags) {
					const tag = typeof raw === "string"
						? (raw.startsWith("#") ? raw.toLowerCase() : `#${raw.toLowerCase()}`)
						: "";
					if (tag !== "") {
						tagCounts.set(tag, (tagCounts.get(tag) ?? 0) + 1);
					}
				}
			}
		}
	}

	if (tagCounts.size === 0) {
		return "No tags in vault.";
	}

	// Sort by count descending
	const sorted = [...tagCounts.entries()].sort((a, b) => b[1] - a[1]);

	// Cap at 100 tags to avoid overwhelming context
	const maxTags = 100;
	const limited = sorted.slice(0, maxTags);
	const lines = limited.map(([tag, count]) => `${tag} (${count})`);
	const suffix = sorted.length > maxTags
		? `\n...and ${sorted.length - maxTags} more tags.`
		: "";

	return lines.join(", ") + suffix;
}

/**
 * Get the most recently modified files.
 */
function buildRecentFiles(app: App, maxFiles: number): string {
	const files = app.vault.getMarkdownFiles();

	// Sort by modification time descending
	const sorted = [...files].sort((a, b) => b.stat.mtime - a.stat.mtime);
	const limited = sorted.slice(0, maxFiles);

	if (limited.length === 0) {
		return "No notes in vault.";
	}

	return limited.map((f) => f.path).join("\n");
}

/**
 * Collect the full vault context summary.
 * This is cheap — all data comes from the metadata cache and vault indexes.
 */
export function collectVaultContext(app: App, maxRecentFiles: number): VaultContext {
	const markdownFiles = app.vault.getMarkdownFiles();
	const allFolders = app.vault.getAllFolders(false);

	return {
		vaultName: app.vault.getName(),
		totalNotes: markdownFiles.length,
		totalFolders: allFolders.length,
		folderTree: buildFolderTree(app),
		tagTaxonomy: buildTagTaxonomy(app),
		recentFiles: buildRecentFiles(app, maxRecentFiles),
	};
}

/**
 * Format the vault context into a system prompt block using the JSON template.
 */
export function formatVaultContext(ctx: VaultContext): string {
	const t = vaultContextTemplate;
	const lines: string[] = [];

	lines.push(t.prefix);
	lines.push("");
	lines.push(t.fields.vaultName.replace("{vaultName}", ctx.vaultName));
	lines.push(t.fields.totalNotes.replace("{totalNotes}", String(ctx.totalNotes)));
	lines.push(t.fields.totalFolders.replace("{totalFolders}", String(ctx.totalFolders)));
	lines.push("");
	lines.push(t.sections.folderTree.replace("{folderTree}", ctx.folderTree));
	lines.push("");
	lines.push(t.sections.tagTaxonomy.replace("{tagTaxonomy}", ctx.tagTaxonomy));
	lines.push("");
	lines.push(t.sections.recentFiles.replace("{recentFiles}", ctx.recentFiles));

	return lines.join("\n");
}