from typing import TYPE_CHECKING from pydantic import BaseModel, Field from core_domain import ( KnowledgeBaseContract, KnowledgeBaseStatus, KnowledgeChunkContract, KnowledgeDocumentContract, KnowledgeSearchRequestContract, KnowledgeSearchResultContract, ) from core_shared import JSONValue if TYPE_CHECKING: from app.db.models import KnowledgeBase, KnowledgeChunk, KnowledgeDocument class KnowledgeBaseCreateRequest(BaseModel): tenant_id: str code: str name: str description: str | None = None metadata_json: dict[str, JSONValue] = Field(default_factory=dict) class KnowledgeBaseStatusUpdateRequest(BaseModel): tenant_id: str status: KnowledgeBaseStatus class KnowledgeBaseResponse(KnowledgeBaseContract): @classmethod def from_entity(cls, entity: "KnowledgeBase") -> "KnowledgeBaseResponse": return cls.model_validate(entity, from_attributes=True) class KnowledgeDocumentCreateRequest(BaseModel): tenant_id: str knowledge_base_id: str title: str content_text: str source_type: str = "text" source_uri: str | None = None metadata_json: dict[str, JSONValue] = Field(default_factory=dict) chunk_size: int | None = Field(default=None, gt=0) chunk_overlap: int | None = Field(default=None, ge=0) class KnowledgeDocumentResponse(KnowledgeDocumentContract): @classmethod def from_entity(cls, entity: "KnowledgeDocument") -> "KnowledgeDocumentResponse": return cls.model_validate(entity, from_attributes=True) class KnowledgeChunkResponse(KnowledgeChunkContract): @classmethod def from_entity(cls, entity: "KnowledgeChunk") -> "KnowledgeChunkResponse": return cls.model_validate(entity, from_attributes=True) class KnowledgeDocumentIngestResponse(BaseModel): document: KnowledgeDocumentResponse chunks: list[KnowledgeChunkResponse] class KnowledgeSearchRequest(KnowledgeSearchRequestContract): pass class KnowledgeSearchResultResponse(KnowledgeSearchResultContract): pass