Add list_vault_tags and get_vault_stats tools to all agent modes
Extend the tool registry to support vault-wide tag listing and statistical overview tools, adding them to READ, EDIT, ORGANIZE, and RESEARCH agent modes. Includes implementations for tag aggregation, folder structure reporting, and metadataCache integration in VaultIndexer.
This commit is contained in:
@@ -3124,10 +3124,17 @@ var ALL_AGENT_MODES = ["ask", "edit", "organize", "research", "workflow"];
|
||||
function filterToolsByName(tools, allowed) {
|
||||
return tools.filter((t) => allowed.has(t.function.name));
|
||||
}
|
||||
var READ_TOOLS = /* @__PURE__ */ new Set(["read_vault_file", "search_vault_files"]);
|
||||
var READ_TOOLS = /* @__PURE__ */ new Set([
|
||||
"read_vault_file",
|
||||
"search_vault_files",
|
||||
"list_vault_tags",
|
||||
"get_vault_stats"
|
||||
]);
|
||||
var ORGANIZE_TOOLS = /* @__PURE__ */ new Set([
|
||||
"read_vault_file",
|
||||
"search_vault_files",
|
||||
"list_vault_tags",
|
||||
"get_vault_stats",
|
||||
"update_frontmatter",
|
||||
"rename_note",
|
||||
"move_note",
|
||||
@@ -3136,6 +3143,8 @@ var ORGANIZE_TOOLS = /* @__PURE__ */ new Set([
|
||||
var EDIT_TOOLS = /* @__PURE__ */ new Set([
|
||||
"read_vault_file",
|
||||
"search_vault_files",
|
||||
"list_vault_tags",
|
||||
"get_vault_stats",
|
||||
"create_note",
|
||||
"append_to_note",
|
||||
"replace_note_section",
|
||||
@@ -3145,7 +3154,12 @@ var EDIT_TOOLS = /* @__PURE__ */ new Set([
|
||||
"delete_note",
|
||||
"insert_link"
|
||||
]);
|
||||
var RESEARCH_TOOLS = /* @__PURE__ */ new Set(["read_vault_file", "search_vault_files"]);
|
||||
var RESEARCH_TOOLS = /* @__PURE__ */ new Set([
|
||||
"read_vault_file",
|
||||
"search_vault_files",
|
||||
"list_vault_tags",
|
||||
"get_vault_stats"
|
||||
]);
|
||||
var AGENT_MODE_CONFIGS = {
|
||||
ask: {
|
||||
label: "Ask",
|
||||
@@ -3182,8 +3196,9 @@ When editing notes:
|
||||
label: "Organize",
|
||||
description: "Tag, rename, move, and link notes to keep the vault tidy.",
|
||||
systemPrompt: `You are an assistant that helps organize the user's Obsidian vault.
|
||||
You can search notes, read them, update frontmatter tags, rename files, move files to folders, and insert wiki-links.
|
||||
IMPORTANT: When you need vault information, do NOT say you will search or read files. You MUST immediately emit a tool_call to the appropriate tool.
|
||||
You can search notes, read them, list all vault tags, get vault structure stats, update frontmatter tags, rename files, move files to folders, and insert wiki-links.
|
||||
IMPORTANT: When the user asks about tags or folder structure, use list_vault_tags or get_vault_stats FIRST instead of searching blindly.
|
||||
When you need vault information, do NOT say you will search or read files. You MUST immediately emit a tool_call to the appropriate tool.
|
||||
Only respond to the user after you have received and analyzed the tool results.
|
||||
When organizing:
|
||||
- Suggest consistent tag vocabularies.
|
||||
@@ -3199,7 +3214,8 @@ When organizing:
|
||||
description: "Deep vault search and synthesis across multiple notes.",
|
||||
systemPrompt: `You are a research assistant that dives deep into the user's Obsidian vault.
|
||||
Your job is to synthesize information across multiple notes, find connections, and produce comprehensive summaries.
|
||||
IMPORTANT: When you need vault information, do NOT say you will search or read files. You MUST immediately emit a tool_call to search_vault_files or read_vault_file.
|
||||
IMPORTANT: When the user asks about tags or vault structure, use list_vault_tags or get_vault_stats FIRST before searching.
|
||||
When you need vault information, do NOT say you will search or read files. You MUST immediately emit a tool_call to search_vault_files or read_vault_file.
|
||||
Only respond to the user after you have received and analyzed the tool results.
|
||||
Search broadly, read key sources, and cross-reference information.
|
||||
Cite specific notes and quotes where possible.
|
||||
@@ -8565,11 +8581,14 @@ var VaultIndexer = class {
|
||||
setVectorStore(vectorStore) {
|
||||
this.vectorStore = vectorStore;
|
||||
}
|
||||
setApp(app) {
|
||||
this.app = app;
|
||||
}
|
||||
tokenize(text) {
|
||||
return text.toLowerCase().replace(/[^\w\s]/g, "").split(/\s+/).filter((token) => token.length > 1 && !STOP_WORDS.has(token));
|
||||
}
|
||||
tokenizeContent(content, file) {
|
||||
const parsed = this.parseMarkdown(content);
|
||||
const parsed = this.parseMarkdown(content, file);
|
||||
const bodyWithoutFrontmatter = content.replace(/^---[\s\S]*?---\n/, "");
|
||||
const paragraphs = bodyWithoutFrontmatter.split(/\n\n+/).map((p) => p.trim()).filter((p) => p && !p.startsWith("#"));
|
||||
const firstParagraph = paragraphs[0] || "";
|
||||
@@ -8629,7 +8648,7 @@ var VaultIndexer = class {
|
||||
for (const file of files) {
|
||||
try {
|
||||
const content = typeof this.vault.cachedRead === "function" ? await this.vault.cachedRead(file) : await this.vault.read(file);
|
||||
const parsed = this.parseMarkdown(content);
|
||||
const parsed = this.parseMarkdown(content, file);
|
||||
entries.push({
|
||||
file,
|
||||
title: parsed.frontmatter.title || file.basename,
|
||||
@@ -8777,32 +8796,62 @@ var VaultIndexer = class {
|
||||
const queryStem = this.stemToken(queryLower);
|
||||
return textLower.includes(queryLower) || textLower.includes(queryStem);
|
||||
}
|
||||
parseMarkdown(content) {
|
||||
const frontmatterRegex = /^---\n([\s\S]*?)\n---\n/;
|
||||
const frontmatterMatch = content.match(frontmatterRegex);
|
||||
const frontmatter = {};
|
||||
if (frontmatterMatch) {
|
||||
parseMarkdown(content, file) {
|
||||
let frontmatter = {};
|
||||
let title = "";
|
||||
let headings = [];
|
||||
if (this.app && file) {
|
||||
try {
|
||||
const lines = frontmatterMatch[1].trim().split("\n");
|
||||
for (const line of lines) {
|
||||
const [key, ...valueParts] = line.split(":");
|
||||
if (!key) continue;
|
||||
const value = valueParts.join(":").trim();
|
||||
if (key.trim() === "title" && value) frontmatter.title = value;
|
||||
else if (key.trim() === "tags" && value) frontmatter.tags = value;
|
||||
const cache = this.app.metadataCache.getFileCache(file);
|
||||
if (cache?.frontmatter) {
|
||||
const fm = cache.frontmatter;
|
||||
if (typeof fm.title === "string") frontmatter.title = fm.title;
|
||||
const rawTags = fm.tags;
|
||||
if (Array.isArray(rawTags)) {
|
||||
frontmatter.tags = rawTags.map(String).join(", ");
|
||||
} else if (typeof rawTags === "string") {
|
||||
frontmatter.tags = rawTags;
|
||||
}
|
||||
}
|
||||
if (cache?.headings) {
|
||||
headings = cache.headings.map((h) => h.heading);
|
||||
}
|
||||
if (headings.length > 0) {
|
||||
title = headings[0];
|
||||
}
|
||||
} catch {
|
||||
Logger.warn("Failed to parse frontmatter", "vault-indexer");
|
||||
}
|
||||
}
|
||||
const titleMatch = content.match(/^# (.+)$/m);
|
||||
const title = titleMatch ? titleMatch[1] : "";
|
||||
const headings = [];
|
||||
const headingRegex = /^#{1,6} (.+)$/gm;
|
||||
let headingMatch;
|
||||
while ((headingMatch = headingRegex.exec(content)) !== null) {
|
||||
headings.push(headingMatch[1]);
|
||||
if (Object.keys(frontmatter).length === 0) {
|
||||
const frontmatterRegex = /^---\n([\s\S]*?)\n---\n/;
|
||||
const frontmatterMatch2 = content.match(frontmatterRegex);
|
||||
if (frontmatterMatch2) {
|
||||
try {
|
||||
const lines = frontmatterMatch2[1].trim().split("\n");
|
||||
for (const line of lines) {
|
||||
const [key, ...valueParts] = line.split(":");
|
||||
if (!key) continue;
|
||||
const value = valueParts.join(":").trim();
|
||||
if (key.trim() === "title" && value) frontmatter.title = value;
|
||||
else if (key.trim() === "tags" && value) frontmatter.tags = value;
|
||||
}
|
||||
} catch {
|
||||
Logger.warn("Failed to parse frontmatter", "vault-indexer");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!title) {
|
||||
const titleMatch = content.match(/^# (.+)$/m);
|
||||
title = titleMatch ? titleMatch[1] : "";
|
||||
}
|
||||
if (headings.length === 0) {
|
||||
const headingRegex = /^#{1,6} (.+)$/gm;
|
||||
let headingMatch;
|
||||
while ((headingMatch = headingRegex.exec(content)) !== null) {
|
||||
headings.push(headingMatch[1]);
|
||||
}
|
||||
}
|
||||
const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---\n/);
|
||||
const bodyWithoutFrontmatter = frontmatterMatch ? content.substring(frontmatterMatch[0].length) : content;
|
||||
const bodyText = bodyWithoutFrontmatter.replace(/#{1,6} .+/g, "").replace(/^\s*[\r\n]/gm, "").trim();
|
||||
return { frontmatter, title, headings, content: bodyText };
|
||||
@@ -8923,6 +8972,12 @@ var ToolExecutor = class {
|
||||
case "insert_link":
|
||||
result = await this.handleInsertLink(parsedArgs);
|
||||
break;
|
||||
case "list_vault_tags":
|
||||
result = await this.handleListVaultTags(parsedArgs);
|
||||
break;
|
||||
case "get_vault_stats":
|
||||
result = await this.handleGetVaultStats(parsedArgs);
|
||||
break;
|
||||
default:
|
||||
result = { success: false, message: `Unknown tool: ${toolName}` };
|
||||
}
|
||||
@@ -9217,6 +9272,105 @@ ${lines.join("\n")}
|
||||
await this.vault.delete(file);
|
||||
return { success: true, message: `Note ${path} deleted successfully` };
|
||||
}
|
||||
async handleListVaultTags(args) {
|
||||
const sortBy = args.sortBy === "count" ? "count" : "name";
|
||||
const files = this.vault.getMarkdownFiles();
|
||||
const tagMap = /* @__PURE__ */ new Map();
|
||||
for (const file of files) {
|
||||
try {
|
||||
const cache = this.app.metadataCache.getFileCache(file);
|
||||
const rawTags = cache?.frontmatter?.tags;
|
||||
const tagList = [];
|
||||
if (Array.isArray(rawTags)) {
|
||||
tagList.push(...rawTags.map(String));
|
||||
} else if (typeof rawTags === "string") {
|
||||
tagList.push(
|
||||
...rawTags.split(/[,\n]+/).map((t) => t.trim()).filter((t) => t.length > 0)
|
||||
);
|
||||
}
|
||||
for (const tag of tagList) {
|
||||
const existing = tagMap.get(tag);
|
||||
if (existing) {
|
||||
existing.count++;
|
||||
if (existing.notes.length < 5) existing.notes.push(file.path);
|
||||
} else {
|
||||
tagMap.set(tag, { count: 1, notes: [file.path] });
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
const entries = Array.from(tagMap.entries()).map(([tag, data]) => ({
|
||||
tag,
|
||||
count: data.count,
|
||||
sampleNotes: data.notes.slice(0, 3)
|
||||
}));
|
||||
if (sortBy === "count") {
|
||||
entries.sort((a, b) => b.count - a.count);
|
||||
} else {
|
||||
entries.sort((a, b) => a.tag.localeCompare(b.tag));
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
message: `Found ${entries.length} unique tags across ${files.length} notes`,
|
||||
data: entries
|
||||
};
|
||||
}
|
||||
async handleGetVaultStats(args) {
|
||||
const files = this.vault.getMarkdownFiles();
|
||||
const folderSet = /* @__PURE__ */ new Set();
|
||||
let totalLength = 0;
|
||||
let taggedCount = 0;
|
||||
let untaggedCount = 0;
|
||||
const tagMap = /* @__PURE__ */ new Map();
|
||||
const recentFiles = [];
|
||||
for (const file of files) {
|
||||
try {
|
||||
const folder = file.path.split("/").slice(0, -1).join("/") || "(root)";
|
||||
folderSet.add(folder);
|
||||
const content = await this.vault.cachedRead(file);
|
||||
totalLength += content.length;
|
||||
const cache = this.app.metadataCache.getFileCache(file);
|
||||
const rawTags = cache?.frontmatter?.tags;
|
||||
let hasTags = false;
|
||||
if (Array.isArray(rawTags) && rawTags.length > 0) {
|
||||
hasTags = true;
|
||||
for (const tag of rawTags.map(String)) {
|
||||
tagMap.set(tag, (tagMap.get(tag) ?? 0) + 1);
|
||||
}
|
||||
} else if (typeof rawTags === "string" && rawTags.trim().length > 0 && rawTags.trim() !== "[]") {
|
||||
hasTags = true;
|
||||
for (const tag of rawTags.split(/[,\n]+/).map((t) => t.trim()).filter((t) => t.length > 0)) {
|
||||
tagMap.set(tag, (tagMap.get(tag) ?? 0) + 1);
|
||||
}
|
||||
}
|
||||
if (hasTags) {
|
||||
taggedCount++;
|
||||
} else {
|
||||
untaggedCount++;
|
||||
}
|
||||
if (file.stat?.mtime) {
|
||||
recentFiles.push({ path: file.path, mtime: file.stat.mtime });
|
||||
}
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
recentFiles.sort((a, b) => b.mtime - a.mtime);
|
||||
return {
|
||||
success: true,
|
||||
message: `Vault has ${files.length} notes in ${folderSet.size} folders`,
|
||||
data: {
|
||||
totalNotes: files.length,
|
||||
totalFolders: folderSet.size,
|
||||
folders: Array.from(folderSet).sort(),
|
||||
taggedNotes: taggedCount,
|
||||
untaggedNotes: untaggedCount,
|
||||
topTags: Array.from(tagMap.entries()).sort((a, b) => b[1] - a[1]).slice(0, 20).map(([tag, count]) => ({ tag, count })),
|
||||
avgNoteLength: files.length > 0 ? Math.round(totalLength / files.length) : 0,
|
||||
recentFiles: recentFiles.slice(0, 10).map((f) => f.path)
|
||||
}
|
||||
};
|
||||
}
|
||||
async handleInsertLink(args) {
|
||||
const sourcePath = args.sourcePath;
|
||||
const targetPath = args.targetPath;
|
||||
@@ -9700,6 +9854,7 @@ var VARIABLE_PATTERN = /\{\{([\w.]+)\}\}/g;
|
||||
var WorkflowEngine = class _WorkflowEngine {
|
||||
constructor(vault, app, ollamaUrl, model, options) {
|
||||
this.vaultIndexer = new VaultIndexer(vault);
|
||||
this.vaultIndexer.setApp(app);
|
||||
this.toolExecutor = new ToolExecutor(vault, app, void 0, this.vaultIndexer);
|
||||
this.ollamaClient = new OllamaClient(ollamaUrl, model, void 0, options?.cacheConfig);
|
||||
this.conversationStateManager = new ConversationStateManager();
|
||||
@@ -10748,6 +10903,7 @@ var ChatView = class extends import_obsidian5.ItemView {
|
||||
this.ollamaClient = this.createOllamaClient(settings.chatModel ?? settings.model, settings);
|
||||
this.agentOllamaClient = (settings.agentModel ?? settings.model) === (settings.chatModel ?? settings.model) ? this.ollamaClient : this.createOllamaClient(settings.agentModel ?? settings.model, settings);
|
||||
this.vaultIndexer = new VaultIndexer(this.app.vault, void 0, vectorStore);
|
||||
this.vaultIndexer.setApp(this.app);
|
||||
this.toolExecutor = new ToolExecutor(
|
||||
this.app.vault,
|
||||
this.app,
|
||||
@@ -11272,6 +11428,34 @@ var ChatView = class extends import_obsidian5.ItemView {
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "function",
|
||||
function: {
|
||||
name: "list_vault_tags",
|
||||
description: "Lists all unique tags used across the vault with usage counts. Use this when the user asks about tags, tag organization, or wants to see all tags. Do NOT use search_vault_files for tag listing.",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
sortBy: {
|
||||
type: "string",
|
||||
description: 'Sort by "name" (alphabetical) or "count" (most used first). Default: "name"',
|
||||
enum: ["name", "count"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "function",
|
||||
function: {
|
||||
name: "get_vault_stats",
|
||||
description: "Returns an overview of the vault: total notes, folder structure, tag distribution, average note length, and recent files. Use this when the user asks about vault structure, folder organization, or wants a high-level overview before making organizational suggestions.",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "function",
|
||||
function: {
|
||||
@@ -13182,6 +13366,7 @@ var OllamaPlugin = class extends import_obsidian7.Plugin {
|
||||
);
|
||||
if (!this.autoLinker) {
|
||||
const vaultIndexer = new VaultIndexer(this.app.vault, void 0, this.vaultVectorStore);
|
||||
vaultIndexer.setApp(this.app);
|
||||
this.autoLinker = new AutoLinker(
|
||||
this.app.vault,
|
||||
vaultIndexer,
|
||||
|
||||
Reference in New Issue
Block a user