basic-memory 0.17.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.
- basic_memory/__init__.py +7 -0
- basic_memory/alembic/alembic.ini +119 -0
- basic_memory/alembic/env.py +185 -0
- basic_memory/alembic/migrations.py +24 -0
- basic_memory/alembic/script.py.mako +26 -0
- basic_memory/alembic/versions/314f1ea54dc4_add_postgres_full_text_search_support_.py +131 -0
- basic_memory/alembic/versions/3dae7c7b1564_initial_schema.py +93 -0
- basic_memory/alembic/versions/502b60eaa905_remove_required_from_entity_permalink.py +51 -0
- basic_memory/alembic/versions/5fe1ab1ccebe_add_projects_table.py +120 -0
- basic_memory/alembic/versions/647e7a75e2cd_project_constraint_fix.py +112 -0
- basic_memory/alembic/versions/9d9c1cb7d8f5_add_mtime_and_size_columns_to_entity_.py +49 -0
- basic_memory/alembic/versions/a1b2c3d4e5f6_fix_project_foreign_keys.py +49 -0
- basic_memory/alembic/versions/a2b3c4d5e6f7_add_search_index_entity_cascade.py +56 -0
- basic_memory/alembic/versions/b3c3938bacdb_relation_to_name_unique_index.py +44 -0
- basic_memory/alembic/versions/cc7172b46608_update_search_index_schema.py +113 -0
- basic_memory/alembic/versions/e7e1f4367280_add_scan_watermark_tracking_to_project.py +37 -0
- basic_memory/alembic/versions/f8a9b2c3d4e5_add_pg_trgm_for_fuzzy_link_resolution.py +239 -0
- basic_memory/api/__init__.py +5 -0
- basic_memory/api/app.py +131 -0
- basic_memory/api/routers/__init__.py +11 -0
- basic_memory/api/routers/directory_router.py +84 -0
- basic_memory/api/routers/importer_router.py +152 -0
- basic_memory/api/routers/knowledge_router.py +318 -0
- basic_memory/api/routers/management_router.py +80 -0
- basic_memory/api/routers/memory_router.py +90 -0
- basic_memory/api/routers/project_router.py +448 -0
- basic_memory/api/routers/prompt_router.py +260 -0
- basic_memory/api/routers/resource_router.py +249 -0
- basic_memory/api/routers/search_router.py +36 -0
- basic_memory/api/routers/utils.py +169 -0
- basic_memory/api/template_loader.py +292 -0
- basic_memory/api/v2/__init__.py +35 -0
- basic_memory/api/v2/routers/__init__.py +21 -0
- basic_memory/api/v2/routers/directory_router.py +93 -0
- basic_memory/api/v2/routers/importer_router.py +182 -0
- basic_memory/api/v2/routers/knowledge_router.py +413 -0
- basic_memory/api/v2/routers/memory_router.py +130 -0
- basic_memory/api/v2/routers/project_router.py +342 -0
- basic_memory/api/v2/routers/prompt_router.py +270 -0
- basic_memory/api/v2/routers/resource_router.py +286 -0
- basic_memory/api/v2/routers/search_router.py +73 -0
- basic_memory/cli/__init__.py +1 -0
- basic_memory/cli/app.py +84 -0
- basic_memory/cli/auth.py +277 -0
- basic_memory/cli/commands/__init__.py +18 -0
- basic_memory/cli/commands/cloud/__init__.py +6 -0
- basic_memory/cli/commands/cloud/api_client.py +112 -0
- basic_memory/cli/commands/cloud/bisync_commands.py +110 -0
- basic_memory/cli/commands/cloud/cloud_utils.py +101 -0
- basic_memory/cli/commands/cloud/core_commands.py +195 -0
- basic_memory/cli/commands/cloud/rclone_commands.py +371 -0
- basic_memory/cli/commands/cloud/rclone_config.py +110 -0
- basic_memory/cli/commands/cloud/rclone_installer.py +263 -0
- basic_memory/cli/commands/cloud/upload.py +233 -0
- basic_memory/cli/commands/cloud/upload_command.py +124 -0
- basic_memory/cli/commands/command_utils.py +77 -0
- basic_memory/cli/commands/db.py +44 -0
- basic_memory/cli/commands/format.py +198 -0
- basic_memory/cli/commands/import_chatgpt.py +84 -0
- basic_memory/cli/commands/import_claude_conversations.py +87 -0
- basic_memory/cli/commands/import_claude_projects.py +86 -0
- basic_memory/cli/commands/import_memory_json.py +87 -0
- basic_memory/cli/commands/mcp.py +76 -0
- basic_memory/cli/commands/project.py +889 -0
- basic_memory/cli/commands/status.py +174 -0
- basic_memory/cli/commands/telemetry.py +81 -0
- basic_memory/cli/commands/tool.py +341 -0
- basic_memory/cli/main.py +28 -0
- basic_memory/config.py +616 -0
- basic_memory/db.py +394 -0
- basic_memory/deps.py +705 -0
- basic_memory/file_utils.py +478 -0
- basic_memory/ignore_utils.py +297 -0
- basic_memory/importers/__init__.py +27 -0
- basic_memory/importers/base.py +79 -0
- basic_memory/importers/chatgpt_importer.py +232 -0
- basic_memory/importers/claude_conversations_importer.py +180 -0
- basic_memory/importers/claude_projects_importer.py +148 -0
- basic_memory/importers/memory_json_importer.py +108 -0
- basic_memory/importers/utils.py +61 -0
- basic_memory/markdown/__init__.py +21 -0
- basic_memory/markdown/entity_parser.py +279 -0
- basic_memory/markdown/markdown_processor.py +160 -0
- basic_memory/markdown/plugins.py +242 -0
- basic_memory/markdown/schemas.py +70 -0
- basic_memory/markdown/utils.py +117 -0
- basic_memory/mcp/__init__.py +1 -0
- basic_memory/mcp/async_client.py +139 -0
- basic_memory/mcp/project_context.py +141 -0
- basic_memory/mcp/prompts/__init__.py +19 -0
- basic_memory/mcp/prompts/ai_assistant_guide.py +70 -0
- basic_memory/mcp/prompts/continue_conversation.py +62 -0
- basic_memory/mcp/prompts/recent_activity.py +188 -0
- basic_memory/mcp/prompts/search.py +57 -0
- basic_memory/mcp/prompts/utils.py +162 -0
- basic_memory/mcp/resources/ai_assistant_guide.md +283 -0
- basic_memory/mcp/resources/project_info.py +71 -0
- basic_memory/mcp/server.py +81 -0
- basic_memory/mcp/tools/__init__.py +48 -0
- basic_memory/mcp/tools/build_context.py +120 -0
- basic_memory/mcp/tools/canvas.py +152 -0
- basic_memory/mcp/tools/chatgpt_tools.py +190 -0
- basic_memory/mcp/tools/delete_note.py +242 -0
- basic_memory/mcp/tools/edit_note.py +324 -0
- basic_memory/mcp/tools/list_directory.py +168 -0
- basic_memory/mcp/tools/move_note.py +551 -0
- basic_memory/mcp/tools/project_management.py +201 -0
- basic_memory/mcp/tools/read_content.py +281 -0
- basic_memory/mcp/tools/read_note.py +267 -0
- basic_memory/mcp/tools/recent_activity.py +534 -0
- basic_memory/mcp/tools/search.py +385 -0
- basic_memory/mcp/tools/utils.py +540 -0
- basic_memory/mcp/tools/view_note.py +78 -0
- basic_memory/mcp/tools/write_note.py +230 -0
- basic_memory/models/__init__.py +15 -0
- basic_memory/models/base.py +10 -0
- basic_memory/models/knowledge.py +226 -0
- basic_memory/models/project.py +87 -0
- basic_memory/models/search.py +85 -0
- basic_memory/repository/__init__.py +11 -0
- basic_memory/repository/entity_repository.py +503 -0
- basic_memory/repository/observation_repository.py +73 -0
- basic_memory/repository/postgres_search_repository.py +379 -0
- basic_memory/repository/project_info_repository.py +10 -0
- basic_memory/repository/project_repository.py +128 -0
- basic_memory/repository/relation_repository.py +146 -0
- basic_memory/repository/repository.py +385 -0
- basic_memory/repository/search_index_row.py +95 -0
- basic_memory/repository/search_repository.py +94 -0
- basic_memory/repository/search_repository_base.py +241 -0
- basic_memory/repository/sqlite_search_repository.py +439 -0
- basic_memory/schemas/__init__.py +86 -0
- basic_memory/schemas/base.py +297 -0
- basic_memory/schemas/cloud.py +50 -0
- basic_memory/schemas/delete.py +37 -0
- basic_memory/schemas/directory.py +30 -0
- basic_memory/schemas/importer.py +35 -0
- basic_memory/schemas/memory.py +285 -0
- basic_memory/schemas/project_info.py +212 -0
- basic_memory/schemas/prompt.py +90 -0
- basic_memory/schemas/request.py +112 -0
- basic_memory/schemas/response.py +229 -0
- basic_memory/schemas/search.py +117 -0
- basic_memory/schemas/sync_report.py +72 -0
- basic_memory/schemas/v2/__init__.py +27 -0
- basic_memory/schemas/v2/entity.py +129 -0
- basic_memory/schemas/v2/resource.py +46 -0
- basic_memory/services/__init__.py +8 -0
- basic_memory/services/context_service.py +601 -0
- basic_memory/services/directory_service.py +308 -0
- basic_memory/services/entity_service.py +864 -0
- basic_memory/services/exceptions.py +37 -0
- basic_memory/services/file_service.py +541 -0
- basic_memory/services/initialization.py +216 -0
- basic_memory/services/link_resolver.py +121 -0
- basic_memory/services/project_service.py +880 -0
- basic_memory/services/search_service.py +404 -0
- basic_memory/services/service.py +15 -0
- basic_memory/sync/__init__.py +6 -0
- basic_memory/sync/background_sync.py +26 -0
- basic_memory/sync/sync_service.py +1259 -0
- basic_memory/sync/watch_service.py +510 -0
- basic_memory/telemetry.py +249 -0
- basic_memory/templates/prompts/continue_conversation.hbs +110 -0
- basic_memory/templates/prompts/search.hbs +101 -0
- basic_memory/utils.py +468 -0
- basic_memory-0.17.1.dist-info/METADATA +617 -0
- basic_memory-0.17.1.dist-info/RECORD +171 -0
- basic_memory-0.17.1.dist-info/WHEEL +4 -0
- basic_memory-0.17.1.dist-info/entry_points.txt +3 -0
- basic_memory-0.17.1.dist-info/licenses/LICENSE +661 -0
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
"""Shared initialization service for Basic Memory.
|
|
2
|
+
|
|
3
|
+
This module provides shared initialization functions used by both CLI and API
|
|
4
|
+
to ensure consistent application startup across all entry points.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import asyncio
|
|
8
|
+
import os
|
|
9
|
+
import sys
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
from loguru import logger
|
|
14
|
+
|
|
15
|
+
from basic_memory import db
|
|
16
|
+
from basic_memory.config import BasicMemoryConfig
|
|
17
|
+
from basic_memory.models import Project
|
|
18
|
+
from basic_memory.repository import (
|
|
19
|
+
ProjectRepository,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
async def initialize_database(app_config: BasicMemoryConfig) -> None:
|
|
24
|
+
"""Initialize database with migrations handled automatically by get_or_create_db.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
app_config: The Basic Memory project configuration
|
|
28
|
+
|
|
29
|
+
Note:
|
|
30
|
+
Database migrations are now handled automatically when the database
|
|
31
|
+
connection is first established via get_or_create_db().
|
|
32
|
+
"""
|
|
33
|
+
try:
|
|
34
|
+
await db.get_or_create_db(app_config.database_path)
|
|
35
|
+
logger.info("Database initialization completed")
|
|
36
|
+
except Exception as e:
|
|
37
|
+
logger.error(f"Error during database initialization: {e}")
|
|
38
|
+
raise
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
async def reconcile_projects_with_config(app_config: BasicMemoryConfig):
|
|
42
|
+
"""Ensure all projects in config.json exist in the projects table and vice versa.
|
|
43
|
+
|
|
44
|
+
This uses the ProjectService's synchronize_projects method to ensure bidirectional
|
|
45
|
+
synchronization between the configuration file and the database.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
app_config: The Basic Memory application configuration
|
|
49
|
+
"""
|
|
50
|
+
logger.info("Reconciling projects from config with database...")
|
|
51
|
+
|
|
52
|
+
# Get database session (engine already created by initialize_database)
|
|
53
|
+
_, session_maker = await db.get_or_create_db(
|
|
54
|
+
db_path=app_config.database_path,
|
|
55
|
+
db_type=db.DatabaseType.FILESYSTEM,
|
|
56
|
+
)
|
|
57
|
+
project_repository = ProjectRepository(session_maker)
|
|
58
|
+
|
|
59
|
+
# Import ProjectService here to avoid circular imports
|
|
60
|
+
from basic_memory.services.project_service import ProjectService
|
|
61
|
+
|
|
62
|
+
# Create project service and synchronize projects
|
|
63
|
+
project_service = ProjectService(repository=project_repository)
|
|
64
|
+
try:
|
|
65
|
+
await project_service.synchronize_projects()
|
|
66
|
+
logger.info("Projects successfully reconciled between config and database")
|
|
67
|
+
except Exception as e:
|
|
68
|
+
logger.error(f"Error during project synchronization: {e}")
|
|
69
|
+
logger.info("Continuing with initialization despite synchronization error")
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
async def initialize_file_sync(
|
|
73
|
+
app_config: BasicMemoryConfig,
|
|
74
|
+
) -> None:
|
|
75
|
+
"""Initialize file synchronization services. This function starts the watch service and does not return
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
app_config: The Basic Memory project configuration
|
|
79
|
+
|
|
80
|
+
Returns:
|
|
81
|
+
The watch service task that's monitoring file changes
|
|
82
|
+
"""
|
|
83
|
+
# Never start file watching during tests. Even "background" watchers add tasks/threads
|
|
84
|
+
# and can interact badly with strict asyncio teardown (especially on Windows/aiosqlite).
|
|
85
|
+
# Skip file sync in test environments to avoid interference with tests
|
|
86
|
+
if app_config.is_test_env:
|
|
87
|
+
logger.info("Test environment detected - skipping file sync initialization")
|
|
88
|
+
return None
|
|
89
|
+
|
|
90
|
+
# delay import
|
|
91
|
+
from basic_memory.sync import WatchService
|
|
92
|
+
|
|
93
|
+
# Get database session (migrations already run if needed)
|
|
94
|
+
_, session_maker = await db.get_or_create_db(
|
|
95
|
+
db_path=app_config.database_path,
|
|
96
|
+
db_type=db.DatabaseType.FILESYSTEM,
|
|
97
|
+
)
|
|
98
|
+
project_repository = ProjectRepository(session_maker)
|
|
99
|
+
|
|
100
|
+
# Initialize watch service
|
|
101
|
+
watch_service = WatchService(
|
|
102
|
+
app_config=app_config,
|
|
103
|
+
project_repository=project_repository,
|
|
104
|
+
quiet=True,
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
# Get active projects
|
|
108
|
+
active_projects = await project_repository.get_active_projects()
|
|
109
|
+
|
|
110
|
+
# Filter to constrained project if MCP server was started with --project
|
|
111
|
+
constrained_project = os.environ.get("BASIC_MEMORY_MCP_PROJECT")
|
|
112
|
+
if constrained_project:
|
|
113
|
+
active_projects = [p for p in active_projects if p.name == constrained_project]
|
|
114
|
+
logger.info(f"Background sync constrained to project: {constrained_project}")
|
|
115
|
+
|
|
116
|
+
# Start sync for all projects as background tasks (non-blocking)
|
|
117
|
+
async def sync_project_background(project: Project):
|
|
118
|
+
"""Sync a single project in the background."""
|
|
119
|
+
# avoid circular imports
|
|
120
|
+
from basic_memory.sync.sync_service import get_sync_service
|
|
121
|
+
|
|
122
|
+
logger.info(f"Starting background sync for project: {project.name}")
|
|
123
|
+
try:
|
|
124
|
+
# Create sync service
|
|
125
|
+
sync_service = await get_sync_service(project)
|
|
126
|
+
|
|
127
|
+
sync_dir = Path(project.path)
|
|
128
|
+
await sync_service.sync(sync_dir, project_name=project.name)
|
|
129
|
+
logger.info(f"Background sync completed successfully for project: {project.name}")
|
|
130
|
+
except Exception as e: # pragma: no cover
|
|
131
|
+
logger.error(f"Error in background sync for project {project.name}: {e}")
|
|
132
|
+
|
|
133
|
+
# Create background tasks for all project syncs (non-blocking)
|
|
134
|
+
sync_tasks = [
|
|
135
|
+
asyncio.create_task(sync_project_background(project)) for project in active_projects
|
|
136
|
+
]
|
|
137
|
+
logger.info(f"Created {len(sync_tasks)} background sync tasks")
|
|
138
|
+
|
|
139
|
+
# Don't await the tasks - let them run in background while we continue
|
|
140
|
+
|
|
141
|
+
# Then start the watch service in the background
|
|
142
|
+
logger.info("Starting watch service for all projects")
|
|
143
|
+
|
|
144
|
+
# run the watch service
|
|
145
|
+
await watch_service.run()
|
|
146
|
+
logger.info("Watch service started")
|
|
147
|
+
|
|
148
|
+
return None
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
async def initialize_app(
|
|
152
|
+
app_config: BasicMemoryConfig,
|
|
153
|
+
):
|
|
154
|
+
"""Initialize the Basic Memory application.
|
|
155
|
+
|
|
156
|
+
This function handles all initialization steps:
|
|
157
|
+
- Running database migrations
|
|
158
|
+
- Reconciling projects from config.json with projects table
|
|
159
|
+
- Setting up file synchronization
|
|
160
|
+
- Starting background migration for legacy project data
|
|
161
|
+
|
|
162
|
+
Args:
|
|
163
|
+
app_config: The Basic Memory project configuration
|
|
164
|
+
"""
|
|
165
|
+
# Skip initialization in cloud mode - cloud manages its own projects
|
|
166
|
+
if app_config.cloud_mode_enabled:
|
|
167
|
+
logger.debug("Skipping initialization in cloud mode - projects managed by cloud")
|
|
168
|
+
return
|
|
169
|
+
|
|
170
|
+
logger.info("Initializing app...")
|
|
171
|
+
# Initialize database first
|
|
172
|
+
await initialize_database(app_config)
|
|
173
|
+
|
|
174
|
+
# Reconcile projects from config.json with projects table
|
|
175
|
+
await reconcile_projects_with_config(app_config)
|
|
176
|
+
|
|
177
|
+
logger.info("App initialization completed (migration running in background if needed)")
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def ensure_initialization(app_config: BasicMemoryConfig) -> None:
|
|
181
|
+
"""Ensure initialization runs in a synchronous context.
|
|
182
|
+
|
|
183
|
+
This is a wrapper for the async initialize_app function that can be
|
|
184
|
+
called from synchronous code like CLI entry points.
|
|
185
|
+
|
|
186
|
+
No-op if app_config.cloud_mode == True. Cloud basic memory manages it's own projects
|
|
187
|
+
|
|
188
|
+
Args:
|
|
189
|
+
app_config: The Basic Memory project configuration
|
|
190
|
+
"""
|
|
191
|
+
# Skip initialization in cloud mode - cloud manages its own projects
|
|
192
|
+
if app_config.cloud_mode_enabled:
|
|
193
|
+
logger.debug("Skipping initialization in cloud mode - projects managed by cloud")
|
|
194
|
+
return
|
|
195
|
+
|
|
196
|
+
async def _init_and_cleanup():
|
|
197
|
+
"""Initialize app and clean up database connections.
|
|
198
|
+
|
|
199
|
+
Database connections created during initialization must be cleaned up
|
|
200
|
+
before the event loop closes, otherwise the process will hang indefinitely.
|
|
201
|
+
"""
|
|
202
|
+
try:
|
|
203
|
+
await initialize_app(app_config)
|
|
204
|
+
finally:
|
|
205
|
+
# Always cleanup database connections to prevent process hang
|
|
206
|
+
await db.shutdown_db()
|
|
207
|
+
|
|
208
|
+
# On Windows, use SelectorEventLoop to avoid ProactorEventLoop cleanup issues
|
|
209
|
+
# The ProactorEventLoop can raise "IndexError: pop from an empty deque" during
|
|
210
|
+
# event loop cleanup when there are pending handles. SelectorEventLoop is more
|
|
211
|
+
# stable for our use case (no subprocess pipes or named pipes needed).
|
|
212
|
+
if sys.platform == "win32":
|
|
213
|
+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
|
214
|
+
|
|
215
|
+
asyncio.run(_init_and_cleanup())
|
|
216
|
+
logger.info("Initialization completed successfully")
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"""Service for resolving markdown links to permalinks."""
|
|
2
|
+
|
|
3
|
+
from typing import Optional, Tuple
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
from loguru import logger
|
|
7
|
+
|
|
8
|
+
from basic_memory.models import Entity
|
|
9
|
+
from basic_memory.repository.entity_repository import EntityRepository
|
|
10
|
+
from basic_memory.schemas.search import SearchQuery, SearchItemType
|
|
11
|
+
from basic_memory.services.search_service import SearchService
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class LinkResolver:
|
|
15
|
+
"""Service for resolving markdown links to permalinks.
|
|
16
|
+
|
|
17
|
+
Uses a combination of exact matching and search-based resolution:
|
|
18
|
+
1. Try exact permalink match (fastest)
|
|
19
|
+
2. Try exact title match
|
|
20
|
+
3. Try exact file path match
|
|
21
|
+
4. Try file path with .md extension (for folder/title patterns)
|
|
22
|
+
5. Fall back to search for fuzzy matching
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(self, entity_repository: EntityRepository, search_service: SearchService):
|
|
26
|
+
"""Initialize with repositories."""
|
|
27
|
+
self.entity_repository = entity_repository
|
|
28
|
+
self.search_service = search_service
|
|
29
|
+
|
|
30
|
+
async def resolve_link(
|
|
31
|
+
self, link_text: str, use_search: bool = True, strict: bool = False
|
|
32
|
+
) -> Optional[Entity]:
|
|
33
|
+
"""Resolve a markdown link to a permalink.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
link_text: The link text to resolve
|
|
37
|
+
use_search: Whether to use search-based fuzzy matching as fallback
|
|
38
|
+
strict: If True, only exact matches are allowed (no fuzzy search fallback)
|
|
39
|
+
"""
|
|
40
|
+
logger.trace(f"Resolving link: {link_text}")
|
|
41
|
+
|
|
42
|
+
# Clean link text and extract any alias
|
|
43
|
+
clean_text, alias = self._normalize_link_text(link_text)
|
|
44
|
+
|
|
45
|
+
# 1. Try exact permalink match first (most efficient)
|
|
46
|
+
entity = await self.entity_repository.get_by_permalink(clean_text)
|
|
47
|
+
if entity:
|
|
48
|
+
logger.debug(f"Found exact permalink match: {entity.permalink}")
|
|
49
|
+
return entity
|
|
50
|
+
|
|
51
|
+
# 2. Try exact title match
|
|
52
|
+
found = await self.entity_repository.get_by_title(clean_text)
|
|
53
|
+
if found:
|
|
54
|
+
# Return first match if there are duplicates (consistent behavior)
|
|
55
|
+
entity = found[0]
|
|
56
|
+
logger.debug(f"Found title match: {entity.title}")
|
|
57
|
+
return entity
|
|
58
|
+
|
|
59
|
+
# 3. Try file path
|
|
60
|
+
found_path = await self.entity_repository.get_by_file_path(clean_text)
|
|
61
|
+
if found_path:
|
|
62
|
+
logger.debug(f"Found entity with path: {found_path.file_path}")
|
|
63
|
+
return found_path
|
|
64
|
+
|
|
65
|
+
# 4. Try file path with .md extension if not already present
|
|
66
|
+
if not clean_text.endswith(".md") and "/" in clean_text:
|
|
67
|
+
file_path_with_md = f"{clean_text}.md"
|
|
68
|
+
found_path_md = await self.entity_repository.get_by_file_path(file_path_with_md)
|
|
69
|
+
if found_path_md:
|
|
70
|
+
logger.debug(f"Found entity with path (with .md): {found_path_md.file_path}")
|
|
71
|
+
return found_path_md
|
|
72
|
+
|
|
73
|
+
# In strict mode, don't try fuzzy search - return None if no exact match found
|
|
74
|
+
if strict:
|
|
75
|
+
return None
|
|
76
|
+
|
|
77
|
+
# 5. Fall back to search for fuzzy matching (only if not in strict mode)
|
|
78
|
+
if use_search and "*" not in clean_text:
|
|
79
|
+
results = await self.search_service.search(
|
|
80
|
+
query=SearchQuery(text=clean_text, entity_types=[SearchItemType.ENTITY]),
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
if results:
|
|
84
|
+
# Look for best match
|
|
85
|
+
best_match = min(results, key=lambda x: x.score) # pyright: ignore
|
|
86
|
+
logger.trace(
|
|
87
|
+
f"Selected best match from {len(results)} results: {best_match.permalink}"
|
|
88
|
+
)
|
|
89
|
+
if best_match.permalink:
|
|
90
|
+
return await self.entity_repository.get_by_permalink(best_match.permalink)
|
|
91
|
+
|
|
92
|
+
# if we couldn't find anything then return None
|
|
93
|
+
return None
|
|
94
|
+
|
|
95
|
+
def _normalize_link_text(self, link_text: str) -> Tuple[str, Optional[str]]:
|
|
96
|
+
"""Normalize link text and extract alias if present.
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
link_text: Raw link text from markdown
|
|
100
|
+
|
|
101
|
+
Returns:
|
|
102
|
+
Tuple of (normalized_text, alias or None)
|
|
103
|
+
"""
|
|
104
|
+
# Strip whitespace
|
|
105
|
+
text = link_text.strip()
|
|
106
|
+
|
|
107
|
+
# Remove enclosing brackets if present
|
|
108
|
+
if text.startswith("[[") and text.endswith("]]"):
|
|
109
|
+
text = text[2:-2]
|
|
110
|
+
|
|
111
|
+
# Handle Obsidian-style aliases (format: [[actual|alias]])
|
|
112
|
+
alias = None
|
|
113
|
+
if "|" in text:
|
|
114
|
+
text, alias = text.split("|", 1)
|
|
115
|
+
text = text.strip()
|
|
116
|
+
alias = alias.strip()
|
|
117
|
+
else:
|
|
118
|
+
# Strip whitespace from text even if no alias
|
|
119
|
+
text = text.strip()
|
|
120
|
+
|
|
121
|
+
return text, alias
|