added get batch of file contents

This commit is contained in:
joe
2025-02-01 12:15:03 -05:00
parent 33c931280f
commit 6b02ecdf71
4 changed files with 122 additions and 4 deletions

View File

@@ -72,6 +72,27 @@ class Obsidian():
return self._safe_call(call_fn)
def get_batch_file_contents(self, filepaths: list[str]) -> str:
"""Get contents of multiple files and concatenate them with headers.
Args:
filepaths: List of file paths to read
Returns:
String containing all file contents with headers
"""
result = []
for filepath in filepaths:
try:
content = self.get_file_contents(filepath)
result.append(f"# {filepath}\n\n{content}\n\n---\n\n")
except Exception as e:
# Add error message but continue processing other files
result.append(f"# {filepath}\n\nError reading file: {str(e)}\n\n---\n\n")
return "".join(result)
def search(self, query: str, context_length: int = 100) -> Any:
url = f"{self.get_base_url()}/search/simple/"
params = {

View File

@@ -1,4 +1,3 @@
import json
import logging
from collections.abc import Sequence
@@ -49,6 +48,7 @@ add_tool_handler(tools.SearchToolHandler())
add_tool_handler(tools.PatchContentToolHandler())
add_tool_handler(tools.AppendContentToolHandler())
add_tool_handler(tools.ComplexSearchToolHandler())
add_tool_handler(tools.BatchGetFileContentsToolHandler())
@app.list_tools()
async def list_tools() -> list[Tool]:

View File

@@ -323,4 +323,43 @@ class ComplexSearchToolHandler(ToolHandler):
type="text",
text=json.dumps(results, indent=2)
)
]
]
class BatchGetFileContentsToolHandler(ToolHandler):
def __init__(self):
super().__init__("obsidian_batch_get_file_contents")
def get_tool_description(self):
return Tool(
name=self.name,
description="Return the contents of multiple files in your vault, concatenated with headers.",
inputSchema={
"type": "object",
"properties": {
"filepaths": {
"type": "array",
"items": {
"type": "string",
"description": "Path to a file (relative to your vault root)",
"format": "path"
},
"description": "List of file paths to read"
},
},
"required": ["filepaths"]
}
)
def run_tool(self, args: dict) -> Sequence[TextContent | ImageContent | EmbeddedResource]:
if "filepaths" not in args:
raise RuntimeError("filepaths argument missing in arguments")
api = obsidian.Obsidian(api_key=api_key)
content = api.get_batch_file_contents(args["filepaths"])
return [
TextContent(
type="text",
text=content
)
]