feat(notion-search): add Notion search execution with workspace/DB modes

For each expanded query variant: hit Notion's workspace search OR
per-database data_sources.query (when --databases is specified).
Union and dedupe by page ID, cap at 30 candidates total. Filters out
non-page objects (databases) from workspace search results.
Property filters (--filter JSON) pass through to data_sources.query
when in per-database mode.

5 tests: workspace dedup, 30-cap, DB-mode dispatch, page-only filter,
property-filter passthrough. 17 passing total.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-28 07:52:00 +09:00
parent 5f6438ecf3
commit a4da24b15c
2 changed files with 162 additions and 1 deletions

View File

@@ -11,8 +11,9 @@ from __future__ import annotations
import json
import re
import sys
from typing import Callable, List, Optional
from typing import Callable, Dict, List, Optional
import _notion_compat as compat
from _search_llm import call_claude as default_llm_caller
EXPAND_PROMPT = """You are a query expander for a Notion semantic search tool. Generate up to {n} variants of the user's query that capture related concepts, synonyms, and cross-language alternates (especially Korean ↔ English).
@@ -78,3 +79,60 @@ def expand_query(
if len(result) >= max_variants:
break
return result
MAX_CANDIDATES = 30
def search_candidates(
notion,
queries: List[str],
*,
databases: Optional[List[str]] = None,
prop_filter: Optional[Dict] = None,
) -> List[Dict]:
"""Run each query against Notion (workspace or per-DB), dedupe, cap at MAX_CANDIDATES.
Returns a list of page result objects (whatever Notion returned), order preserves
first-seen position across all variants — used as fallback ordering when rerank is off.
"""
seen_ids = set()
candidates: List[Dict] = []
for query in queries:
if databases:
# Per-database query (data_sources.query)
for db_id in databases:
try:
data_source_id = compat.resolve_data_source_id(notion, db_id)
except Exception as exc:
print(f"Warning: could not resolve database {db_id}: {exc}",
file=sys.stderr)
continue
params = {"data_source_id": data_source_id, "page_size": 100}
if prop_filter:
params["filter"] = prop_filter
response = notion.data_sources.query(**params)
for page in response.get("results", []):
page_id = page.get("id")
if page_id and page_id not in seen_ids:
seen_ids.add(page_id)
candidates.append(page)
if len(candidates) >= MAX_CANDIDATES:
return candidates
else:
# Workspace-wide search
response = notion.search(query=query, page_size=100)
for item in response.get("results", []):
if item.get("object") != "page":
continue
page_id = item.get("id")
if page_id and page_id not in seen_ids:
seen_ids.add(page_id)
candidates.append(item)
if len(candidates) >= MAX_CANDIDATES:
return candidates
return candidates