vectoramp 0.1.0

Official Rust client for the VectorAmp public API.
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! Typed builders for ingestion sources.
//!
//! Each builder fills in the required `source_type`, sensible config defaults,
//! and a default name when one is not supplied. The builders all implement
//! [`IntoCreateSourceRequest`] so they can be passed wherever a source
//! definition is expected.

use std::collections::HashMap;

use serde_json::{json, Value};

use crate::types::{CreateSourceRequest, Metadata};

/// Source-type identifier values used by the public API.
pub mod source_type {
    pub const S3: &str = "s3";
    pub const WEB: &str = "web";
    pub const GCS: &str = "gcs";
    pub const GDRIVE: &str = "gdrive";
    pub const JIRA: &str = "jira";
    pub const CONFLUENCE: &str = "confluence";
    pub const FILE_UPLOAD: &str = "file_upload";
}

/// Trait implemented by typed source builders so they can be converted into
/// [`CreateSourceRequest`].
pub trait IntoCreateSourceRequest {
    fn into_create_source_request(self) -> CreateSourceRequest;
}

impl IntoCreateSourceRequest for CreateSourceRequest {
    fn into_create_source_request(self) -> CreateSourceRequest {
        self
    }
}

/// Escape hatch for source types or configuration shapes not yet modeled by
/// this SDK.
#[derive(Debug, Default, Clone)]
pub struct GenericSource {
    pub source_type: String,
    pub name: String,
    pub description: Option<String>,
    pub config: HashMap<String, Value>,
    pub metadata: Option<Metadata>,
}

impl IntoCreateSourceRequest for GenericSource {
    fn into_create_source_request(self) -> CreateSourceRequest {
        CreateSourceRequest {
            source_type: self.source_type,
            name: self.name,
            description: self.description,
            config: self.config,
            metadata: self.metadata,
        }
    }
}

/// CSS selectors used by [`WebSource`] to pick out content from crawled pages.
#[derive(Debug, Default, Clone)]
pub struct WebSelectors {
    pub content: Option<String>,
    pub title: Option<String>,
    pub exclude: Vec<String>,
}

impl WebSelectors {
    fn to_value(&self) -> Value {
        let mut map = serde_json::Map::new();
        if let Some(c) = &self.content {
            map.insert("content".into(), Value::String(c.clone()));
        }
        if let Some(t) = &self.title {
            map.insert("title".into(), Value::String(t.clone()));
        }
        if !self.exclude.is_empty() {
            map.insert(
                "exclude".into(),
                Value::Array(self.exclude.iter().cloned().map(Value::String).collect()),
            );
        }
        Value::Object(map)
    }
}

/// Web crawler ingestion source.
///
/// Name defaults to `web-<host>` from the first start URL, or
/// `rust-sdk-web-source` when no URL is supplied.
#[derive(Debug, Default, Clone)]
pub struct WebSource {
    pub name: Option<String>,
    pub start_urls: Vec<String>,
    pub max_depth: Option<u32>,
    pub max_pages: Option<u32>,
    pub allowed_domains: Vec<String>,
    pub rate_limit_ms: Option<u32>,
    pub respect_robots_txt: Option<bool>,
    pub include_assets: Option<bool>,
    pub max_assets_per_page: Option<u32>,
    pub selectors: Option<WebSelectors>,
    pub headers: HashMap<String, String>,
    pub description: Option<String>,
    pub metadata: Option<Metadata>,
    pub config_extra: HashMap<String, Value>,
}

impl IntoCreateSourceRequest for WebSource {
    fn into_create_source_request(self) -> CreateSourceRequest {
        let mut config: HashMap<String, Value> = HashMap::new();
        config.insert("type".into(), Value::String(source_type::WEB.into()));
        config.insert("start_urls".into(), json!(self.start_urls));
        if let Some(v) = self.max_depth {
            config.insert("max_depth".into(), json!(v));
        }
        if let Some(v) = self.max_pages {
            config.insert("max_pages".into(), json!(v));
        }
        if !self.allowed_domains.is_empty() {
            config.insert("allowed_domains".into(), json!(self.allowed_domains));
        }
        if let Some(v) = self.rate_limit_ms {
            config.insert("rate_limit_ms".into(), json!(v));
        }
        if let Some(v) = self.respect_robots_txt {
            config.insert("respect_robots_txt".into(), json!(v));
        }
        if let Some(v) = self.include_assets {
            config.insert("include_assets".into(), json!(v));
        }
        if let Some(v) = self.max_assets_per_page {
            config.insert("max_assets_per_page".into(), json!(v));
        }
        if let Some(s) = &self.selectors {
            config.insert("selectors".into(), s.to_value());
        }
        if !self.headers.is_empty() {
            let mut h = serde_json::Map::new();
            for (k, v) in &self.headers {
                h.insert(k.clone(), Value::String(v.clone()));
            }
            config.insert("headers".into(), Value::Object(h));
        }
        merge_extra(&mut config, self.config_extra);

        let name = self
            .name
            .unwrap_or_else(|| web_default_name(&self.start_urls));

        CreateSourceRequest {
            source_type: source_type::WEB.into(),
            name,
            description: self.description,
            config,
            metadata: self.metadata,
        }
    }
}

/// Amazon S3 ingestion source.
#[derive(Debug, Default, Clone)]
pub struct S3Source {
    pub name: Option<String>,
    pub bucket: String,
    pub prefix: Option<String>,
    pub region: Option<String>,
    pub access_key_id: Option<String>,
    pub secret_access_key: Option<String>,
    pub file_patterns: Vec<String>,
    pub max_file_size_mb: Option<u32>,
    pub sync_mode: Option<String>,
    pub description: Option<String>,
    pub metadata: Option<Metadata>,
    pub config_extra: HashMap<String, Value>,
}

impl IntoCreateSourceRequest for S3Source {
    fn into_create_source_request(self) -> CreateSourceRequest {
        let mut config: HashMap<String, Value> = HashMap::new();
        config.insert("type".into(), Value::String(source_type::S3.into()));
        config.insert("bucket".into(), Value::String(self.bucket.clone()));
        config.insert(
            "sync_mode".into(),
            Value::String(self.sync_mode.unwrap_or_else(|| "incremental".into())),
        );
        if let Some(v) = self.prefix {
            config.insert("prefix".into(), Value::String(v));
        }
        if let Some(v) = self.region {
            config.insert("region".into(), Value::String(v));
        }
        if let Some(v) = self.access_key_id {
            config.insert("access_key_id".into(), Value::String(v));
        }
        if let Some(v) = self.secret_access_key {
            config.insert("secret_access_key".into(), Value::String(v));
        }
        if !self.file_patterns.is_empty() {
            config.insert("file_patterns".into(), json!(self.file_patterns));
        }
        if let Some(v) = self.max_file_size_mb {
            config.insert("max_file_size_mb".into(), json!(v));
        }
        merge_extra(&mut config, self.config_extra);

        let name = self.name.unwrap_or_else(|| {
            if self.bucket.is_empty() {
                "rust-sdk-s3-source".into()
            } else {
                format!("s3-{}", sanitize(&self.bucket))
            }
        });

        CreateSourceRequest {
            source_type: source_type::S3.into(),
            name,
            description: self.description,
            config,
            metadata: self.metadata,
        }
    }
}

/// Google Cloud Storage ingestion source.
#[derive(Debug, Default, Clone)]
pub struct GcsSource {
    pub name: Option<String>,
    pub bucket: String,
    pub prefix: Option<String>,
    pub project_id: Option<String>,
    pub credentials_json: Option<String>,
    pub file_patterns: Vec<String>,
    pub max_file_size_mb: Option<u32>,
    pub sync_mode: Option<String>,
    pub description: Option<String>,
    pub metadata: Option<Metadata>,
    pub config_extra: HashMap<String, Value>,
}

impl IntoCreateSourceRequest for GcsSource {
    fn into_create_source_request(self) -> CreateSourceRequest {
        let mut config: HashMap<String, Value> = HashMap::new();
        config.insert("type".into(), Value::String(source_type::GCS.into()));
        config.insert("bucket".into(), Value::String(self.bucket.clone()));
        config.insert(
            "sync_mode".into(),
            Value::String(self.sync_mode.unwrap_or_else(|| "incremental".into())),
        );
        if let Some(v) = self.prefix {
            config.insert("prefix".into(), Value::String(v));
        }
        if let Some(v) = self.project_id {
            config.insert("project_id".into(), Value::String(v));
        }
        if let Some(v) = self.credentials_json {
            config.insert("credentials_json".into(), Value::String(v));
        }
        if !self.file_patterns.is_empty() {
            config.insert("file_patterns".into(), json!(self.file_patterns));
        }
        if let Some(v) = self.max_file_size_mb {
            config.insert("max_file_size_mb".into(), json!(v));
        }
        merge_extra(&mut config, self.config_extra);

        let name = self.name.unwrap_or_else(|| {
            if self.bucket.is_empty() {
                "rust-sdk-gcs-source".into()
            } else {
                format!("gcs-{}", sanitize(&self.bucket))
            }
        });

        CreateSourceRequest {
            source_type: source_type::GCS.into(),
            name,
            description: self.description,
            config,
            metadata: self.metadata,
        }
    }
}

/// Google Drive ingestion source. The public `source_type` is `gdrive`.
#[derive(Debug, Default, Clone)]
pub struct GoogleDriveSource {
    pub name: Option<String>,
    pub auth_mode: Option<String>,
    pub service_account_json: Option<String>,
    pub delegated_user: Option<String>,
    pub oauth_credentials: Option<HashMap<String, Value>>,
    pub drive_id: Option<String>,
    pub folder_ids: Vec<String>,
    pub query: Option<String>,
    pub mime_types: Vec<String>,
    pub include_shared_drives: Option<bool>,
    pub include_team_drives: Option<bool>,
    pub sync_mode: Option<String>,
    pub page_size: Option<u32>,
    pub resume_cursor: Option<String>,
    pub fetch_attachments: Option<bool>,
    pub sampling_enabled: Option<bool>,
    pub sampling_limit: Option<u32>,
    pub max_concurrent_downloads: Option<u32>,
    pub description: Option<String>,
    pub metadata: Option<Metadata>,
    pub config_extra: HashMap<String, Value>,
}

impl IntoCreateSourceRequest for GoogleDriveSource {
    fn into_create_source_request(self) -> CreateSourceRequest {
        let mut config: HashMap<String, Value> = HashMap::new();
        config.insert("type".into(), Value::String(source_type::GDRIVE.into()));
        config.insert(
            "sync_mode".into(),
            Value::String(self.sync_mode.unwrap_or_else(|| "incremental".into())),
        );
        if let Some(v) = self.auth_mode {
            config.insert("auth_mode".into(), Value::String(v));
        }
        if let Some(v) = self.service_account_json {
            config.insert("service_account_json".into(), Value::String(v));
        }
        if let Some(v) = self.delegated_user {
            config.insert("delegated_user".into(), Value::String(v));
        }
        if let Some(v) = self.oauth_credentials {
            config.insert("oauth_credentials".into(), json!(v));
        }
        if let Some(v) = &self.drive_id {
            config.insert("drive_id".into(), Value::String(v.clone()));
        }
        if !self.folder_ids.is_empty() {
            config.insert("folder_ids".into(), json!(self.folder_ids));
        }
        if let Some(v) = self.query {
            config.insert("query".into(), Value::String(v));
        }
        if !self.mime_types.is_empty() {
            config.insert("mime_types".into(), json!(self.mime_types));
        }
        if let Some(v) = self.include_shared_drives {
            config.insert("include_shared_drives".into(), json!(v));
        }
        if let Some(v) = self.include_team_drives {
            config.insert("include_team_drives".into(), json!(v));
        }
        if let Some(v) = self.page_size {
            config.insert("page_size".into(), json!(v));
        }
        if let Some(v) = self.resume_cursor {
            config.insert("resume_cursor".into(), Value::String(v));
        }
        if let Some(v) = self.fetch_attachments {
            config.insert("fetch_attachments".into(), json!(v));
        }
        if let Some(v) = self.sampling_enabled {
            config.insert("sampling_enabled".into(), json!(v));
        }
        if let Some(v) = self.sampling_limit {
            config.insert("sampling_limit".into(), json!(v));
        }
        if let Some(v) = self.max_concurrent_downloads {
            config.insert("max_concurrent_downloads".into(), json!(v));
        }
        merge_extra(&mut config, self.config_extra);

        let name = self.name.clone().unwrap_or_else(|| {
            if let Some(d) = &self.drive_id {
                format!("gdrive-{}", sanitize(d))
            } else if let Some(f) = self.folder_ids.first() {
                if !f.is_empty() {
                    format!("gdrive-{}", sanitize(f))
                } else {
                    "rust-sdk-gdrive-source".into()
                }
            } else {
                "rust-sdk-gdrive-source".into()
            }
        });

        CreateSourceRequest {
            source_type: source_type::GDRIVE.into(),
            name,
            description: self.description,
            config,
            metadata: self.metadata,
        }
    }
}

/// File upload source used by the presigned upload flow. Prefer
/// [`crate::Dataset::ingest_files`] for the full local-file upload pipeline.
#[derive(Debug, Default, Clone)]
pub struct FileUploadSource {
    pub name: Option<String>,
    pub storage_provider: Option<String>,
    pub key_prefix_template: Option<String>,
    pub allowed_content_types: Vec<String>,
    pub max_file_size_mb: Option<u32>,
    pub max_files_per_job: Option<u32>,
    pub sync_mode: Option<String>,
    pub description: Option<String>,
    pub metadata: Option<Metadata>,
    pub config_extra: HashMap<String, Value>,
}

impl IntoCreateSourceRequest for FileUploadSource {
    fn into_create_source_request(self) -> CreateSourceRequest {
        let mut config: HashMap<String, Value> = HashMap::new();
        config.insert(
            "type".into(),
            Value::String(source_type::FILE_UPLOAD.into()),
        );
        config.insert(
            "storage_provider".into(),
            Value::String(self.storage_provider.unwrap_or_else(|| "s3".into())),
        );
        config.insert(
            "sync_mode".into(),
            Value::String(self.sync_mode.unwrap_or_else(|| "full".into())),
        );
        if let Some(v) = self.key_prefix_template {
            config.insert("key_prefix_template".into(), Value::String(v));
        }
        if !self.allowed_content_types.is_empty() {
            config.insert(
                "allowed_content_types".into(),
                json!(self.allowed_content_types),
            );
        }
        if let Some(v) = self.max_file_size_mb {
            config.insert("max_file_size_mb".into(), json!(v));
        }
        if let Some(v) = self.max_files_per_job {
            config.insert("max_files_per_job".into(), json!(v));
        }
        merge_extra(&mut config, self.config_extra);

        let name = self.name.unwrap_or_else(|| "rust-sdk-file-upload".into());

        CreateSourceRequest {
            source_type: source_type::FILE_UPLOAD.into(),
            name,
            description: self.description,
            config,
            metadata: self.metadata,
        }
    }
}

/// Jira ingestion source.
#[derive(Debug, Default, Clone)]
pub struct JiraSource {
    pub name: Option<String>,
    pub cloud_id: String,
    pub access_token: Option<String>,
    pub project_keys: Vec<String>,
    pub jql: Option<String>,
    pub include_comments: Option<bool>,
    pub sync_mode: Option<String>,
    pub description: Option<String>,
    pub metadata: Option<Metadata>,
    pub config_extra: HashMap<String, Value>,
}

impl IntoCreateSourceRequest for JiraSource {
    fn into_create_source_request(self) -> CreateSourceRequest {
        let mut config: HashMap<String, Value> = HashMap::new();
        config.insert("type".into(), Value::String(source_type::JIRA.into()));
        config.insert("cloud_id".into(), Value::String(self.cloud_id.clone()));
        config.insert(
            "include_comments".into(),
            Value::Bool(self.include_comments.unwrap_or(true)),
        );
        config.insert(
            "sync_mode".into(),
            Value::String(self.sync_mode.unwrap_or_else(|| "incremental".into())),
        );
        if let Some(v) = self.access_token {
            config.insert("access_token".into(), Value::String(v));
        }
        if !self.project_keys.is_empty() {
            config.insert("project_keys".into(), json!(self.project_keys));
        }
        if let Some(v) = self.jql {
            config.insert("jql".into(), Value::String(v));
        }
        merge_extra(&mut config, self.config_extra);

        let hint = self
            .project_keys
            .first()
            .filter(|p| !p.is_empty())
            .cloned()
            .unwrap_or_else(|| self.cloud_id.clone());
        let name = self.name.unwrap_or_else(|| {
            if hint.is_empty() {
                "rust-sdk-jira-source".into()
            } else {
                format!("jira-{}", sanitize(&hint))
            }
        });

        CreateSourceRequest {
            source_type: source_type::JIRA.into(),
            name,
            description: self.description,
            config,
            metadata: self.metadata,
        }
    }
}

/// Confluence ingestion source (spaces and pages).
///
/// Provide either `cloud_id` (Atlassian OAuth site id) or `base_url`
/// (e.g. `https://company.atlassian.net`). `auth_mode` defaults to `basic`
/// (`username` + `api_token`); pass `oauth_credentials` for OAuth.
#[derive(Debug, Default, Clone)]
pub struct ConfluenceSource {
    pub name: Option<String>,
    pub cloud_id: Option<String>,
    pub base_url: Option<String>,
    pub auth_mode: Option<String>,
    pub username: Option<String>,
    pub api_token: Option<String>,
    pub oauth_credentials: Option<HashMap<String, Value>>,
    pub spaces: Vec<String>,
    pub include_attachments: Option<bool>,
    pub sync_mode: Option<String>,
    pub description: Option<String>,
    pub metadata: Option<Metadata>,
    pub config_extra: HashMap<String, Value>,
}

impl IntoCreateSourceRequest for ConfluenceSource {
    fn into_create_source_request(self) -> CreateSourceRequest {
        let mut config: HashMap<String, Value> = HashMap::new();
        config.insert("type".into(), Value::String(source_type::CONFLUENCE.into()));
        config.insert(
            "auth_mode".into(),
            Value::String(self.auth_mode.unwrap_or_else(|| "basic".into())),
        );
        config.insert(
            "include_attachments".into(),
            Value::Bool(self.include_attachments.unwrap_or(false)),
        );
        config.insert(
            "sync_mode".into(),
            Value::String(self.sync_mode.unwrap_or_else(|| "incremental".into())),
        );
        if let Some(v) = &self.cloud_id {
            config.insert("cloud_id".into(), Value::String(v.clone()));
        }
        if let Some(v) = &self.base_url {
            config.insert("base_url".into(), Value::String(v.clone()));
        }
        if let Some(v) = self.username {
            config.insert("username".into(), Value::String(v));
        }
        if let Some(v) = self.api_token {
            config.insert("api_token".into(), Value::String(v));
        }
        if let Some(v) = self.oauth_credentials {
            config.insert("oauth_credentials".into(), json!(v));
        }
        if !self.spaces.is_empty() {
            config.insert("spaces".into(), json!(self.spaces));
        }
        merge_extra(&mut config, self.config_extra);

        let hint = self
            .spaces
            .first()
            .filter(|s| !s.is_empty())
            .cloned()
            .or_else(|| confluence_host(self.base_url.as_deref()))
            .or_else(|| self.cloud_id.clone());
        let name = self.name.unwrap_or_else(|| match hint {
            Some(h) if !h.is_empty() => format!("confluence-{}", sanitize(&h)),
            _ => "rust-sdk-confluence-source".into(),
        });

        CreateSourceRequest {
            source_type: source_type::CONFLUENCE.into(),
            name,
            description: self.description,
            config,
            metadata: self.metadata,
        }
    }
}

fn confluence_host(base_url: Option<&str>) -> Option<String> {
    let url = base_url?;
    if let Ok(parsed) = url::Url::parse(url) {
        if let Some(host) = parsed.host_str() {
            return Some(host.to_owned());
        }
    }
    None
}

fn merge_extra(config: &mut HashMap<String, Value>, extra: HashMap<String, Value>) {
    for (k, v) in extra {
        config.insert(k, v);
    }
}

fn web_default_name(start_urls: &[String]) -> String {
    let first = match start_urls.iter().find(|s| !s.is_empty()) {
        Some(s) => s,
        None => return "rust-sdk-web-source".into(),
    };
    if let Ok(parsed) = url::Url::parse(first) {
        if let Some(host) = parsed.host_str() {
            return format!("web-{}", sanitize(host));
        }
    }
    format!("web-{}", sanitize(first))
}

/// Lowercase, dash-separated sanitization used to derive default source names.
pub(crate) fn sanitize(value: &str) -> String {
    let lower = value.trim().to_lowercase();
    let mut out = String::with_capacity(lower.len());
    let mut last_dash = false;
    for c in lower.chars() {
        let keep = c.is_ascii_lowercase() || c.is_ascii_digit();
        if keep {
            out.push(c);
            last_dash = false;
        } else if !last_dash {
            out.push('-');
            last_dash = true;
        }
    }
    let trimmed = out.trim_matches('-').to_owned();
    if trimmed.is_empty() {
        "source".into()
    } else {
        trimmed
    }
}