SurfSense/surfsense_local/backend/modules/source_roots/models.py
DESKTOP-RTLN3BA\$punk 00ed2a746d feat(sources): keep every source in a folder and let a chat or Studio run read only the ticked folders and sources
File-agent proposal 01, phases 0-2, without the Trash and without linked disk roots. Revisions 0024 and 0025 add a managed source root per workspace, a folders tree, and a nullable folder_id on documents, and back-fill existing sources into the root folder. The folders slice creates, renames, moves and deletes folders (delete removes its sources, as today) and moves sources between them. Uploads can carry a relative path that lands under the matching folders. A thread stores its folder and source scope, and chat sends and Studio regenerations resolve it on the server. The existing document_ids request field keeps working exactly as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 17:32:56 -07:00

56 lines
1.8 KiB
Python

import enum
from datetime import datetime
from sqlalchemy import ForeignKey, Index, func, text
from sqlalchemy.orm import Mapped, mapped_column
from shared.db import Base, text_enum
class SourceRootKind(enum.StrEnum):
# The Library: folders the user makes in the app, bytes in the data directory.
MANAGED = "managed"
# A folder on disk, indexed in place. Not built yet.
LINKED = "linked"
class SourceRootState(enum.StrEnum):
# Every state linked roots will need, so adding one never rebuilds the
# table: a rebuild cascades into folders and unfiles every document.
READY = "ready"
SCANNING = "scanning"
UNAVAILABLE = "unavailable"
PAUSED = "paused"
UNLINKING = "unlinking"
class SourceRoot(Base):
"""Where a tree of folders comes from; each workspace has one managed Library."""
__tablename__ = "source_roots"
__table_args__ = (
Index("source_roots_workspace_name", "workspace_id", "name_key", unique=True),
Index(
"source_roots_one_managed",
"workspace_id",
unique=True,
sqlite_where=text("kind = 'managed'"),
),
)
id: Mapped[int] = mapped_column(primary_key=True)
workspace_id: Mapped[int] = mapped_column(
ForeignKey("workspaces.id", ondelete="CASCADE")
)
kind: Mapped[SourceRootKind] = mapped_column(text_enum(SourceRootKind))
name: Mapped[str]
name_key: Mapped[str]
# The resolved absolute path of a linked root; None for the Library.
disk_path: Mapped[str | None]
state: Mapped[SourceRootState] = mapped_column(
text_enum(SourceRootState), default=SourceRootState.READY
)
created_at: Mapped[datetime] = mapped_column(server_default=func.now())
updated_at: Mapped[datetime] = mapped_column(
server_default=func.now(), onupdate=func.now()
)