basic-memory 0.2.12__py3-none-any.whl → 0.16.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of basic-memory might be problematic. Click here for more details.

Files changed (149) hide show
  1. basic_memory/__init__.py +5 -1
  2. basic_memory/alembic/alembic.ini +119 -0
  3. basic_memory/alembic/env.py +27 -3
  4. basic_memory/alembic/migrations.py +4 -9
  5. basic_memory/alembic/versions/502b60eaa905_remove_required_from_entity_permalink.py +51 -0
  6. basic_memory/alembic/versions/5fe1ab1ccebe_add_projects_table.py +108 -0
  7. basic_memory/alembic/versions/647e7a75e2cd_project_constraint_fix.py +104 -0
  8. basic_memory/alembic/versions/9d9c1cb7d8f5_add_mtime_and_size_columns_to_entity_.py +49 -0
  9. basic_memory/alembic/versions/a1b2c3d4e5f6_fix_project_foreign_keys.py +49 -0
  10. basic_memory/alembic/versions/b3c3938bacdb_relation_to_name_unique_index.py +44 -0
  11. basic_memory/alembic/versions/cc7172b46608_update_search_index_schema.py +100 -0
  12. basic_memory/alembic/versions/e7e1f4367280_add_scan_watermark_tracking_to_project.py +37 -0
  13. basic_memory/api/app.py +63 -31
  14. basic_memory/api/routers/__init__.py +4 -1
  15. basic_memory/api/routers/directory_router.py +84 -0
  16. basic_memory/api/routers/importer_router.py +152 -0
  17. basic_memory/api/routers/knowledge_router.py +165 -28
  18. basic_memory/api/routers/management_router.py +80 -0
  19. basic_memory/api/routers/memory_router.py +28 -67
  20. basic_memory/api/routers/project_router.py +406 -0
  21. basic_memory/api/routers/prompt_router.py +260 -0
  22. basic_memory/api/routers/resource_router.py +219 -14
  23. basic_memory/api/routers/search_router.py +21 -13
  24. basic_memory/api/routers/utils.py +130 -0
  25. basic_memory/api/template_loader.py +292 -0
  26. basic_memory/cli/app.py +52 -1
  27. basic_memory/cli/auth.py +277 -0
  28. basic_memory/cli/commands/__init__.py +13 -2
  29. basic_memory/cli/commands/cloud/__init__.py +6 -0
  30. basic_memory/cli/commands/cloud/api_client.py +112 -0
  31. basic_memory/cli/commands/cloud/bisync_commands.py +110 -0
  32. basic_memory/cli/commands/cloud/cloud_utils.py +101 -0
  33. basic_memory/cli/commands/cloud/core_commands.py +195 -0
  34. basic_memory/cli/commands/cloud/rclone_commands.py +301 -0
  35. basic_memory/cli/commands/cloud/rclone_config.py +110 -0
  36. basic_memory/cli/commands/cloud/rclone_installer.py +249 -0
  37. basic_memory/cli/commands/cloud/upload.py +233 -0
  38. basic_memory/cli/commands/cloud/upload_command.py +124 -0
  39. basic_memory/cli/commands/command_utils.py +51 -0
  40. basic_memory/cli/commands/db.py +26 -7
  41. basic_memory/cli/commands/import_chatgpt.py +83 -0
  42. basic_memory/cli/commands/import_claude_conversations.py +86 -0
  43. basic_memory/cli/commands/import_claude_projects.py +85 -0
  44. basic_memory/cli/commands/import_memory_json.py +35 -92
  45. basic_memory/cli/commands/mcp.py +84 -10
  46. basic_memory/cli/commands/project.py +876 -0
  47. basic_memory/cli/commands/status.py +47 -30
  48. basic_memory/cli/commands/tool.py +341 -0
  49. basic_memory/cli/main.py +13 -6
  50. basic_memory/config.py +481 -22
  51. basic_memory/db.py +192 -32
  52. basic_memory/deps.py +252 -22
  53. basic_memory/file_utils.py +113 -58
  54. basic_memory/ignore_utils.py +297 -0
  55. basic_memory/importers/__init__.py +27 -0
  56. basic_memory/importers/base.py +79 -0
  57. basic_memory/importers/chatgpt_importer.py +232 -0
  58. basic_memory/importers/claude_conversations_importer.py +177 -0
  59. basic_memory/importers/claude_projects_importer.py +148 -0
  60. basic_memory/importers/memory_json_importer.py +108 -0
  61. basic_memory/importers/utils.py +58 -0
  62. basic_memory/markdown/entity_parser.py +143 -23
  63. basic_memory/markdown/markdown_processor.py +3 -3
  64. basic_memory/markdown/plugins.py +39 -21
  65. basic_memory/markdown/schemas.py +1 -1
  66. basic_memory/markdown/utils.py +28 -13
  67. basic_memory/mcp/async_client.py +134 -4
  68. basic_memory/mcp/project_context.py +141 -0
  69. basic_memory/mcp/prompts/__init__.py +19 -0
  70. basic_memory/mcp/prompts/ai_assistant_guide.py +70 -0
  71. basic_memory/mcp/prompts/continue_conversation.py +62 -0
  72. basic_memory/mcp/prompts/recent_activity.py +188 -0
  73. basic_memory/mcp/prompts/search.py +57 -0
  74. basic_memory/mcp/prompts/utils.py +162 -0
  75. basic_memory/mcp/resources/ai_assistant_guide.md +283 -0
  76. basic_memory/mcp/resources/project_info.py +71 -0
  77. basic_memory/mcp/server.py +7 -13
  78. basic_memory/mcp/tools/__init__.py +33 -21
  79. basic_memory/mcp/tools/build_context.py +120 -0
  80. basic_memory/mcp/tools/canvas.py +130 -0
  81. basic_memory/mcp/tools/chatgpt_tools.py +187 -0
  82. basic_memory/mcp/tools/delete_note.py +225 -0
  83. basic_memory/mcp/tools/edit_note.py +320 -0
  84. basic_memory/mcp/tools/list_directory.py +167 -0
  85. basic_memory/mcp/tools/move_note.py +545 -0
  86. basic_memory/mcp/tools/project_management.py +200 -0
  87. basic_memory/mcp/tools/read_content.py +271 -0
  88. basic_memory/mcp/tools/read_note.py +255 -0
  89. basic_memory/mcp/tools/recent_activity.py +534 -0
  90. basic_memory/mcp/tools/search.py +369 -14
  91. basic_memory/mcp/tools/utils.py +374 -16
  92. basic_memory/mcp/tools/view_note.py +77 -0
  93. basic_memory/mcp/tools/write_note.py +207 -0
  94. basic_memory/models/__init__.py +3 -2
  95. basic_memory/models/knowledge.py +67 -15
  96. basic_memory/models/project.py +87 -0
  97. basic_memory/models/search.py +10 -6
  98. basic_memory/repository/__init__.py +2 -0
  99. basic_memory/repository/entity_repository.py +229 -7
  100. basic_memory/repository/observation_repository.py +35 -3
  101. basic_memory/repository/project_info_repository.py +10 -0
  102. basic_memory/repository/project_repository.py +103 -0
  103. basic_memory/repository/relation_repository.py +21 -2
  104. basic_memory/repository/repository.py +147 -29
  105. basic_memory/repository/search_repository.py +437 -59
  106. basic_memory/schemas/__init__.py +22 -9
  107. basic_memory/schemas/base.py +97 -8
  108. basic_memory/schemas/cloud.py +50 -0
  109. basic_memory/schemas/directory.py +30 -0
  110. basic_memory/schemas/importer.py +35 -0
  111. basic_memory/schemas/memory.py +188 -23
  112. basic_memory/schemas/project_info.py +211 -0
  113. basic_memory/schemas/prompt.py +90 -0
  114. basic_memory/schemas/request.py +57 -3
  115. basic_memory/schemas/response.py +9 -1
  116. basic_memory/schemas/search.py +33 -35
  117. basic_memory/schemas/sync_report.py +72 -0
  118. basic_memory/services/__init__.py +2 -1
  119. basic_memory/services/context_service.py +251 -106
  120. basic_memory/services/directory_service.py +295 -0
  121. basic_memory/services/entity_service.py +595 -60
  122. basic_memory/services/exceptions.py +21 -0
  123. basic_memory/services/file_service.py +284 -30
  124. basic_memory/services/initialization.py +191 -0
  125. basic_memory/services/link_resolver.py +50 -56
  126. basic_memory/services/project_service.py +863 -0
  127. basic_memory/services/search_service.py +172 -34
  128. basic_memory/sync/__init__.py +3 -2
  129. basic_memory/sync/background_sync.py +26 -0
  130. basic_memory/sync/sync_service.py +1176 -96
  131. basic_memory/sync/watch_service.py +412 -135
  132. basic_memory/templates/prompts/continue_conversation.hbs +110 -0
  133. basic_memory/templates/prompts/search.hbs +101 -0
  134. basic_memory/utils.py +388 -28
  135. basic_memory-0.16.1.dist-info/METADATA +493 -0
  136. basic_memory-0.16.1.dist-info/RECORD +148 -0
  137. {basic_memory-0.2.12.dist-info → basic_memory-0.16.1.dist-info}/entry_points.txt +1 -0
  138. basic_memory/alembic/README +0 -1
  139. basic_memory/cli/commands/sync.py +0 -203
  140. basic_memory/mcp/tools/knowledge.py +0 -56
  141. basic_memory/mcp/tools/memory.py +0 -151
  142. basic_memory/mcp/tools/notes.py +0 -122
  143. basic_memory/schemas/discovery.py +0 -28
  144. basic_memory/sync/file_change_scanner.py +0 -158
  145. basic_memory/sync/utils.py +0 -34
  146. basic_memory-0.2.12.dist-info/METADATA +0 -291
  147. basic_memory-0.2.12.dist-info/RECORD +0 -78
  148. {basic_memory-0.2.12.dist-info → basic_memory-0.16.1.dist-info}/WHEEL +0 -0
  149. {basic_memory-0.2.12.dist-info → basic_memory-0.16.1.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,255 @@
1
+ """Read note tool for Basic Memory MCP server."""
2
+
3
+ from textwrap import dedent
4
+ from typing import Optional
5
+
6
+ from loguru import logger
7
+ from fastmcp import Context
8
+
9
+ from basic_memory.mcp.async_client import get_client
10
+ from basic_memory.mcp.project_context import get_active_project
11
+ from basic_memory.mcp.server import mcp
12
+ from basic_memory.mcp.tools.search import search_notes
13
+ from basic_memory.mcp.tools.utils import call_get
14
+ from basic_memory.schemas.memory import memory_url_path
15
+ from basic_memory.utils import validate_project_path
16
+
17
+
18
+ @mcp.tool(
19
+ description="Read a markdown note by title or permalink.",
20
+ )
21
+ async def read_note(
22
+ identifier: str,
23
+ project: Optional[str] = None,
24
+ page: int = 1,
25
+ page_size: int = 10,
26
+ context: Context | None = None,
27
+ ) -> str:
28
+ """Return the raw markdown for a note, or guidance text if no match is found.
29
+
30
+ Finds and retrieves a note by its title, permalink, or content search,
31
+ returning the raw markdown content including observations, relations, and metadata.
32
+
33
+ Project Resolution:
34
+ Server resolves projects in this order: Single Project Mode → project parameter → default project.
35
+ If project unknown, use list_memory_projects() or recent_activity() first.
36
+
37
+ This tool will try multiple lookup strategies to find the most relevant note:
38
+ 1. Direct permalink lookup
39
+ 2. Title search fallback
40
+ 3. Text search as last resort
41
+
42
+ Args:
43
+ project: Project name to read from. Optional - server will resolve using the
44
+ hierarchy above. If unknown, use list_memory_projects() to discover
45
+ available projects.
46
+ identifier: The title or permalink of the note to read
47
+ Can be a full memory:// URL, a permalink, a title, or search text
48
+ page: Page number for paginated results (default: 1)
49
+ page_size: Number of items per page (default: 10)
50
+ context: Optional FastMCP context for performance caching.
51
+
52
+ Returns:
53
+ The full markdown content of the note if found, or helpful guidance if not found.
54
+ Content includes frontmatter, observations, relations, and all markdown formatting.
55
+
56
+ Examples:
57
+ # Read by permalink
58
+ read_note("my-research", "specs/search-spec")
59
+
60
+ # Read by title
61
+ read_note("work-project", "Search Specification")
62
+
63
+ # Read with memory URL
64
+ read_note("my-research", "memory://specs/search-spec")
65
+
66
+ # Read with pagination
67
+ read_note("work-project", "Project Updates", page=2, page_size=5)
68
+
69
+ # Read recent meeting notes
70
+ read_note("team-docs", "Weekly Standup")
71
+
72
+ Raises:
73
+ HTTPError: If project doesn't exist or is inaccessible
74
+ SecurityError: If identifier attempts path traversal
75
+
76
+ Note:
77
+ If the exact note isn't found, this tool provides helpful suggestions
78
+ including related notes, search commands, and note creation templates.
79
+ """
80
+ async with get_client() as client:
81
+ # Get and validate the project
82
+ active_project = await get_active_project(client, project, context)
83
+
84
+ # Validate identifier to prevent path traversal attacks
85
+ # We need to check both the raw identifier and the processed path
86
+ processed_path = memory_url_path(identifier)
87
+ project_path = active_project.home
88
+
89
+ if not validate_project_path(identifier, project_path) or not validate_project_path(
90
+ processed_path, project_path
91
+ ):
92
+ logger.warning(
93
+ "Attempted path traversal attack blocked",
94
+ identifier=identifier,
95
+ processed_path=processed_path,
96
+ project=active_project.name,
97
+ )
98
+ return f"# Error\n\nIdentifier '{identifier}' is not allowed - paths must stay within project boundaries"
99
+
100
+ project_url = active_project.project_url
101
+
102
+ # Get the file via REST API - first try direct permalink lookup
103
+ entity_path = memory_url_path(identifier)
104
+ path = f"{project_url}/resource/{entity_path}"
105
+ logger.info(f"Attempting to read note from Project: {active_project.name} URL: {path}")
106
+
107
+ try:
108
+ # Try direct lookup first
109
+ response = await call_get(client, path, params={"page": page, "page_size": page_size})
110
+
111
+ # If successful, return the content
112
+ if response.status_code == 200:
113
+ logger.info("Returning read_note result from resource: {path}", path=entity_path)
114
+ return response.text
115
+ except Exception as e: # pragma: no cover
116
+ logger.info(f"Direct lookup failed for '{path}': {e}")
117
+ # Continue to fallback methods
118
+
119
+ # Fallback 1: Try title search via API
120
+ logger.info(f"Search title for: {identifier}")
121
+ title_results = await search_notes.fn(
122
+ query=identifier, search_type="title", project=project, context=context
123
+ )
124
+
125
+ # Handle both SearchResponse object and error strings
126
+ if title_results and hasattr(title_results, "results") and title_results.results:
127
+ result = title_results.results[0] # Get the first/best match
128
+ if result.permalink:
129
+ try:
130
+ # Try to fetch the content using the found permalink
131
+ path = f"{project_url}/resource/{result.permalink}"
132
+ response = await call_get(
133
+ client, path, params={"page": page, "page_size": page_size}
134
+ )
135
+
136
+ if response.status_code == 200:
137
+ logger.info(f"Found note by title search: {result.permalink}")
138
+ return response.text
139
+ except Exception as e: # pragma: no cover
140
+ logger.info(
141
+ f"Failed to fetch content for found title match {result.permalink}: {e}"
142
+ )
143
+ else:
144
+ logger.info(
145
+ f"No results in title search for: {identifier} in project {active_project.name}"
146
+ )
147
+
148
+ # Fallback 2: Text search as a last resort
149
+ logger.info(f"Title search failed, trying text search for: {identifier}")
150
+ text_results = await search_notes.fn(
151
+ query=identifier, search_type="text", project=project, context=context
152
+ )
153
+
154
+ # We didn't find a direct match, construct a helpful error message
155
+ # Handle both SearchResponse object and error strings
156
+ if not text_results or not hasattr(text_results, "results") or not text_results.results:
157
+ # No results at all
158
+ return format_not_found_message(active_project.name, identifier)
159
+ else:
160
+ # We found some related results
161
+ return format_related_results(active_project.name, identifier, text_results.results[:5])
162
+
163
+
164
+ def format_not_found_message(project: str | None, identifier: str) -> str:
165
+ """Format a helpful message when no note was found."""
166
+ return dedent(f"""
167
+ # Note Not Found in {project}: "{identifier}"
168
+
169
+ I couldn't find any notes matching "{identifier}". Here are some suggestions:
170
+
171
+ ## Check Identifier Type
172
+ - If you provided a title, try using the exact permalink instead
173
+ - If you provided a permalink, check for typos or try a broader search
174
+
175
+ ## Search Instead
176
+ Try searching for related content:
177
+ ```
178
+ search_notes(project="{project}", query="{identifier}")
179
+ ```
180
+
181
+ ## Recent Activity
182
+ Check recently modified notes:
183
+ ```
184
+ recent_activity(timeframe="7d")
185
+ ```
186
+
187
+ ## Create New Note
188
+ This might be a good opportunity to create a new note on this topic:
189
+ ```
190
+ write_note(
191
+ project="{project}",
192
+ title="{identifier.capitalize()}",
193
+ content='''
194
+ # {identifier.capitalize()}
195
+
196
+ ## Overview
197
+ [Your content here]
198
+
199
+ ## Observations
200
+ - [category] [Observation about {identifier}]
201
+
202
+ ## Relations
203
+ - relates_to [[Related Topic]]
204
+ ''',
205
+ folder="notes"
206
+ )
207
+ ```
208
+ """)
209
+
210
+
211
+ def format_related_results(project: str | None, identifier: str, results) -> str:
212
+ """Format a helpful message with related results when an exact match wasn't found."""
213
+ message = dedent(f"""
214
+ # Note Not Found in {project}: "{identifier}"
215
+
216
+ I couldn't find an exact match for "{identifier}", but I found some related notes:
217
+
218
+ """)
219
+
220
+ for i, result in enumerate(results):
221
+ message += dedent(f"""
222
+ ## {i + 1}. {result.title}
223
+ - **Type**: {result.type.value}
224
+ - **Permalink**: {result.permalink}
225
+
226
+ You can read this note with:
227
+ ```
228
+ read_note(project="{project}", {result.permalink}")
229
+ ```
230
+
231
+ """)
232
+
233
+ message += dedent(f"""
234
+ ## Try More Specific Lookup
235
+ For exact matches, try using the full permalink from one of the results above.
236
+
237
+ ## Search For More Results
238
+ To see more related content:
239
+ ```
240
+ search_notes(project="{project}", query="{identifier}")
241
+ ```
242
+
243
+ ## Create New Note
244
+ If none of these match what you're looking for, consider creating a new note:
245
+ ```
246
+ write_note(
247
+ project="{project}",
248
+ title="[Your title]",
249
+ content="[Your content]",
250
+ folder="notes"
251
+ )
252
+ ```
253
+ """)
254
+
255
+ return message