SurfSense/surfsense_backend/app/schemas/documents.py

118 lines
2.7 KiB
Python
Raw Normal View History

from datetime import datetime
2025-10-01 18:50:36 -07:00
from typing import TypeVar
from uuid import UUID
from pydantic import BaseModel, ConfigDict
2025-03-14 18:53:14 -07:00
from app.db import DocumentType
from .chunks import ChunkRead
T = TypeVar("T")
2025-03-14 18:53:14 -07:00
class ExtensionDocumentMetadata(BaseModel):
BrowsingSessionId: str
VisitedWebPageURL: str
VisitedWebPageTitle: str
VisitedWebPageDateWithTimeInISOString: str
VisitedWebPageReffererURL: str
VisitedWebPageVisitDurationInMilliseconds: str
2025-03-14 18:53:14 -07:00
class ExtensionDocumentContent(BaseModel):
metadata: ExtensionDocumentMetadata
pageContent: str # noqa: N815
2025-03-14 18:53:14 -07:00
class DocumentBase(BaseModel):
document_type: DocumentType
content: (
list[ExtensionDocumentContent] | list[str] | str
) # Updated to allow string content
2025-03-14 18:53:14 -07:00
search_space_id: int
2025-03-14 18:53:14 -07:00
class DocumentsCreate(DocumentBase):
pass
2025-03-14 18:53:14 -07:00
class DocumentUpdate(DocumentBase):
pass
class DocumentStatusSchema(BaseModel):
"""Document processing status."""
2026-02-06 05:35:15 +05:30
state: str # "ready", "processing", "failed"
reason: str | None = None
2025-03-14 18:53:14 -07:00
class DocumentRead(BaseModel):
id: int
title: str
document_type: DocumentType
document_metadata: dict
content: str # Changed to string to match frontend
content_hash: str
unique_identifier_hash: str | None
2025-03-14 18:53:14 -07:00
created_at: datetime
updated_at: datetime | None
2025-03-14 18:53:14 -07:00
search_space_id: int
created_by_id: UUID | None = None # User who created/uploaded this document
created_by_name: str | None = None
created_by_email: str | None = None
2026-02-06 05:35:15 +05:30
status: DocumentStatusSchema | None = (
None # Processing status (ready, processing, failed)
)
2025-03-14 18:53:14 -07:00
model_config = ConfigDict(from_attributes=True)
class DocumentWithChunksRead(DocumentRead):
chunks: list[ChunkRead] = []
model_config = ConfigDict(from_attributes=True)
2025-10-01 18:50:36 -07:00
class PaginatedResponse[T](BaseModel):
items: list[T]
total: int
page: int
page_size: int
has_more: bool
class DocumentTitleRead(BaseModel):
"""Lightweight document response for mention picker - only essential fields."""
id: int
title: str
document_type: DocumentType
model_config = ConfigDict(from_attributes=True)
class DocumentTitleSearchResponse(BaseModel):
"""Response for document title search - optimized for typeahead."""
items: list[DocumentTitleRead]
has_more: bool
class DocumentStatusItemRead(BaseModel):
"""Lightweight document status payload for batch status polling."""
id: int
title: str
document_type: DocumentType
status: DocumentStatusSchema
model_config = ConfigDict(from_attributes=True)
class DocumentStatusBatchResponse(BaseModel):
"""Batch status response for a set of document IDs."""
items: list[DocumentStatusItemRead]