-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.mjs
More file actions
640 lines (586 loc) · 24.1 KB
/
extension.mjs
File metadata and controls
640 lines (586 loc) · 24.1 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
// Extension: copilot-obsidian
// A GitHub Copilot CLI extension that connects your Obsidian vault
// to the Copilot agent — read, write, search, and manage notes
// directly from the terminal.
import {
readFileSync,
writeFileSync,
appendFileSync,
existsSync,
mkdirSync,
readdirSync,
statSync,
unlinkSync,
renameSync,
} from "node:fs";
import { join, dirname } from "node:path";
import { approveAll } from "@github/copilot-sdk";
import { joinSession } from "@github/copilot-sdk/extension";
// ---------------------------------------------------------------------------
// Configuration — config file with env-var fallback
// ---------------------------------------------------------------------------
const HOME = process.env.USERPROFILE || process.env.HOME || "~";
const EXTENSION_DIR = dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Z]:)/, "$1"));
/** Load obsidian.config.json from the extension directory, if it exists. */
function loadConfig() {
const configPath = join(EXTENSION_DIR, "obsidian.config.json");
if (existsSync(configPath)) {
try {
return JSON.parse(readFileSync(configPath, "utf-8"));
} catch { /* fall through to defaults */ }
}
return {};
}
const CONFIG = loadConfig();
// Vault path: config.vault > OBSIDIAN_VAULT_PATH > OBSIDIAN_VAULT_NAME > default
function resolveVaultPath() {
if (CONFIG.vault) return CONFIG.vault.replace(/^~/, HOME);
if (process.env.OBSIDIAN_VAULT_PATH) return process.env.OBSIDIAN_VAULT_PATH;
const name = process.env.OBSIDIAN_VAULT_NAME || "MyVault";
return join(HOME, "ObsidianVaults", name);
}
const VAULT_PATH = resolveVaultPath();
// Context-injection keywords
const CONTEXT_KEYWORDS = (
CONFIG.keywords
? CONFIG.keywords
: (process.env.OBSIDIAN_KEYWORDS || "vault,obsidian,note,knowledge")
.split(",")
.map((k) => k.trim())
).map((k) => k.toLowerCase()).filter(Boolean);
// Daily note settings
const DAILY_FOLDER = CONFIG.daily?.folder || "DailyNotes";
const DAILY_TEMPLATE = CONFIG.daily?.template || "---\ndate: {{date}}\n---\n\n# {{date}}\n\n## Notes\n\n";
// Templates from config
const TEMPLATES = CONFIG.templates || {};
// Folders to exclude from recursive scans (e.g. agent session data, .git)
const EXCLUDE_DIRS = new Set((CONFIG.exclude || []).map((d) => d.toLowerCase()));
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/** Recursively collect all .md files under a directory.
* Returns array of { folder, name, absPath } where folder is relative to vault root.
* Skips dotfiles and directories listed in config.exclude. */
function walkVault(dir = VAULT_PATH, relDir = "") {
const results = [];
if (!existsSync(dir)) return results;
for (const entry of readdirSync(dir, { withFileTypes: true })) {
if (entry.name.startsWith(".")) continue;
if (EXCLUDE_DIRS.has(entry.name.toLowerCase())) continue;
const abs = join(dir, entry.name);
const rel = relDir ? `${relDir}/${entry.name}` : entry.name;
if (entry.isDirectory()) {
results.push(...walkVault(abs, rel));
} else if (entry.name.endsWith(".md")) {
results.push({
folder: relDir || ".",
name: entry.name.replace(/\.md$/, ""),
absPath: abs,
});
}
}
return results;
}
/** Return all top-level folders in the vault (excluding dotfiles and config.exclude). */
function listFolders() {
if (!existsSync(VAULT_PATH)) return [];
return readdirSync(VAULT_PATH).filter((f) => {
if (f.startsWith(".")) return false;
if (EXCLUDE_DIRS.has(f.toLowerCase())) return false;
return statSync(join(VAULT_PATH, f)).isDirectory();
});
}
/** List markdown files in a folder (non-recursive). */
function listNotes(folder) {
const dirPath = join(VAULT_PATH, folder);
if (!existsSync(dirPath)) return [];
return readdirSync(dirPath)
.filter((f) => f.endsWith(".md"))
.map((f) => f.replace(/\.md$/, ""));
}
/** Read a single note. Returns null if not found. */
function readNote(folder, name) {
const filePath = join(VAULT_PATH, folder, `${name}.md`);
if (!existsSync(filePath)) return null;
return readFileSync(filePath, "utf-8");
}
/** Write (create or overwrite) a note. Returns the absolute path written. */
function writeNote(folder, name, content) {
const dirPath = join(VAULT_PATH, folder);
if (!existsSync(dirPath)) mkdirSync(dirPath, { recursive: true });
const filePath = join(dirPath, `${name}.md`);
writeFileSync(filePath, content, "utf-8");
return filePath;
}
/** Parse YAML frontmatter from markdown content. Returns { frontmatter, body }. */
function parseFrontmatter(content) {
const match = content.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/);
if (!match) return { frontmatter: {}, body: content };
const fm = {};
for (const line of match[1].split("\n")) {
const colon = line.indexOf(":");
if (colon === -1) continue;
const key = line.slice(0, colon).trim();
let value = line.slice(colon + 1).trim();
// Handle simple arrays: [a, b, c]
if (value.startsWith("[") && value.endsWith("]")) {
value = value.slice(1, -1).split(",").map((v) => v.trim().replace(/^["']|["']$/g, ""));
}
// Handle quoted strings
if (typeof value === "string") {
value = value.replace(/^["']|["']$/g, "");
}
fm[key] = value;
}
return { frontmatter: fm, body: match[2] };
}
/** Render a template string with {{key}} placeholders. */
function renderTemplate(template, vars) {
return template.replace(/\{\{(\w+)\}\}/g, (_, key) => vars[key] ?? `{{${key}}}`);
}
/** Get today's date as YYYY-MM-DD. */
function today() {
return new Date().toISOString().split("T")[0];
}
/** Substring search across the entire vault (recursive). Supports optional frontmatter filters. */
function searchVault(query, filters = {}) {
const results = [];
const lowerQuery = query ? query.toLowerCase() : null;
const allNotes = walkVault();
for (const note of allNotes) {
const content = readFileSync(note.absPath, "utf-8");
const { frontmatter } = parseFrontmatter(content);
// Apply frontmatter filters
let filterMatch = true;
for (const [key, val] of Object.entries(filters)) {
const fmVal = frontmatter[key];
if (!fmVal) { filterMatch = false; break; }
const fmLower = Array.isArray(fmVal)
? fmVal.map((v) => String(v).toLowerCase())
: [String(fmVal).toLowerCase()];
if (!fmLower.some((v) => v.includes(val.toLowerCase()))) { filterMatch = false; break; }
}
if (!filterMatch) continue;
// Text search (skip if no query — filter-only mode)
if (lowerQuery && !content.toLowerCase().includes(lowerQuery)) continue;
// Build snippet
let snippet = "";
if (lowerQuery) {
const idx = content.toLowerCase().indexOf(lowerQuery);
const start = Math.max(0, idx - 100);
const end = Math.min(content.length, idx + query.length + 100);
snippet = content.substring(start, end).replace(/\n/g, " ").trim();
} else {
snippet = content.substring(0, 200).replace(/\n/g, " ").trim();
}
results.push({
folder: note.folder,
name: note.name,
frontmatter,
snippet,
});
}
return results;
}
/** Find all notes that contain a [[wikilink]] to the given note name. */
function findBacklinks(targetName) {
const allNotes = walkVault();
const results = [];
// Match [[targetName]] or [[targetName|alias]]
const patterns = [
`[[${targetName}]]`,
`[[${targetName}|`,
];
const lower = patterns.map((p) => p.toLowerCase());
for (const note of allNotes) {
if (note.name === targetName && note.folder === ".") continue;
const content = readFileSync(note.absPath, "utf-8");
const contentLower = content.toLowerCase();
if (lower.some((p) => contentLower.includes(p))) {
// Extract snippet around the link
const idx = Math.max(
contentLower.indexOf(lower[0]),
contentLower.indexOf(lower[1]),
);
const start = Math.max(0, idx - 80);
const end = Math.min(content.length, idx + targetName.length + 80);
results.push({
folder: note.folder,
name: note.name,
snippet: content.substring(start, end).replace(/\n/g, " ").trim(),
});
}
}
return results;
}
/** Get recently modified notes across the vault, sorted by mtime desc. */
function recentNotes(limit = 10) {
const allNotes = walkVault();
const withMtime = allNotes.map((note) => {
const stat = statSync(note.absPath);
return { ...note, mtime: stat.mtimeMs, modified: stat.mtime.toISOString() };
});
withMtime.sort((a, b) => b.mtime - a.mtime);
return withMtime.slice(0, limit);
}
// ---------------------------------------------------------------------------
// Extension entry point
// ---------------------------------------------------------------------------
const session = await joinSession({
onPermissionRequest: approveAll,
hooks: {
onSessionStart: async () => {
const configSource = CONFIG.vault ? "config file" : (process.env.OBSIDIAN_VAULT_PATH ? "env var" : "default");
await session.log(
`📓 Obsidian extension loaded — vault: ${VAULT_PATH} (${configSource})`,
);
},
onUserPromptSubmitted: async (input) => {
const prompt = input.prompt.toLowerCase();
const mentionsVault = CONTEXT_KEYWORDS.some((k) => prompt.includes(k));
if (!mentionsVault) return;
const folders = listFolders();
const folderList = folders
.map((f) => {
const notes = listNotes(f);
const desc = CONFIG.folders?.[f]?.description;
return `- **${f}/** (${notes.length} notes)${desc ? " — " + desc : ""}`;
})
.join("\n");
return {
additionalContext: [
`[Obsidian Vault] The user has an Obsidian vault at ${VAULT_PATH}.`,
`Folders:\n${folderList}`,
`Tools: vault_read, vault_write, vault_append, vault_list, vault_search, vault_summary, vault_backlinks, vault_recent, vault_daily, vault_delete, vault_rename.`,
].join("\n"),
};
},
},
// ---------------------------------------------------------------------------
// Tools
// ---------------------------------------------------------------------------
tools: [
// ---- vault_read --------------------------------------------------------
{
name: "vault_read",
description:
"Read a note from the Obsidian vault. Returns the full markdown content.",
parameters: {
type: "object",
properties: {
folder: {
type: "string",
description: "Vault folder name (e.g. 'Projects', 'DailyNotes'). Use vault_list to discover folders.",
},
name: {
type: "string",
description: "Note name without .md extension.",
},
},
required: ["folder", "name"],
},
handler: async (args) => {
const content = readNote(args.folder, args.name);
if (!content)
return { textResultForLlm: `Note not found: ${args.folder}/${args.name}.md`, resultType: "failure" };
return content;
},
},
// ---- vault_write -------------------------------------------------------
{
name: "vault_write",
description:
"Create or update a note in the Obsidian vault. Use [[wikilinks]] for cross-references. Include YAML frontmatter when appropriate.",
parameters: {
type: "object",
properties: {
folder: { type: "string", description: "Vault folder to write into. Created if it doesn't exist." },
name: { type: "string", description: "Note name without .md extension." },
content: { type: "string", description: "Full markdown content including optional YAML frontmatter." },
template: { type: "string", description: "Optional: name of a template from config to use as the base content. Template variables ({{date}}, {{title}}) are auto-filled." },
},
required: ["folder", "name"],
},
handler: async (args) => {
let content = args.content;
if (!content && args.template && TEMPLATES[args.template]) {
content = renderTemplate(TEMPLATES[args.template], { date: today(), title: args.name });
}
if (!content) {
return { textResultForLlm: "Either content or a valid template name is required.", resultType: "failure" };
}
const filePath = writeNote(args.folder, args.name, content);
return `Note written: ${filePath}`;
},
},
// ---- vault_append ------------------------------------------------------
{
name: "vault_append",
description:
"Append content to an existing note without overwriting it. Creates the note if it doesn't exist. Useful for adding sections, action items, or meeting notes incrementally.",
parameters: {
type: "object",
properties: {
folder: { type: "string", description: "Vault folder containing the note." },
name: { type: "string", description: "Note name without .md extension." },
content: { type: "string", description: "Markdown content to append to the end of the note." },
},
required: ["folder", "name", "content"],
},
handler: async (args) => {
const filePath = join(VAULT_PATH, args.folder, `${args.name}.md`);
if (!existsSync(filePath)) {
// Create it
const dirPath = join(VAULT_PATH, args.folder);
if (!existsSync(dirPath)) mkdirSync(dirPath, { recursive: true });
writeFileSync(filePath, args.content, "utf-8");
return `Created note with content: ${filePath}`;
}
appendFileSync(filePath, `\n${args.content}`, "utf-8");
return `Appended to: ${filePath}`;
},
},
// ---- vault_list --------------------------------------------------------
{
name: "vault_list",
description:
"List all notes in a vault folder, or list all top-level folders if no folder is specified.",
parameters: {
type: "object",
properties: {
folder: { type: "string", description: "Folder to list. Omit to list top-level folders." },
},
},
handler: async (args) => {
if (!args.folder) {
const folders = listFolders();
if (folders.length === 0) return `Vault is empty: ${VAULT_PATH}`;
const lines = folders.map((f) => {
const count = listNotes(f);
const desc = CONFIG.folders?.[f]?.description;
return `- **${f}/** (${count.length} notes)${desc ? " — " + desc : ""}`;
});
return `Vault folders:\n${lines.join("\n")}`;
}
const notes = listNotes(args.folder);
if (notes.length === 0) return `No notes in ${args.folder}/`;
return `${args.folder}/ (${notes.length} notes):\n${notes.map((n) => `- ${n}`).join("\n")}`;
},
},
// ---- vault_search ------------------------------------------------------
{
name: "vault_search",
description:
"Search across all notes in the Obsidian vault. Supports text search and/or frontmatter filters (e.g. type, status, tags). Searches recursively through all folders.",
parameters: {
type: "object",
properties: {
query: { type: "string", description: "Text to search for. Omit to search by frontmatter filters only." },
type: { type: "string", description: "Filter by frontmatter 'type' field (e.g. 'meeting', 'person', 'topic')." },
status: { type: "string", description: "Filter by frontmatter 'status' field (e.g. 'active', 'open')." },
tags: { type: "string", description: "Filter by frontmatter 'tags' field (partial match)." },
},
},
handler: async (args) => {
const filters = {};
if (args.type) filters.type = args.type;
if (args.status) filters.status = args.status;
if (args.tags) filters.tags = args.tags;
if (!args.query && Object.keys(filters).length === 0) {
return { textResultForLlm: "Provide a query and/or frontmatter filters (type, status, tags).", resultType: "failure" };
}
const results = searchVault(args.query || null, filters);
if (results.length === 0) return `No results found.`;
return `Found ${results.length} match(es):\n\n${results
.map((r) => {
const fm = Object.entries(r.frontmatter).map(([k, v]) => `${k}: ${Array.isArray(v) ? v.join(", ") : v}`).join(" | ");
return `**${r.folder}/${r.name}**${fm ? ` [${fm}]` : ""}\n> ...${r.snippet}...`;
})
.join("\n\n")}`;
},
},
// ---- vault_summary -----------------------------------------------------
{
name: "vault_summary",
description:
"High-level vault overview: folders, note counts, wikilink stats, and config info.",
parameters: { type: "object", properties: {} },
handler: async () => {
const folders = listFolders();
const lines = folders.map((f) => {
const notes = listNotes(f);
const desc = CONFIG.folders?.[f]?.description;
return `- **${f}:** ${notes.length} note(s)${desc ? " — " + desc : ""}`;
});
let totalLinks = 0;
let totalNotes = 0;
const allNotes = walkVault();
for (const note of allNotes) {
totalNotes++;
const content = readFileSync(note.absPath, "utf-8");
const links = content.match(/\[\[[^\]]+\]\]/g);
if (links) totalLinks += links.length;
}
const configSource = CONFIG.vault ? "obsidian.config.json" : "environment / defaults";
return [
`## Obsidian Vault Summary`,
`**Path:** ${VAULT_PATH}`,
`**Config:** ${configSource}`,
`**Notes:** ${totalNotes} (across ${folders.length} folders)`,
`**Wikilinks:** ${totalLinks}`,
``,
...lines,
].join("\n");
},
},
// ---- vault_backlinks ---------------------------------------------------
{
name: "vault_backlinks",
description:
"Find all notes that contain a [[wikilink]] to the given note. Essential for understanding how knowledge is connected in the vault.",
parameters: {
type: "object",
properties: {
name: { type: "string", description: "Note name to find backlinks for (e.g. 'ProjectAlpha'). Searched as [[name]] across all vault notes." },
},
required: ["name"],
},
handler: async (args) => {
const results = findBacklinks(args.name);
if (results.length === 0) return `No backlinks found for [[${args.name}]].`;
return `Found ${results.length} note(s) linking to [[${args.name}]]:\n\n${results
.map((r) => `**${r.folder}/${r.name}**\n> ...${r.snippet}...`)
.join("\n\n")}`;
},
},
// ---- vault_recent ------------------------------------------------------
{
name: "vault_recent",
description:
"List the most recently modified notes in the vault, sorted by last-modified time.",
parameters: {
type: "object",
properties: {
limit: { type: "number", description: "Max notes to return (default 10)." },
},
},
handler: async (args) => {
const notes = recentNotes(args.limit || 10);
if (notes.length === 0) return "No notes in vault.";
return `Recently modified notes:\n\n${notes
.map((n, i) => `${i + 1}. **${n.folder}/${n.name}** — ${n.modified.split("T")[0]} ${n.modified.split("T")[1]?.slice(0, 5) || ""}`)
.join("\n")}`;
},
},
// ---- vault_daily -------------------------------------------------------
{
name: "vault_daily",
description:
"Read today's daily note, or create it from a template if it doesn't exist. Optionally specify a different date.",
parameters: {
type: "object",
properties: {
date: { type: "string", description: "Date in YYYY-MM-DD format. Defaults to today." },
},
},
handler: async (args) => {
const date = args.date || today();
const existing = readNote(DAILY_FOLDER, date);
if (existing) return `## Daily Note: ${date}\n\n${existing}`;
// Create from template
const content = renderTemplate(DAILY_TEMPLATE, { date, title: date });
const filePath = writeNote(DAILY_FOLDER, date, content);
return `Created daily note: ${filePath}\n\n${content}`;
},
},
// ---- vault_delete ------------------------------------------------------
{
name: "vault_delete",
description: "Delete a note from the vault. Use with caution.",
parameters: {
type: "object",
properties: {
folder: { type: "string", description: "Vault folder." },
name: { type: "string", description: "Note name without .md extension." },
},
required: ["folder", "name"],
},
handler: async (args) => {
const filePath = join(VAULT_PATH, args.folder, `${args.name}.md`);
if (!existsSync(filePath))
return { textResultForLlm: `Note not found: ${args.folder}/${args.name}.md`, resultType: "failure" };
unlinkSync(filePath);
return `Deleted: ${filePath}`;
},
},
// ---- vault_lookup ------------------------------------------------------
{
name: "vault_lookup",
description:
"Fast lookup across Knowledgebase and Playbooks folders. Returns full note content (not just snippets) for quick reference during calls. Searches by text query across titles and content.",
parameters: {
type: "object",
properties: {
query: {
type: "string",
description: "Search term (e.g. 'quota', 'caching', 'provisioned'). Searched in note titles and content within Knowledgebase/ and Playbooks/ folders.",
},
},
required: ["query"],
},
handler: async (args) => {
const lowerQuery = args.query.toLowerCase();
const refFolders = ["Knowledgebase", "Playbooks"];
const results = [];
for (const folder of refFolders) {
const notes = listNotes(folder);
for (const name of notes) {
const content = readNote(folder, name);
if (!content) continue;
if (
name.toLowerCase().includes(lowerQuery) ||
content.toLowerCase().includes(lowerQuery)
) {
results.push({ folder, name, content });
}
}
}
if (results.length === 0) return `No matches for "${args.query}" in Knowledgebase or Playbooks.`;
return results
.map((r) => `# ${r.folder}/${r.name}\n\n${r.content}`)
.join("\n\n---\n\n");
},
},
// ---- vault_rename ------------------------------------------------------
{
name: "vault_rename",
description:
"Rename or move a note within the vault. Does NOT update wikilinks in other notes.",
parameters: {
type: "object",
properties: {
folder: { type: "string", description: "Current folder." },
name: { type: "string", description: "Current note name (no .md)." },
new_folder: { type: "string", description: "Destination folder. Omit to keep same." },
new_name: { type: "string", description: "New note name (no .md). Omit to keep same." },
},
required: ["folder", "name"],
},
handler: async (args) => {
const srcPath = join(VAULT_PATH, args.folder, `${args.name}.md`);
if (!existsSync(srcPath))
return { textResultForLlm: `Note not found: ${args.folder}/${args.name}.md`, resultType: "failure" };
const destFolder = args.new_folder || args.folder;
const destName = args.new_name || args.name;
const destDir = join(VAULT_PATH, destFolder);
if (!existsSync(destDir)) mkdirSync(destDir, { recursive: true });
const destPath = join(destDir, `${destName}.md`);
renameSync(srcPath, destPath);
return `Renamed: ${srcPath} → ${destPath}`;
},
},
],
});
await session.log(
"📓 Obsidian vault connected — use vault_list to explore, vault_search to find content",
);