-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsemantic_fs.py
More file actions
349 lines (304 loc) · 11.9 KB
/
semantic_fs.py
File metadata and controls
349 lines (304 loc) · 11.9 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
from __future__ import annotations
from typing import Optional, Any, Callable
from mixedbread import Mixedbread
from datetime import datetime
import json
class SemanticFS:
"""
A semantic filesystem backed by mixedbread.
Paths like:
/todos/buy-groceries.md
/memories/user.md
/tools/add_todo.md
Supports CRUD operations and semantic search across all or filtered paths.
"""
def __init__(self, api_key: str, store_name: str = "bernd"):
self.mixedbread = Mixedbread(api_key=api_key)
self.store_name = store_name
self._setup_store()
# For toolbox functionality
self._tool_handlers: dict[str, Callable] = {}
self._tool_schemas: dict[str, dict] = {}
def _setup_store(self):
"""Create store if it doesn't exist."""
store = self.mixedbread.stores.list(q=self.store_name)
if not store.data:
self.mixedbread.stores.create(
name=self.store_name,
description="Semantic filesystem for Bernd",
config={
"contextualization": {
"with_metadata": [
"type",
"created_at",
"updated_at",
"priority",
"status",
"due_date",
"tags",
]
}
},
)
def _path_to_id(self, path: str) -> str:
"""Convert path to file identifier."""
path = path.strip()
if path.startswith("/"):
path = path[1:]
return path.replace("/", "__")
def _id_to_path(self, file_id: str) -> str:
"""Convert file identifier back to path."""
return "/" + file_id.replace("__", "/")
# Core filesystem operations
def write(self, path: str, content: str, metadata: Optional[dict] = None) -> dict:
"""Write content to a path."""
file_id = self._path_to_id(path)
now = datetime.now().isoformat()
# Preserve created_at if file exists
created_at = now
try:
existing = self.mixedbread.stores.files.get(
file_identifier=file_id,
store_identifier=self.store_name,
)
created_at = existing.metadata.get("created_at", now)
self.mixedbread.stores.files.delete(
file_identifier=file_id,
store_identifier=self.store_name,
)
except Exception:
pass
meta = metadata or {}
meta["path"] = path # Store path for search retrieval
meta["created_at"] = created_at
meta["updated_at"] = now
filename = path.split("/")[-1]
mime_type = "text/plain" if filename.endswith(".json") else "text/markdown"
self.mixedbread.stores.files.upload(
store_identifier=self.store_name,
file=(filename, content, mime_type),
metadata=meta,
external_id=file_id,
overwrite=True,
)
return {"status": "ok", "path": path}
def read(self, path: str) -> dict:
"""Read content from a path."""
file_id = self._path_to_id(path)
try:
# Get file metadata
resp = self.mixedbread.stores.files.retrieve(
file_identifier=file_id,
store_identifier=self.store_name,
)
# Download actual content
content_resp = self.mixedbread.files.content(file_id=resp.id)
content = content_resp.read().decode("utf-8")
return {
"path": path,
"content": content,
"metadata": resp.metadata if hasattr(resp, "metadata") else {},
}
except Exception as e:
print(f"[SemanticFS] Error reading {path}: {e}")
return {"error": f"Not found: {path}"}
def delete(self, path: str) -> dict:
"""Delete a file at path."""
file_id = self._path_to_id(path)
try:
self.mixedbread.stores.files.delete(
file_identifier=file_id,
store_identifier=self.store_name,
)
return {"status": "deleted", "path": path}
except Exception:
return {"error": f"Not found: {path}"}
def list(self, prefix: str = "/", limit: int = 100) -> list[dict]:
"""List files under a path prefix."""
prefix_id = self._path_to_id(prefix) if prefix != "/" else ""
files = []
cursor = None
while len(files) < limit:
resp = self.mixedbread.stores.files.list(
store_identifier=self.store_name,
limit=min(100, limit - len(files)),
after=cursor,
)
for item in resp.data:
file_id = item.external_id or ""
if prefix == "/" or file_id.startswith(prefix_id):
files.append(
{
"path": self._id_to_path(file_id),
"metadata": item.metadata
if hasattr(item, "metadata")
else {},
}
)
if not resp.pagination.has_more:
break
cursor = resp.pagination.last_cursor
return files
def search(self, query: str, prefix: str = "/", top_k: int = 10) -> list[dict]:
"""Semantic search, optionally filtered by path prefix."""
# Build metadata filter for prefix
metadata_filter = None
if prefix != "/":
metadata_filter = {
"key": "path",
"operator": "starts_with",
"value": prefix,
}
try:
resp = self.mixedbread.stores.search(
store_identifiers=[self.store_name],
query=query,
top_k=top_k,
filters=metadata_filter,
)
except Exception as e:
print(f"[SemanticFS] Search error: {e}")
return []
results = []
for item in resp.data:
metadata = item.metadata if hasattr(item, "metadata") else {}
path = metadata.get("path", "/" + (item.filename or ""))
results.append(
{
"path": path,
"content": item.text if hasattr(item, "text") else "",
"score": item.score,
"metadata": metadata,
}
)
return results
def clear_prefix(self, prefix: str) -> dict:
"""Delete all files under a prefix."""
files = self.list(prefix=prefix, limit=1000)
deleted = 0
for f in files:
self.delete(f["path"])
deleted += 1
return {"status": "cleared", "prefix": prefix, "deleted": deleted}
def write_binary(self, path: str, data: bytes, mime_type: str, metadata: Optional[dict] = None) -> dict:
"""Write binary data (like images) to a path."""
file_id = self._path_to_id(path)
now = datetime.now().isoformat()
# Delete existing file if present
try:
self.mixedbread.stores.files.delete(
file_identifier=file_id,
store_identifier=self.store_name,
)
except Exception:
pass
meta = metadata or {}
meta["path"] = path
meta["created_at"] = now
meta["type"] = "binary"
filename = path.split("/")[-1]
self.mixedbread.stores.files.upload(
store_identifier=self.store_name,
file=(filename, data, mime_type),
metadata=meta,
external_id=file_id,
overwrite=True,
)
return {"status": "ok", "path": path}
def read_binary(self, path: str) -> dict:
"""Read binary data from a path."""
file_id = self._path_to_id(path)
try:
resp = self.mixedbread.stores.files.retrieve(
file_identifier=file_id,
store_identifier=self.store_name,
)
content_resp = self.mixedbread.files.content(file_id=resp.id)
data = content_resp.read()
return {
"path": path,
"data": data,
"metadata": resp.metadata if hasattr(resp, "metadata") else {},
}
except Exception as e:
print(f"[SemanticFS] Error reading binary {path}: {e}")
return {"error": f"Not found: {path}"}
# Toolbox functionality (tools are stored at /tools/*)
def register_tool(
self, name: str, description: str, parameters: dict, handler: Callable
):
"""Register a tool, storing its schema in the filesystem."""
self._tool_handlers[name] = handler
self._tool_schemas[name] = {
"name": name,
"description": description,
"parameters": parameters,
}
# Store in filesystem for semantic search
content = f"Tool: {name}\n\n{description}\n\nParameters:\n{json.dumps(parameters, indent=2)}"
self.write(f"/tools/{name}.md", content, {"type": "tool"})
def search_tools(self, query: str, top_k: int = 5) -> list[dict]:
"""Search for tools by intent."""
results = self.search(query, prefix="/tools", top_k=top_k)
tools = []
for r in results:
name = r["path"].split("/")[-1].replace(".md", "")
if name in self._tool_schemas:
tools.append(
{
"name": name,
"description": self._tool_schemas[name]["description"],
"score": r["score"],
}
)
return tools
def get_tool(self, name: str) -> dict:
"""Get tool schema."""
if name not in self._tool_schemas:
return {"error": f"Tool not found: {name}"}
return self._tool_schemas[name]
def use_tool(self, name: str, arguments: dict) -> Any:
"""Execute a tool."""
if name not in self._tool_handlers:
return {"error": f"Tool not found: {name}"}
try:
return self._tool_handlers[name](arguments)
except Exception as e:
return {"error": str(e)}
def get_toolbox_schema(self) -> list[dict]:
"""Get the toolbox tool definition for the model."""
return [
{
"type": "function",
"name": "toolbox",
"description": """Access the toolbox to find and use tools.
Actions:
- search: Find tools by describing what you want to do
- get: Get full parameter details for a tool
- use: Execute a tool with arguments
Use 'search' to find the right tool, 'get' to see its parameters, then 'use' to execute.""",
"parameters": {
"type": "object",
"properties": {
"action": {
"type": "string",
"enum": ["search", "get", "use"],
"description": "The action to perform",
},
"query": {
"type": "string",
"description": "Search query (for 'search' action)",
},
"tool_name": {
"type": "string",
"description": "Tool name (for 'get' and 'use' actions)",
},
"arguments": {
"type": "object",
"description": "Arguments for the tool (for 'use' action)",
},
},
"required": ["action"],
},
},
]