- Add api_tokens table with role-based access (researcher, developer, viewer)
- Add POST /auth/token/generate endpoint for creating tokens
- Add GET /auth/tokens endpoint for listing user's tokens
- Add DELETE /auth/tokens/{token_id} endpoint for revoking tokens
- Add agent_type field to documents (research, development, general)
- Implement role-based access control for documents:
- researcher: access to research and general documents
- developer: access to development and general documents
- viewer: read-only access
- Update document model and schemas with agent_type field
- Add comprehensive tests for API token functionality
- All existing tests pass (73 total)
47 lines
2.3 KiB
Python
47 lines
2.3 KiB
Python
import uuid
|
|
from datetime import datetime
|
|
from enum import Enum
|
|
|
|
from sqlalchemy import Boolean, DateTime, ForeignKey, String, Text
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.database import Base
|
|
|
|
|
|
def generate_uuid() -> str:
|
|
return str(uuid.uuid4())
|
|
|
|
|
|
class ReasoningType(str, Enum):
|
|
CHAIN = "chain"
|
|
IDEA = "idea"
|
|
CONTEXT = "context"
|
|
REFLECTION = "reflection"
|
|
|
|
|
|
class Document(Base):
|
|
__tablename__ = "documents"
|
|
|
|
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=generate_uuid)
|
|
title: Mapped[str] = mapped_column(String(500), nullable=False)
|
|
content: Mapped[str] = mapped_column(Text, nullable=False, default="")
|
|
project_id: Mapped[str] = mapped_column(String(36), ForeignKey("projects.id", ondelete="CASCADE"), nullable=False)
|
|
folder_id: Mapped[str | None] = mapped_column(String(36), ForeignKey("folders.id", ondelete="SET NULL"), nullable=True)
|
|
path: Mapped[str] = mapped_column(Text, nullable=False)
|
|
is_deleted: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
|
|
deleted_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
|
deleted_by: Mapped[str | None] = mapped_column(String(36), nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
|
|
# Phase 2: Reasoning fields
|
|
reasoning_type: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
|
confidence: Mapped[str | None] = mapped_column(String(10), nullable=True) # Store as string to handle JSON serialization
|
|
reasoning_steps: Mapped[str | None] = mapped_column(Text, nullable=True) # JSON array as text
|
|
model_source: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
|
tiptap_content: Mapped[str | None] = mapped_column(Text, nullable=True) # JSON object as text
|
|
# Phase 3: Link tracking
|
|
outgoing_links: Mapped[str] = mapped_column(Text, nullable=False, default="[]") # JSON array of document IDs
|
|
backlinks_count: Mapped[int] = mapped_column(default=0, nullable=False) # Cached count of incoming links
|
|
# Role-based access
|
|
agent_type: Mapped[str] = mapped_column(String(20), nullable=False, default="general") # research, development, general
|