refactor(tools): resolve_project uses shared score_project_match helper

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-12 16:43:05 -04:00
parent 460959f0d4
commit 9a96fdb3c0
2 changed files with 40 additions and 14 deletions
+8 -14
View File
@@ -54,30 +54,24 @@ def score_project_match(query: str, project) -> float:
async def resolve_project(user_id: int, project_name: str):
"""Exact-then-fuzzy project lookup. Returns the Project or None.
"""Exact-then-scored project lookup. Returns the Project or None.
Resolution order:
1. Exact title match (case-insensitive via DB)
2. project_name is a substring of an existing title
3. Existing title is a substring of project_name
4. SequenceMatcher ratio >= 0.55
1. Exact title match (case-insensitive via DB query).
2. Highest `score_project_match` across all projects, threshold 0.55.
"""
from fabledassistant.services.projects import get_project_by_title, list_projects
proj = await get_project_by_title(user_id, project_name)
if proj is not None:
return proj
needle = project_name.lower().strip()
all_p = await list_projects(user_id)
best, best_score = None, 0.0
for p in all_p:
haystack = p.title.lower().strip()
if needle in haystack or haystack in needle:
return p
best, best_r = None, 0.0
for p in all_p:
r = SequenceMatcher(None, needle, p.title.lower().strip()).ratio()
if r >= 0.55 and r > best_r:
best, best_r = p, r
score = score_project_match(project_name, p)
if score >= 0.55 and score > best_score:
best, best_score = p, score
return best