1use anyhow::Result;
2use chrono::{DateTime, NaiveDate, Utc};
3use serde::{Deserialize, Serialize};
4use serde_json::{Value, json};
5use utoipa::ToSchema;
6use uuid::Uuid;
7
8use super::Visibility;
9
10#[derive(Debug, Clone, Copy, Serialize, Deserialize, ToSchema, PartialEq, Eq)]
11#[serde(rename_all = "snake_case")]
12pub enum LibraryIngestStatus {
13 Pending,
14 Running,
15 Succeeded,
16 Failed,
17}
18
19impl LibraryIngestStatus {
20 pub fn as_str(self) -> &'static str {
21 match self {
22 Self::Pending => "pending",
23 Self::Running => "running",
24 Self::Succeeded => "succeeded",
25 Self::Failed => "failed",
26 }
27 }
28}
29
30impl std::str::FromStr for LibraryIngestStatus {
31 type Err = anyhow::Error;
32
33 fn from_str(value: &str) -> Result<Self, Self::Err> {
34 match value {
35 "pending" => Ok(Self::Pending),
36 "running" => Ok(Self::Running),
37 "succeeded" => Ok(Self::Succeeded),
38 "failed" => Ok(Self::Failed),
39 other => Err(anyhow::anyhow!(
40 "unsupported library ingest status: {other}"
41 )),
42 }
43 }
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
47pub struct CreateFolderRequest {
48 #[serde(default)]
49 pub parent_folder_id: Option<Uuid>,
50 pub name: String,
51}
52
53#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
54pub struct MoveFolderRequest {
55 #[serde(default)]
56 pub target_folder_id: Option<Uuid>,
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
60pub struct MoveFileRequest {
61 #[serde(default)]
62 pub target_folder_id: Option<Uuid>,
63}
64
65#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
66pub struct CreateTextRequest {
67 #[serde(default)]
68 pub folder_id: Option<Uuid>,
69 pub title: String,
70 pub content: String,
71 #[serde(default = "default_text_content_format")]
72 pub content_format: LibraryTextContentFormat,
73 #[serde(default, skip_serializing_if = "Option::is_none")]
74 pub source_uri: Option<String>,
75 #[serde(default, skip_serializing_if = "Option::is_none")]
76 pub summary: Option<String>,
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
80pub struct UpsertLibraryTextRequest {
81 pub external_id: String,
82 #[serde(default)]
83 pub folder_id: Option<Uuid>,
84 pub title: String,
85 pub content: String,
86 #[serde(default = "default_text_content_format")]
87 pub content_format: LibraryTextContentFormat,
88 #[serde(default, skip_serializing_if = "Option::is_none")]
89 pub source_uri: Option<String>,
90 #[serde(default, skip_serializing_if = "Option::is_none")]
91 pub summary: Option<String>,
92 #[serde(default, skip_serializing_if = "Option::is_none")]
93 pub published_at: Option<NaiveDate>,
94 #[serde(default = "default_metadata_json")]
95 #[schema(value_type = Object)]
96 pub metadata_json: Value,
97}
98
99#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
100pub struct LibraryFileSummary {
101 pub file_id: Uuid,
102 pub group_key: String,
103 pub group_path: String,
104 pub visibility: Visibility,
105 #[serde(default)]
106 pub folder_id: Option<Uuid>,
107 pub filename: String,
108 pub media_type: String,
109 pub size_bytes: i64,
110 pub ingest_status: LibraryIngestStatus,
111 #[serde(default, skip_serializing_if = "Option::is_none")]
112 pub error_message: Option<String>,
113 pub created_at: DateTime<Utc>,
114 pub updated_at: DateTime<Utc>,
115 #[serde(default, skip_serializing_if = "Option::is_none")]
116 pub ingested_at: Option<DateTime<Utc>>,
117}
118
119#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
120pub struct LibraryFolderNode {
121 pub group_key: String,
122 pub group_path: String,
123 pub visibility: Visibility,
124 #[serde(default)]
125 pub folder_id: Option<Uuid>,
126 #[serde(default)]
127 pub parent_folder_id: Option<Uuid>,
128 pub name: String,
129 pub path: String,
130 pub processing_count: usize,
131 #[schema(no_recursion)]
132 pub children: Vec<LibraryFolderNode>,
133 pub files: Vec<LibraryFileSummary>,
134}
135
136#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
137pub struct LibraryFolderResponse {
138 pub folder_id: Uuid,
139 pub group_key: String,
140 pub group_path: String,
141 pub visibility: Visibility,
142 #[serde(default)]
143 pub parent_folder_id: Option<Uuid>,
144 pub name: String,
145 pub path: String,
146 pub created_at: DateTime<Utc>,
147 pub updated_at: DateTime<Utc>,
148}
149
150#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
151pub struct LibraryTreeResponse {
152 pub root: LibraryFolderNode,
153}
154
155#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
156pub struct LibraryIngestJobResponse {
157 pub job_id: Uuid,
158 pub group_key: String,
159 pub group_path: String,
160 pub visibility: Visibility,
161 pub file_id: Uuid,
162 pub status: LibraryIngestStatus,
163 #[serde(default, skip_serializing_if = "Option::is_none")]
164 pub docling_task_id: Option<String>,
165 #[serde(default, skip_serializing_if = "Option::is_none")]
166 pub error_message: Option<String>,
167 pub created_at: DateTime<Utc>,
168 #[serde(default, skip_serializing_if = "Option::is_none")]
169 pub started_at: Option<DateTime<Utc>>,
170 #[serde(default, skip_serializing_if = "Option::is_none")]
171 pub finished_at: Option<DateTime<Utc>>,
172 pub updated_at: DateTime<Utc>,
173}
174
175#[derive(Debug, Clone, Copy, Serialize, Deserialize, ToSchema, PartialEq, Eq)]
176#[serde(rename_all = "snake_case")]
177pub enum LibraryTextContentFormat {
178 PlainText,
179 Markdown,
180}
181
182#[derive(Debug, Clone, Copy, Serialize, Deserialize, ToSchema, PartialEq, Eq)]
183#[serde(rename_all = "snake_case")]
184pub enum LibraryPreviewContentFormat {
185 PlainText,
186 Markdown,
187}
188
189#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
190pub struct LibraryDocumentSectionPreview {
191 pub document_id: i64,
192 pub section_key: String,
193 pub section_label: String,
194 pub sort_order: i32,
195 pub title: String,
196 pub preview_text: String,
197 #[serde(default = "default_preview_content_format")]
198 pub content_format: LibraryPreviewContentFormat,
199}
200
201#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
202pub struct LibraryFileDetailResponse {
203 pub file_id: Uuid,
204 pub group_key: String,
205 pub group_path: String,
206 pub visibility: Visibility,
207 #[serde(default)]
208 pub folder_id: Option<Uuid>,
209 pub folder_path: String,
210 pub filename: String,
211 pub media_type: String,
212 pub size_bytes: i64,
213 pub sha256: String,
214 pub ingest_status: LibraryIngestStatus,
215 #[serde(default, skip_serializing_if = "Option::is_none")]
216 pub error_message: Option<String>,
217 pub created_at: DateTime<Utc>,
218 pub updated_at: DateTime<Utc>,
219 #[serde(default, skip_serializing_if = "Option::is_none")]
220 pub ingested_at: Option<DateTime<Utc>>,
221 pub sections: Vec<LibraryDocumentSectionPreview>,
222 pub jobs: Vec<LibraryIngestJobResponse>,
223}
224
225#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
226pub struct LibraryUploadResponse {
227 pub files: Vec<LibraryFileSummary>,
228 pub jobs: Vec<LibraryIngestJobResponse>,
229}
230
231fn default_preview_content_format() -> LibraryPreviewContentFormat {
232 LibraryPreviewContentFormat::PlainText
233}
234
235fn default_text_content_format() -> LibraryTextContentFormat {
236 LibraryTextContentFormat::PlainText
237}
238
239fn default_metadata_json() -> Value {
240 json!({})
241}