mineru 0.5.0

Rust client, CLI, and API server for OpenAI-compatible MinerU document parsing
Documentation
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! Strict wire DTOs for the official MinerU 4.2.3 task API.
//!
//! The upstream contract is published as Pydantic models in
//! `mineru/tasks/contracts.py`, `mineru/task_types.py`, and
//! `mineru/parser/api_server.py`. Every payload here mirrors one of them and is
//! emitted with the same `exclude_none` semantics: absent optionals are omitted,
//! zero values and empty containers are kept.

use axum::{
    Json,
    http::StatusCode,
    response::{IntoResponse, Response},
};
use serde::{Deserialize, Serialize};
use std::fmt;

/// `MAX_TASK_FILES` in `mineru/tasks/contracts.py`.
pub const MAX_TASK_FILES: usize = 100;
/// `TASK_UPLOAD_TTL` in `mineru/tasks/service.py`.
pub const TASK_UPLOAD_TTL_SECS: u64 = 3600;
/// Upper bound for the `x-mineru-task-lease-seconds` request header.
pub const MAX_LEASE_SECS: u64 = 3600;
/// `TaskFileInput.file_name` maximum length.
pub const MAX_FILE_NAME: usize = 1024;
/// `TASK_OUTPUT_FORMATS` in `mineru/task_types.py`.
pub const TASK_OUTPUT_FORMATS: [&str; 4] = ["default", "docx", "html", "latex"];
/// `HealthFeatures.sources`.
pub const SOURCES: [&str; 2] = ["upload", "url"];

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum TaskStatus {
    WaitingFile,
    Pending,
    Running,
    Completed,
    Partial,
    Failed,
    Canceled,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum TaskFileState {
    WaitingFile,
    Pending,
    Running,
    Completed,
    Failed,
    Canceled,
}

impl TaskFileState {
    pub fn is_terminal(self) -> bool {
        matches!(self, Self::Completed | Self::Failed | Self::Canceled)
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TaskOutputFormat {
    Default,
    Docx,
    Html,
    Latex,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FilePurpose {
    Parse,
    ParseOutput,
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Tier {
    Flash,
    Basic,
    Standard,
    Advanced,
}

#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum OcrMode {
    #[default]
    Auto,
    Txt,
    Ocr,
}

#[derive(Clone, Debug, Serialize)]
pub struct ErrorDetail {
    pub code: String,
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub param: Option<String>,
}

#[derive(Clone, Debug, Serialize)]
pub struct ErrorResponse {
    pub error: ErrorDetail,
}

#[derive(Clone, Debug)]
pub struct WireError {
    pub status: StatusCode,
    pub code: &'static str,
    pub message: String,
    pub param: Option<String>,
}

pub mod code {
    pub const INVALID_REQUEST: &str = "invalid_request";
    pub const INTERNAL_ERROR: &str = "internal_error";
    pub const API_ERROR: &str = "api_error";
    pub const MODEL_NOT_FOUND: &str = "model_not_found";
    pub const UPLOAD_SIZE_MISMATCH: &str = "upload_size_mismatch";
    pub const FILE_HASH_MISMATCH: &str = "file_hash_mismatch";
    pub const TASK_ALREADY_TERMINAL: &str = "task_already_terminal";
    pub const UPLOAD_INTERRUPTED: &str = "upload_interrupted";
    pub const REQUEST_INTERRUPTED: &str = "request_interrupted";
    pub const QUALITY_TIER_UNAVAILABLE: &str = "quality_tier_unavailable";
    pub const TASK_NOT_FOUND: &str = "task_not_found";
    pub const UPLOAD_NOT_FOUND: &str = "upload_not_found";
    pub const PAGE_RANGE_INVALID: &str = "page_range_invalid";
}

impl WireError {
    pub fn new(
        status: StatusCode,
        code: &'static str,
        message: impl Into<String>,
        param: Option<String>,
    ) -> Self {
        Self {
            status,
            code,
            message: message.into(),
            param,
        }
    }

    /// The standard `invalid_task_request` constructor: 400 `invalid_request`.
    pub fn invalid(message: impl Into<String>, param: impl Into<String>) -> Self {
        Self::new(
            StatusCode::BAD_REQUEST,
            code::INVALID_REQUEST,
            message,
            Some(param.into()),
        )
    }

    pub fn invalid_unattributed(message: impl Into<String>) -> Self {
        Self::new(
            StatusCode::BAD_REQUEST,
            code::INVALID_REQUEST,
            message,
            None,
        )
    }

    /// Unknown or foreign task id: upstream answers 404 `task_not_found`.
    pub fn task_not_found(task_id: &str) -> Self {
        Self::new(
            StatusCode::NOT_FOUND,
            code::TASK_NOT_FOUND,
            format!("Task {task_id} not found"),
            None,
        )
    }

    /// Unknown `file_id` in a task upload: upstream answers 404 `upload_not_found`.
    pub fn upload_not_found() -> Self {
        Self::new(
            StatusCode::NOT_FOUND,
            code::UPLOAD_NOT_FOUND,
            "Unknown file_id",
            None,
        )
    }

    pub fn internal() -> Self {
        Self::new(
            StatusCode::INTERNAL_SERVER_ERROR,
            code::INTERNAL_ERROR,
            "Internal server error",
            None,
        )
    }

    pub fn too_large() -> Self {
        Self::new(
            StatusCode::PAYLOAD_TOO_LARGE,
            code::INVALID_REQUEST,
            "The request exceeds the allowed size.",
            None,
        )
    }
}

impl fmt::Display for WireError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.message)
    }
}

impl std::error::Error for WireError {}

impl IntoResponse for WireError {
    fn into_response(self) -> Response {
        let body = ErrorResponse {
            error: ErrorDetail {
                code: self.code.into(),
                message: self.message,
                param: self.param,
            },
        };
        (self.status, Json(body)).into_response()
    }
}

/// Mirrors `validate_parse_filename`: a logical basename, never a path.
pub fn validate_file_name(name: &str) -> Result<(), WireError> {
    let fail = || WireError::invalid("Invalid file_name.", "file_name");
    if name.is_empty() || name.len() > MAX_FILE_NAME {
        return Err(fail());
    }
    if name == "." || name == ".." {
        return Err(fail());
    }
    if name
        .chars()
        .any(|c| matches!(c, '/' | '\\' | ':') || (c.is_control() && c != '\t'))
    {
        return Err(fail());
    }
    Ok(())
}

pub fn validate_hash(hash: &str) -> Result<(), WireError> {
    if hash.len() != 64
        || !hash
            .bytes()
            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
    {
        return Err(WireError::invalid(
            "Expected a lowercase SHA-256 hex digest.",
            "sha256sum",
        ));
    }
    Ok(())
}

pub(crate) fn validate_page_range(value: &str) -> Result<(), WireError> {
    let raw = value;
    let value = value.trim();
    // Upstream's default is the empty string, meaning every page.
    if value.is_empty() || value == "all" {
        return Ok(());
    }
    for part in value.split(',') {
        let endpoints: Vec<_> = part.split('-').map(str::trim).collect();
        if endpoints.is_empty() || endpoints.len() > 2 {
            return Err(range_error(
                raw,
                "expected a page, an inclusive range, or 'all'",
            ));
        }
        let first = page_endpoint(endpoints[0], raw)?;
        let last = endpoints
            .get(1)
            .map(|v| page_endpoint(v, raw))
            .transpose()?;
        if let Some((relative, end)) = last {
            let (start_relative, start) = first;
            let invalid_order = match (start_relative, relative) {
                (true, true) => start < end,
                (false, false) => start > end,
                _ => false,
            };
            if invalid_order {
                return Err(range_error(raw, "reversed ranges are not supported"));
            }
        }
    }
    Ok(())
}

/// Upstream `InvalidRequestError('page_range_invalid', ...)`.
fn range_error(raw: &str, reason: &str) -> WireError {
    WireError::new(
        StatusCode::BAD_REQUEST,
        code::PAGE_RANGE_INVALID,
        format!("Invalid page range '{raw}': {reason}"),
        Some("page_range".to_string()),
    )
}

fn page_endpoint(value: &str, raw: &str) -> Result<(bool, u32), WireError> {
    let (relative, digits) = value
        .strip_prefix('r')
        .map_or((false, value), |v| (true, v));
    if digits.is_empty() || digits.starts_with('0') || !digits.bytes().all(|b| b.is_ascii_digit()) {
        return Err(range_error(
            raw,
            "expected a page, an inclusive range, or 'all'",
        ));
    }
    Ok((
        relative,
        digits
            .parse()
            .map_err(|_| range_error(raw, "expected a page, an inclusive range, or 'all'"))?,
    ))
}
mod task;

pub use task::*;

// ---------------------------------------------------------------------------
// Discovery and file objects
// ---------------------------------------------------------------------------

#[derive(Clone, Debug, Serialize)]
pub struct HealthFeatures {
    pub task_output_formats: Vec<&'static str>,
    pub sources: Vec<&'static str>,
}

impl Default for HealthFeatures {
    fn default() -> Self {
        Self {
            task_output_formats: TASK_OUTPUT_FORMATS.to_vec(),
            sources: SOURCES.to_vec(),
        }
    }
}

#[derive(Clone, Debug, Serialize)]
pub struct HealthRuntime {
    pub device: &'static str,
}

#[derive(Clone, Debug, Serialize)]
pub struct Health {
    pub status: &'static str,
    pub version: &'static str,
    pub features: HealthFeatures,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub runtime: Option<HealthRuntime>,
}

impl Default for Health {
    fn default() -> Self {
        Self {
            status: "ok",
            version: crate::official_worker::PACKAGE_VERSION,
            features: HealthFeatures::default(),
            runtime: Some(HealthRuntime { device: "cpu" }),
        }
    }
}

#[derive(Clone, Debug, Serialize)]
pub struct ModelInfo {
    pub id: String,
    pub object: &'static str,
    pub created: i64,
    pub owned_by: &'static str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

#[derive(Clone, Debug, Serialize)]
pub struct ModelListResponse {
    pub object: &'static str,
    pub data: Vec<ModelInfo>,
}

#[derive(Clone, Debug, Serialize)]
pub struct TierInfo {
    pub id: Tier,
    pub description: &'static str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub current_model: Option<String>,
}

#[derive(Clone, Debug, Serialize)]
pub struct TierListResponse {
    pub object: &'static str,
    pub data: Vec<TierInfo>,
}

/// `FileObjectModel`.
#[derive(Clone, Debug, Serialize)]
pub struct FileObjectModel {
    pub id: String,
    pub object: &'static str,
    pub bytes: u64,
    pub created_at: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<i64>,
    pub file_name: String,
    pub purpose: FilePurpose,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sha256sum: Option<String>,
}

impl FileObjectModel {
    pub const OBJECT: &'static str = "file";
}

/// The tier descriptions upstream publishes verbatim.
pub fn tier_description(tier: Tier) -> &'static str {
    match tier {
        Tier::Flash => "Fast local text extraction.",
        Tier::Basic => "Basic parsing with local lightweight models.",
        Tier::Standard => "Standard parsing for most documents.",
        Tier::Advanced => "Advanced parsing for difficult documents.",
    }
}

#[cfg(test)]
#[path = "wire_tests.rs"]
mod tests;