SurfSense/surfsense_backend/app/agents/chat/runtime/references/folders.py
CREDO23 7fb0707933 refactor(backend): rename search_space -> workspace across app bulk (Phase 2 Wave D)
Scoped codemod over surfsense_backend/app (excluding routes/, Wave E): renames
search_space_id -> workspace_id, search_space -> workspace, SearchSpace -> Workspace
across services, utils, tasks, agents, gateway, event_bus, notifications, podcasts,
automations, observability params, and prompt .md files. Also flips the camelCase
payload key searchSpaceId -> workspaceId (no backend reader; hard cutover).

Preserved carve-outs (verbatim): Celery task names "delete_search_space_background"
and "ai_sort_search_space" (wire names), and the OTel/metric key "search_space.id"
(dashboards depend on it). Enum values 'SEARCH_SPACE' and SearchSourceConnector
untouched.
2026-06-26 18:30:47 +02:00

54 lines
1.6 KiB
Python

"""Resolve ``@folder`` ids into references for the pointer block."""
from __future__ import annotations
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.agents.chat.runtime.path_resolver import DOCUMENTS_ROOT, PathIndex
from app.db import Folder
from .models import FolderReference
def folder_pointer_path(folder_id: int, folder_paths: dict[int, str]) -> str:
"""Trailing-slash virtual path so the model reads the pointer as a directory."""
base = folder_paths.get(folder_id, DOCUMENTS_ROOT)
return base if base.endswith("/") else f"{base}/"
async def resolve_folder_references(
session: AsyncSession,
*,
workspace_id: int,
folder_ids: list[int],
index: PathIndex,
) -> list[FolderReference]:
"""Map folder ids to references in input order; unknown ids are dropped."""
if not folder_ids:
return []
rows = await session.execute(
select(Folder).where(
Folder.workspace_id == workspace_id,
Folder.id.in_(folder_ids),
)
)
folders_by_id = {row.id: row for row in rows.scalars().all()}
references: list[FolderReference] = []
for folder_id in dict.fromkeys(folder_ids):
folder = folders_by_id.get(folder_id)
if folder is None:
continue
references.append(
FolderReference(
entity_id=folder.id,
label=str(folder.name or "untitled"),
path=folder_pointer_path(folder.id, index.folder_paths),
)
)
return references
__all__ = ["folder_pointer_path", "resolve_folder_references"]