small improvements

This commit is contained in:
Markus Pfundstein
2024-11-29 17:41:57 +01:00
parent 110873e396
commit f6d0c24674
3 changed files with 57 additions and 3 deletions

View File

@@ -114,4 +114,18 @@ class Obsidian():
response.raise_for_status()
return None
return self._safe_call(call_fn)
def search_json(self, query: dict) -> Any:
url = f"{self.get_base_url()}/search/"
headers = self._get_headers() | {
'Content-Type': 'application/vnd.olrapi.jsonlogic+json'
}
def call_fn():
response = requests.post(url, headers=headers, json=query, verify=self.verify_ssl)
response.raise_for_status()
return response.json()
return self._safe_call(call_fn)

View File

@@ -50,6 +50,7 @@ add_tool_handler(tools.GetFileContentsToolHandler())
add_tool_handler(tools.SearchToolHandler())
add_tool_handler(tools.PatchContentToolHandler())
add_tool_handler(tools.AppendContentToolHandler())
add_tool_handler(tools.ComplexSearchToolHandler())
#@app.list_resources()
#async def list_resources() -> list[Resource]:

View File

@@ -129,18 +129,19 @@ class GetFileContentsToolHandler(ToolHandler):
class SearchToolHandler(ToolHandler):
def __init__(self):
super().__init__("search")
super().__init__("simple_search")
def get_tool_description(self):
return Tool(
name=self.name,
description="Search for documents matching a specified text query across all files in the vault",
description="""Simple search for documents matching a specified text query across all files in the vault.
Use this tool when you want to do a simple text search""",
inputSchema={
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Text to search for in the vault"
"description": "Text to a simple search for in the vault."
},
"context_length": {
"type": "integer",
@@ -285,4 +286,42 @@ class PatchContentToolHandler(ToolHandler):
type="text",
text=f"Successfully patched content in {args['filepath']}"
)
]
class ComplexSearchToolHandler(ToolHandler):
def __init__(self):
super().__init__("complex_search")
def get_tool_description(self):
return Tool(
name=self.name,
description="""Complex search for documents using a JsonLogic query.
Supports standard JsonLogic operators plus 'glob' and 'regexp' for pattern matching. Results must be non-falsy.
Use this tool when you want to do a complex search, e.g. for all documents with certain tags etc.
""",
inputSchema={
"type": "object",
"properties": {
"query": {
"type": "object",
"description": "JsonLogic query object. Example: {\"glob\": [\"*.md\", {\"var\": \"path\"}]} matches all markdown files"
}
},
"required": ["query"]
}
)
def run_tool(self, args: dict) -> Sequence[TextContent | ImageContent | EmbeddedResource]:
if "query" not in args:
raise RuntimeError("query argument missing in arguments")
api = obsidian.Obsidian(api_key=api_key)
results = api.search_json(args["query"])
return [
TextContent(
type="text",
text=json.dumps(results, indent=2)
)
]