Skip to main content

wellfriend/
models.rs

1//! Typed models of the core resources whose shapes the API document leaves open.
2//!
3//! Every model keeps fields it does not know in `extra`, so nothing the API sends is lost.
4
5use serde::{Deserialize, Serialize};
6
7use crate::generated::models::Upload;
8use crate::Object;
9
10string_enum! {
11    /// The state of a document operation.
12    OperationState {
13        Waiting => "waiting",
14        Queued => "queued",
15        Running => "running",
16        Completed => "completed",
17        Failed => "failed",
18        Cancelled => "cancelled",
19    }
20}
21
22impl OperationState {
23    /// Whether the operation has finished: completed, failed, or
24    /// cancelled (state `cancelled`, also accepted as `canceled`).
25    pub fn is_terminal(&self) -> bool {
26        matches!(
27            self,
28            OperationState::Completed | OperationState::Failed | OperationState::Cancelled
29        ) || matches!(self, OperationState::Unknown(value) if value == "canceled" || value == "dead_letter")
30    }
31}
32
33string_enum! {
34    /// Where an operation's output went.
35    OutputKind {
36        /// A new version of the input document.
37        Version => "version",
38        /// A new, derived document.
39        Artifact => "artifact",
40    }
41}
42
43/// A document stored in the workspace.
44#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
45#[serde(rename_all = "camelCase")]
46pub struct Document {
47    /// The document id (`doc_...`).
48    pub id: String,
49    /// The title, by default the uploaded file name.
50    #[serde(default)]
51    pub title: String,
52    /// The classification, for example `private`.
53    #[serde(default, skip_serializing_if = "Option::is_none")]
54    pub classification: Option<String>,
55    /// The lifecycle state, for example `READY`.
56    #[serde(default, skip_serializing_if = "Option::is_none")]
57    pub lifecycle_state: Option<String>,
58    /// `live` or `test`.
59    #[serde(default, skip_serializing_if = "Option::is_none")]
60    pub environment: Option<String>,
61    /// The current immutable version.
62    #[serde(default, skip_serializing_if = "Option::is_none")]
63    pub current_version: Option<DocumentVersion>,
64    /// When the document was created (RFC 3339).
65    #[serde(default, skip_serializing_if = "Option::is_none")]
66    pub created_at: Option<String>,
67    /// When the document last changed (RFC 3339).
68    #[serde(default, skip_serializing_if = "Option::is_none")]
69    pub updated_at: Option<String>,
70    /// Fields this SDK version does not model.
71    #[serde(flatten)]
72    pub extra: Object,
73}
74
75impl Document {
76    /// The id of the current version, when the document has one.
77    pub fn current_version_id(&self) -> Option<&str> {
78        self.current_version
79            .as_ref()
80            .map(|version| version.id.as_str())
81    }
82}
83
84/// One immutable version of a document.
85#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
86#[serde(rename_all = "camelCase")]
87pub struct DocumentVersion {
88    /// The version id (`ver_...`).
89    pub id: String,
90    /// 1 for the upload, then one more for each new version.
91    #[serde(default, skip_serializing_if = "Option::is_none")]
92    pub version_number: Option<i64>,
93    /// What created the version, for example `ingest` or a tool id.
94    #[serde(default, skip_serializing_if = "Option::is_none")]
95    pub operation: Option<String>,
96    /// The content type, for example `application/pdf`.
97    #[serde(default, skip_serializing_if = "Option::is_none")]
98    pub mime_type: Option<String>,
99    /// The size in bytes.
100    #[serde(
101        default,
102        deserialize_with = "crate::util::u64_lenient",
103        skip_serializing_if = "Option::is_none"
104    )]
105    pub size_bytes: Option<u64>,
106    /// The SHA-256 of the content, in hex.
107    #[serde(default, skip_serializing_if = "Option::is_none")]
108    pub sha256: Option<String>,
109    /// The validation state, for example `VALID`.
110    #[serde(default, skip_serializing_if = "Option::is_none")]
111    pub validation_state: Option<String>,
112    /// When the version was created (RFC 3339).
113    #[serde(default, skip_serializing_if = "Option::is_none")]
114    pub created_at: Option<String>,
115    /// Fields this SDK version does not model.
116    #[serde(flatten)]
117    pub extra: Object,
118}
119
120/// A document operation: one tool run on one or more document versions.
121#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
122#[serde(rename_all = "camelCase")]
123pub struct Operation {
124    /// The operation id.
125    pub id: String,
126    /// The tool that runs.
127    #[serde(default)]
128    pub tool_id: crate::ToolId,
129    /// The current state.
130    #[serde(default)]
131    pub state: OperationState,
132    /// The input document (absent while the operation waits on another one).
133    #[serde(default, skip_serializing_if = "Option::is_none")]
134    pub document_id: Option<String>,
135    /// The input version.
136    #[serde(default, skip_serializing_if = "Option::is_none")]
137    pub input_version_id: Option<String>,
138    /// The document holding the output, once completed.
139    #[serde(default, skip_serializing_if = "Option::is_none")]
140    pub output_document_id: Option<String>,
141    /// The output version, once completed.
142    #[serde(default, skip_serializing_if = "Option::is_none")]
143    pub output_version_id: Option<String>,
144    /// Whether the output is a new version of the input or a new document.
145    #[serde(default, skip_serializing_if = "Option::is_none")]
146    pub output_kind: Option<OutputKind>,
147    /// The tool parameters as stored (secrets are never returned).
148    #[serde(default, deserialize_with = "crate::util::null_default")]
149    pub parameters: Object,
150    /// The tool's result details, for example compression statistics.
151    #[serde(default, deserialize_with = "crate::util::null_default")]
152    pub result: Object,
153    /// Why the operation failed.
154    #[serde(default, skip_serializing_if = "Option::is_none")]
155    pub error_code: Option<String>,
156    /// A readable explanation of the failure.
157    #[serde(default, skip_serializing_if = "Option::is_none")]
158    pub error_message: Option<String>,
159    /// When the operation was accepted (RFC 3339).
160    #[serde(default, skip_serializing_if = "Option::is_none")]
161    pub created_at: Option<String>,
162    /// When processing started.
163    #[serde(default, skip_serializing_if = "Option::is_none")]
164    pub started_at: Option<String>,
165    /// When the operation finished.
166    #[serde(default, skip_serializing_if = "Option::is_none")]
167    pub completed_at: Option<String>,
168    /// True when an identical earlier result was returned without processing again.
169    #[serde(default, skip_serializing_if = "Option::is_none")]
170    pub reused: Option<bool>,
171    /// True when a damaged input PDF was repaired before the tool ran.
172    #[serde(default, skip_serializing_if = "Option::is_none")]
173    pub input_repaired: Option<bool>,
174    /// Further inputs, for tools that read several files.
175    #[serde(default, skip_serializing_if = "Option::is_none")]
176    pub inputs: Option<Vec<VersionRef>>,
177    /// The earlier operation whose output this one reads.
178    #[serde(default, skip_serializing_if = "Option::is_none")]
179    pub source: Option<Object>,
180    /// Fields this SDK version does not model.
181    #[serde(flatten)]
182    pub extra: Object,
183}
184
185impl Operation {
186    /// The output file name the tool reported, when it reported one.
187    pub fn output_file_name(&self) -> Option<&str> {
188        self.result.get("fileName").and_then(|value| value.as_str())
189    }
190}
191
192/// A document version reference.
193#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
194#[serde(rename_all = "camelCase")]
195pub struct VersionRef {
196    /// The document id.
197    pub document_id: String,
198    /// The version id; omitted means the current version.
199    #[serde(default, skip_serializing_if = "Option::is_none")]
200    pub version_id: Option<String>,
201}
202
203/// One output file of an operation.
204#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
205#[serde(rename_all = "camelCase")]
206pub struct Output {
207    /// 0 for the whole output; 1 and up for the files inside a ZIP output.
208    #[serde(default)]
209    pub index: u32,
210    /// The document holding this output (for a part inside a ZIP, once it has been read).
211    #[serde(default, skip_serializing_if = "Option::is_none")]
212    pub document_id: Option<String>,
213    /// The version holding this output.
214    #[serde(default, skip_serializing_if = "Option::is_none")]
215    pub version_id: Option<String>,
216    /// The file name.
217    #[serde(default, rename = "name")]
218    pub filename: String,
219    /// The content type.
220    #[serde(default, rename = "mimeType")]
221    pub content_type: String,
222    /// The size in bytes.
223    #[serde(
224        default,
225        deserialize_with = "crate::util::u64_lenient",
226        skip_serializing_if = "Option::is_none"
227    )]
228    pub size_bytes: Option<u64>,
229    /// Fields this SDK version does not model.
230    #[serde(flatten)]
231    pub extra: Object,
232}
233
234/// The outputs of an operation.
235#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
236#[serde(rename_all = "camelCase")]
237pub struct OperationOutputs {
238    /// The operation id.
239    #[serde(default)]
240    pub operation_id: String,
241    /// Index 0 is the whole output; a ZIP output lists its files from index 1.
242    #[serde(default, deserialize_with = "crate::util::null_default")]
243    pub outputs: Vec<Output>,
244    /// Fields this SDK version does not model.
245    #[serde(flatten)]
246    pub extra: Object,
247}
248
249string_enum! {
250    /// The status of a workflow definition.
251    WorkflowStatus {
252        Draft => "DRAFT",
253        Published => "PUBLISHED",
254        Archived => "ARCHIVED",
255    }
256}
257
258/// A workflow definition.
259#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
260#[serde(rename_all = "camelCase")]
261pub struct Workflow {
262    /// The workflow id (`wf_...`).
263    pub id: String,
264    /// The name.
265    #[serde(default)]
266    pub name: String,
267    /// The description.
268    #[serde(default, skip_serializing_if = "Option::is_none")]
269    pub description: Option<String>,
270    /// `DRAFT` until published.
271    #[serde(default)]
272    pub status: WorkflowStatus,
273    /// `live` or `test`.
274    #[serde(default, skip_serializing_if = "Option::is_none")]
275    pub environment: Option<String>,
276    /// The trigger definition.
277    #[serde(default, skip_serializing_if = "Option::is_none")]
278    pub trigger: Option<serde_json::Value>,
279    /// The workflow graph (`wellfriend-workflow/v2`).
280    #[serde(default, skip_serializing_if = "Option::is_none")]
281    pub graph: Option<serde_json::Value>,
282    /// The draft's version number.
283    #[serde(default, skip_serializing_if = "Option::is_none")]
284    pub current_version: Option<i64>,
285    /// When the workflow was created (RFC 3339).
286    #[serde(default, skip_serializing_if = "Option::is_none")]
287    pub created_at: Option<String>,
288    /// When the workflow last changed.
289    #[serde(default, skip_serializing_if = "Option::is_none")]
290    pub updated_at: Option<String>,
291    /// When it was last published.
292    #[serde(default, skip_serializing_if = "Option::is_none")]
293    pub published_at: Option<String>,
294    /// Fields this SDK version does not model.
295    #[serde(flatten)]
296    pub extra: Object,
297}
298
299string_enum! {
300    /// The state of a workflow run or step.
301    RunState {
302        Pending => "PENDING",
303        Running => "RUNNING",
304        Waiting => "WAITING",
305        Succeeded => "SUCCEEDED",
306        Failed => "FAILED",
307        Cancelled => "CANCELLED",
308        Skipped => "SKIPPED",
309    }
310}
311
312impl RunState {
313    /// Whether the run has finished, successfully or not.
314    pub fn is_terminal(&self) -> bool {
315        matches!(
316            self,
317            RunState::Succeeded | RunState::Failed | RunState::Cancelled
318        )
319    }
320}
321
322/// One run of a workflow.
323#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
324#[serde(rename_all = "camelCase")]
325pub struct WorkflowRun {
326    /// The run id (`wfrun_...`).
327    pub id: String,
328    /// The workflow.
329    #[serde(default)]
330    pub workflow_id: String,
331    /// The published version that runs.
332    #[serde(default, skip_serializing_if = "Option::is_none")]
333    pub workflow_version_id: Option<String>,
334    /// The current state.
335    #[serde(default)]
336    pub state: RunState,
337    /// The run input.
338    #[serde(default, deserialize_with = "crate::util::null_default")]
339    pub input: Object,
340    /// The run output.
341    #[serde(default, deserialize_with = "crate::util::null_default")]
342    pub output: Object,
343    /// The failure, when the run failed (`{"code": ...}`).
344    #[serde(default, deserialize_with = "crate::util::null_default")]
345    pub error: Object,
346    /// The idempotency key the run was started with.
347    #[serde(default, skip_serializing_if = "Option::is_none")]
348    pub idempotency_key: Option<String>,
349    /// The steps, one per graph node.
350    #[serde(default, deserialize_with = "crate::util::null_default")]
351    pub steps: Vec<WorkflowStep>,
352    /// When the run was created (RFC 3339).
353    #[serde(default, skip_serializing_if = "Option::is_none")]
354    pub created_at: Option<String>,
355    /// When it started.
356    #[serde(default, skip_serializing_if = "Option::is_none")]
357    pub started_at: Option<String>,
358    /// When it finished.
359    #[serde(default, skip_serializing_if = "Option::is_none")]
360    pub completed_at: Option<String>,
361    /// When it was cancelled.
362    #[serde(default, skip_serializing_if = "Option::is_none")]
363    pub cancelled_at: Option<String>,
364    /// Fields this SDK version does not model.
365    #[serde(flatten)]
366    pub extra: Object,
367}
368
369impl WorkflowRun {
370    /// The failure code, when the run failed.
371    pub fn error_code(&self) -> Option<&str> {
372        self.error.get("code").and_then(|value| value.as_str())
373    }
374
375    /// The step for a graph node.
376    pub fn step(&self, node_id: &str) -> Option<&WorkflowStep> {
377        self.steps.iter().find(|step| step.node_id == node_id)
378    }
379}
380
381/// One step of a workflow run.
382#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
383#[serde(rename_all = "camelCase")]
384pub struct WorkflowStep {
385    /// The step id.
386    pub id: String,
387    /// The graph node this step runs.
388    #[serde(default)]
389    pub node_id: String,
390    /// `action`, `completion`, `wait`, `branch` and so on.
391    #[serde(default)]
392    pub node_type: String,
393    /// The action of an action node, for example `document-operations.create`.
394    #[serde(default, skip_serializing_if = "Option::is_none")]
395    pub action: Option<String>,
396    /// The step state.
397    #[serde(default)]
398    pub state: RunState,
399    /// The approval to decide, for an approval step.
400    #[serde(default, skip_serializing_if = "Option::is_none")]
401    pub approval_id: Option<String>,
402    /// The attempt number.
403    #[serde(default)]
404    pub attempt: i64,
405    /// The step input.
406    #[serde(default, deserialize_with = "crate::util::null_default")]
407    pub input: Object,
408    /// The step output; for a tool step, the operation.
409    #[serde(default, deserialize_with = "crate::util::null_default")]
410    pub output: Object,
411    /// The failure, when the step failed.
412    #[serde(default, deserialize_with = "crate::util::null_default")]
413    pub error: Object,
414    /// When the step started.
415    #[serde(default, skip_serializing_if = "Option::is_none")]
416    pub started_at: Option<String>,
417    /// When the step finished.
418    #[serde(default, skip_serializing_if = "Option::is_none")]
419    pub completed_at: Option<String>,
420    /// Fields this SDK version does not model.
421    #[serde(flatten)]
422    pub extra: Object,
423}
424
425/// The answer to completing a resumable upload.
426#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
427#[serde(untagged)]
428pub enum UploadCompletion {
429    /// The document was created (or already existed for this upload).
430    Document {
431        /// The new document.
432        document: Document,
433        /// False when the upload had already been completed.
434        #[serde(default)]
435        created: bool,
436    },
437    /// The parts are still being assembled: poll the upload until it is `completed`.
438    Pending(Upload),
439}