wellfriend-document-sdk 1.0.0

Official async Rust SDK for the Wellfriend document API: typed document tools, pipelines, streaming uploads and downloads, retries and webhook verification.
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
428
429
430
431
432
433
434
435
436
437
438
439
//! Typed models of the core resources whose shapes the API document leaves open.
//!
//! Every model keeps fields it does not know in `extra`, so nothing the API sends is lost.

use serde::{Deserialize, Serialize};

use crate::generated::models::Upload;
use crate::Object;

string_enum! {
    /// The state of a document operation.
    OperationState {
        Waiting => "waiting",
        Queued => "queued",
        Running => "running",
        Completed => "completed",
        Failed => "failed",
        Cancelled => "cancelled",
    }
}

impl OperationState {
    /// Whether the operation has finished: completed, failed, or
    /// cancelled (state `cancelled`, also accepted as `canceled`).
    pub fn is_terminal(&self) -> bool {
        matches!(
            self,
            OperationState::Completed | OperationState::Failed | OperationState::Cancelled
        ) || matches!(self, OperationState::Unknown(value) if value == "canceled" || value == "dead_letter")
    }
}

string_enum! {
    /// Where an operation's output went.
    OutputKind {
        /// A new version of the input document.
        Version => "version",
        /// A new, derived document.
        Artifact => "artifact",
    }
}

/// A document stored in the workspace.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Document {
    /// The document id (`doc_...`).
    pub id: String,
    /// The title, by default the uploaded file name.
    #[serde(default)]
    pub title: String,
    /// The classification, for example `private`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub classification: Option<String>,
    /// The lifecycle state, for example `READY`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub lifecycle_state: Option<String>,
    /// `live` or `test`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub environment: Option<String>,
    /// The current immutable version.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub current_version: Option<DocumentVersion>,
    /// When the document was created (RFC 3339).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub created_at: Option<String>,
    /// When the document last changed (RFC 3339).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<String>,
    /// Fields this SDK version does not model.
    #[serde(flatten)]
    pub extra: Object,
}

impl Document {
    /// The id of the current version, when the document has one.
    pub fn current_version_id(&self) -> Option<&str> {
        self.current_version
            .as_ref()
            .map(|version| version.id.as_str())
    }
}

/// One immutable version of a document.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DocumentVersion {
    /// The version id (`ver_...`).
    pub id: String,
    /// 1 for the upload, then one more for each new version.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version_number: Option<i64>,
    /// What created the version, for example `ingest` or a tool id.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub operation: Option<String>,
    /// The content type, for example `application/pdf`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mime_type: Option<String>,
    /// The size in bytes.
    #[serde(
        default,
        deserialize_with = "crate::util::u64_lenient",
        skip_serializing_if = "Option::is_none"
    )]
    pub size_bytes: Option<u64>,
    /// The SHA-256 of the content, in hex.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sha256: Option<String>,
    /// The validation state, for example `VALID`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub validation_state: Option<String>,
    /// When the version was created (RFC 3339).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub created_at: Option<String>,
    /// Fields this SDK version does not model.
    #[serde(flatten)]
    pub extra: Object,
}

/// A document operation: one tool run on one or more document versions.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Operation {
    /// The operation id.
    pub id: String,
    /// The tool that runs.
    #[serde(default)]
    pub tool_id: crate::ToolId,
    /// The current state.
    #[serde(default)]
    pub state: OperationState,
    /// The input document (absent while the operation waits on another one).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub document_id: Option<String>,
    /// The input version.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub input_version_id: Option<String>,
    /// The document holding the output, once completed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub output_document_id: Option<String>,
    /// The output version, once completed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub output_version_id: Option<String>,
    /// Whether the output is a new version of the input or a new document.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub output_kind: Option<OutputKind>,
    /// The tool parameters as stored (secrets are never returned).
    #[serde(default, deserialize_with = "crate::util::null_default")]
    pub parameters: Object,
    /// The tool's result details, for example compression statistics.
    #[serde(default, deserialize_with = "crate::util::null_default")]
    pub result: Object,
    /// Why the operation failed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error_code: Option<String>,
    /// A readable explanation of the failure.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error_message: Option<String>,
    /// When the operation was accepted (RFC 3339).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub created_at: Option<String>,
    /// When processing started.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub started_at: Option<String>,
    /// When the operation finished.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub completed_at: Option<String>,
    /// True when an identical earlier result was returned without processing again.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reused: Option<bool>,
    /// True when a damaged input PDF was repaired before the tool ran.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub input_repaired: Option<bool>,
    /// Further inputs, for tools that read several files.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub inputs: Option<Vec<VersionRef>>,
    /// The earlier operation whose output this one reads.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source: Option<Object>,
    /// Fields this SDK version does not model.
    #[serde(flatten)]
    pub extra: Object,
}

impl Operation {
    /// The output file name the tool reported, when it reported one.
    pub fn output_file_name(&self) -> Option<&str> {
        self.result.get("fileName").and_then(|value| value.as_str())
    }
}

/// A document version reference.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VersionRef {
    /// The document id.
    pub document_id: String,
    /// The version id; omitted means the current version.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version_id: Option<String>,
}

/// One output file of an operation.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Output {
    /// 0 for the whole output; 1 and up for the files inside a ZIP output.
    #[serde(default)]
    pub index: u32,
    /// The document holding this output (for a part inside a ZIP, once it has been read).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub document_id: Option<String>,
    /// The version holding this output.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version_id: Option<String>,
    /// The file name.
    #[serde(default, rename = "name")]
    pub filename: String,
    /// The content type.
    #[serde(default, rename = "mimeType")]
    pub content_type: String,
    /// The size in bytes.
    #[serde(
        default,
        deserialize_with = "crate::util::u64_lenient",
        skip_serializing_if = "Option::is_none"
    )]
    pub size_bytes: Option<u64>,
    /// Fields this SDK version does not model.
    #[serde(flatten)]
    pub extra: Object,
}

/// The outputs of an operation.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OperationOutputs {
    /// The operation id.
    #[serde(default)]
    pub operation_id: String,
    /// Index 0 is the whole output; a ZIP output lists its files from index 1.
    #[serde(default, deserialize_with = "crate::util::null_default")]
    pub outputs: Vec<Output>,
    /// Fields this SDK version does not model.
    #[serde(flatten)]
    pub extra: Object,
}

string_enum! {
    /// The status of a workflow definition.
    WorkflowStatus {
        Draft => "DRAFT",
        Published => "PUBLISHED",
        Archived => "ARCHIVED",
    }
}

/// A workflow definition.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Workflow {
    /// The workflow id (`wf_...`).
    pub id: String,
    /// The name.
    #[serde(default)]
    pub name: String,
    /// The description.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// `DRAFT` until published.
    #[serde(default)]
    pub status: WorkflowStatus,
    /// `live` or `test`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub environment: Option<String>,
    /// The trigger definition.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub trigger: Option<serde_json::Value>,
    /// The workflow graph (`wellfriend-workflow/v2`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub graph: Option<serde_json::Value>,
    /// The draft's version number.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub current_version: Option<i64>,
    /// When the workflow was created (RFC 3339).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub created_at: Option<String>,
    /// When the workflow last changed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<String>,
    /// When it was last published.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub published_at: Option<String>,
    /// Fields this SDK version does not model.
    #[serde(flatten)]
    pub extra: Object,
}

string_enum! {
    /// The state of a workflow run or step.
    RunState {
        Pending => "PENDING",
        Running => "RUNNING",
        Waiting => "WAITING",
        Succeeded => "SUCCEEDED",
        Failed => "FAILED",
        Cancelled => "CANCELLED",
        Skipped => "SKIPPED",
    }
}

impl RunState {
    /// Whether the run has finished, successfully or not.
    pub fn is_terminal(&self) -> bool {
        matches!(
            self,
            RunState::Succeeded | RunState::Failed | RunState::Cancelled
        )
    }
}

/// One run of a workflow.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WorkflowRun {
    /// The run id (`wfrun_...`).
    pub id: String,
    /// The workflow.
    #[serde(default)]
    pub workflow_id: String,
    /// The published version that runs.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub workflow_version_id: Option<String>,
    /// The current state.
    #[serde(default)]
    pub state: RunState,
    /// The run input.
    #[serde(default, deserialize_with = "crate::util::null_default")]
    pub input: Object,
    /// The run output.
    #[serde(default, deserialize_with = "crate::util::null_default")]
    pub output: Object,
    /// The failure, when the run failed (`{"code": ...}`).
    #[serde(default, deserialize_with = "crate::util::null_default")]
    pub error: Object,
    /// The idempotency key the run was started with.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub idempotency_key: Option<String>,
    /// The steps, one per graph node.
    #[serde(default, deserialize_with = "crate::util::null_default")]
    pub steps: Vec<WorkflowStep>,
    /// When the run was created (RFC 3339).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub created_at: Option<String>,
    /// When it started.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub started_at: Option<String>,
    /// When it finished.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub completed_at: Option<String>,
    /// When it was cancelled.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cancelled_at: Option<String>,
    /// Fields this SDK version does not model.
    #[serde(flatten)]
    pub extra: Object,
}

impl WorkflowRun {
    /// The failure code, when the run failed.
    pub fn error_code(&self) -> Option<&str> {
        self.error.get("code").and_then(|value| value.as_str())
    }

    /// The step for a graph node.
    pub fn step(&self, node_id: &str) -> Option<&WorkflowStep> {
        self.steps.iter().find(|step| step.node_id == node_id)
    }
}

/// One step of a workflow run.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WorkflowStep {
    /// The step id.
    pub id: String,
    /// The graph node this step runs.
    #[serde(default)]
    pub node_id: String,
    /// `action`, `completion`, `wait`, `branch` and so on.
    #[serde(default)]
    pub node_type: String,
    /// The action of an action node, for example `document-operations.create`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub action: Option<String>,
    /// The step state.
    #[serde(default)]
    pub state: RunState,
    /// The approval to decide, for an approval step.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub approval_id: Option<String>,
    /// The attempt number.
    #[serde(default)]
    pub attempt: i64,
    /// The step input.
    #[serde(default, deserialize_with = "crate::util::null_default")]
    pub input: Object,
    /// The step output; for a tool step, the operation.
    #[serde(default, deserialize_with = "crate::util::null_default")]
    pub output: Object,
    /// The failure, when the step failed.
    #[serde(default, deserialize_with = "crate::util::null_default")]
    pub error: Object,
    /// When the step started.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub started_at: Option<String>,
    /// When the step finished.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub completed_at: Option<String>,
    /// Fields this SDK version does not model.
    #[serde(flatten)]
    pub extra: Object,
}

/// The answer to completing a resumable upload.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum UploadCompletion {
    /// The document was created (or already existed for this upload).
    Document {
        /// The new document.
        document: Document,
        /// False when the upload had already been completed.
        #[serde(default)]
        created: bool,
    },
    /// The parts are still being assembled: poll the upload until it is `completed`.
    Pending(Upload),
}