Spaces:
Runtime error
Runtime error
File size: 6,169 Bytes
ed86a97 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | """Knowledge source and memory indexing models."""
from datetime import datetime
from enum import Enum
from typing import Any, Dict, List, Optional
from uuid import UUID, uuid4
from pydantic import BaseModel, ConfigDict, Field
class KnowledgeSourceType(str, Enum):
EXCEL = "excel"
CSV = "csv"
URL = "url"
PDF = "pdf"
DOC = "doc"
DOCX = "docx"
TXT = "txt"
DOCUMENT = "document"
API_STUB = "api_stub"
class KnowledgeVersionState(str, Enum):
DRAFT = "draft"
ACTIVE = "active"
ARCHIVED = "archived"
FAILED = "failed"
class KnowledgeCrawlTrigger(str, Enum):
COMMIT = "commit"
MANUAL_BOT_RECRAWL = "manual_bot_recrawl"
class KnowledgeCrawlStatus(str, Enum):
QUEUED = "queued"
RUNNING = "running"
AWAITING_BROWSER_FALLBACK = "awaiting_browser_fallback"
COMPLETED = "completed"
FAILED = "failed"
CANCELLED = "cancelled"
class KnowledgeCrawlPhase(str, Enum):
HTTP_FETCH = "http_fetch"
BROWSER_FALLBACK = "browser_fallback"
INDEXING = "indexing"
class KnowledgeSource(BaseModel):
"""Logical source container."""
model_config = ConfigDict(from_attributes=True)
id: UUID = Field(default_factory=uuid4)
workspace_id: UUID
name: str
source_type: KnowledgeSourceType
description: Optional[str] = None
metadata: Dict[str, Any] = Field(default_factory=dict)
is_active: bool = True
created_at: datetime = Field(default_factory=datetime.now)
updated_at: datetime = Field(default_factory=datetime.now)
created_by: Optional[str] = None
class KnowledgeSourceVersion(BaseModel):
"""Versioned payload for a knowledge source."""
model_config = ConfigDict(from_attributes=True)
id: UUID = Field(default_factory=uuid4)
source_id: UUID
workspace_id: UUID
version_label: str
state: KnowledgeVersionState = KnowledgeVersionState.DRAFT
content_hash: Optional[str] = None
storage_ref: Optional[str] = None
parsed_payload: Dict[str, Any] = Field(default_factory=dict)
validation_report: Dict[str, Any] = Field(default_factory=dict)
created_at: datetime = Field(default_factory=datetime.now)
updated_at: datetime = Field(default_factory=datetime.now)
created_by: Optional[str] = None
class KnowledgeSourceAssignment(BaseModel):
"""Scenario/subscenario assignment with required/optional policy."""
model_config = ConfigDict(from_attributes=True)
id: UUID = Field(default_factory=uuid4)
workspace_id: UUID
source_id: UUID
source_version_id: UUID
scenario_id: UUID
subscenario_id: Optional[UUID] = None
required: bool = True
weight: float = 1.0
effective_from: Optional[datetime] = None
effective_to: Optional[datetime] = None
created_at: datetime = Field(default_factory=datetime.now)
updated_at: datetime = Field(default_factory=datetime.now)
created_by: Optional[str] = None
class KnowledgeImportJob(BaseModel):
"""Import/validation job status."""
model_config = ConfigDict(from_attributes=True)
id: UUID = Field(default_factory=uuid4)
workspace_id: UUID
source_id: Optional[UUID] = None
filename: Optional[str] = None
source_type: KnowledgeSourceType = KnowledgeSourceType.CSV
status: str = "pending"
row_count: int = 0
valid_count: int = 0
invalid_count: int = 0
validation_report: Dict[str, Any] = Field(default_factory=dict)
created_at: datetime = Field(default_factory=datetime.now)
completed_at: Optional[datetime] = None
created_by: Optional[str] = None
class KnowledgeCrawlRun(BaseModel):
"""Persisted crawl/index lifecycle for a source version."""
model_config = ConfigDict(from_attributes=True)
id: UUID = Field(default_factory=uuid4)
workspace_id: UUID
source_id: UUID
source_version_id: UUID
trigger: KnowledgeCrawlTrigger = KnowledgeCrawlTrigger.COMMIT
status: KnowledgeCrawlStatus = KnowledgeCrawlStatus.QUEUED
phase: KnowledgeCrawlPhase = KnowledgeCrawlPhase.HTTP_FETCH
totals: Dict[str, Any] = Field(
default_factory=lambda: {
"total": 0,
"success": 0,
"failed": 0,
"unreadable": 0,
"indexed_pages": 0,
"indexed_chunks": 0,
}
)
started_at: Optional[datetime] = None
completed_at: Optional[datetime] = None
created_at: datetime = Field(default_factory=datetime.now)
created_by: Optional[str] = None
class KnowledgePage(BaseModel):
"""Fetched canonical page payload belonging to a source version."""
model_config = ConfigDict(from_attributes=True)
id: UUID = Field(default_factory=uuid4)
workspace_id: UUID
source_id: UUID
source_version_id: UUID
url: str
url_canonical: str
title: str = ""
content_type: str = ""
http_status: Optional[int] = None
fetch_status: str = "unknown"
fetch_method: str = "http"
content_hash: Optional[str] = None
raw_chars: int = 0
cleaned_text: str = ""
fetched_at: Optional[datetime] = None
error: Optional[str] = None
created_at: datetime = Field(default_factory=datetime.now)
updated_at: datetime = Field(default_factory=datetime.now)
class KnowledgeChunk(BaseModel):
"""Searchable chunk extracted from a knowledge page."""
model_config = ConfigDict(from_attributes=True)
id: UUID = Field(default_factory=uuid4)
workspace_id: UUID
page_id: UUID
source_id: UUID
source_version_id: UUID
chunk_index: int = 0
chunk_text: str
token_estimate: int = 0
chunk_hash: str
embedding_json: Optional[List[float]] = None
created_at: datetime = Field(default_factory=datetime.now)
class SessionKnowledgeSnapshot(BaseModel):
"""Frozen reference of memory scope used by a session."""
model_config = ConfigDict(from_attributes=True)
id: UUID = Field(default_factory=uuid4)
session_id: UUID
workspace_id: UUID
bot_id: UUID
source_version_ids: List[str] = Field(default_factory=list)
crawl_run_ids: List[str] = Field(default_factory=list)
created_at: datetime = Field(default_factory=datetime.now)
|