mirror of
https://github.com/langgenius/dify.git
synced 2026-11-11 05:41:09 +08:00
refactor(api): establish dataset application boundaries (#41729)
Co-authored-by: Jyong <jyong@dify.ai>
This commit is contained in:
1 parent
c005932db9
commit
a67cd87eb0
417 files changed
+30522
-23751
No files matched your search
@@ -1,16 +1,12 @@
|
||||
"""Response schemas for dataset document endpoints."""
|
||||
|
||||
from collections.abc import Iterable
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from pydantic import Field, field_validator
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from fields.base import ResponseModel
|
||||
from libs.helper import to_timestamp
|
||||
from models.dataset import DocMetadataDetailItem, Document
|
||||
|
||||
|
||||
def normalize_enum(value: Any) -> Any:
|
||||
@@ -70,39 +66,6 @@ class DocumentResponse(ResponseModel):
|
||||
return to_timestamp(value)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class DocumentWithSession:
|
||||
"""Expose session-backed document fields during response validation."""
|
||||
|
||||
document: Document
|
||||
session: Session
|
||||
|
||||
@property
|
||||
def data_source_detail_dict(self) -> dict[str, Any]:
|
||||
return self.document.get_data_source_detail_dict(session=self.session)
|
||||
|
||||
@property
|
||||
def hit_count(self) -> int:
|
||||
return self.document.get_hit_count(session=self.session)
|
||||
|
||||
@property
|
||||
def doc_metadata_details(self) -> list[DocMetadataDetailItem] | None:
|
||||
return self.document.get_doc_metadata_details(session=self.session)
|
||||
|
||||
def __getattr__(self, name: str) -> Any:
|
||||
return getattr(self.document, name) # guard-ignore: no-new-getattr -- delegates model fields
|
||||
|
||||
|
||||
def document_response(document: Document, *, session: Session) -> DocumentResponse:
|
||||
return DocumentResponse.model_validate(
|
||||
DocumentWithSession(document=document, session=session), from_attributes=True
|
||||
)
|
||||
|
||||
|
||||
def document_responses(documents: Iterable[Document], *, session: Session) -> list[DocumentResponse]:
|
||||
return [document_response(document, session=session) for document in documents]
|
||||
|
||||
|
||||
class DocumentListResponse(ResponseModel):
|
||||
data: list[DocumentResponse]
|
||||
has_more: bool
|
||||
|
||||
Reference in new issue
Block a user