liboxen 0.65.0

Oxen is a fast data version control system, built with machine learning training data in mind. Designed to handle terabytes of data with ease, using a workflow similar to git. Version both structured and unstructured data of any modality: text, images, video, audio, CSV, Parquet, JSONL, model checkpoints, and more. liboxen is the embeddable core library behind the oxen CLI and server, which power fine tuning and inference pipelines for multimodal LLMs, image models, and video models on Oxen.ai.
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
use crate::api;
use crate::api::client;
use crate::error::OxenError;
use crate::opts::DFOpts;
use crate::util;
use crate::util::fs::AtomicFile;
use crate::view::entries::PaginatedMetadataEntriesResponse;
use crate::view::json_data_frame_view::WorkspaceJsonDataFrameViewResponse;
use futures_util::StreamExt;
use std::path::Path;
use tokio_util::io::StreamReader;

use crate::model::RemoteRepository;
use crate::view::StatusMessage;

pub mod columns;
pub mod embeddings;
pub mod rows;

pub async fn get(
    remote_repo: &RemoteRepository,
    workspace_id: impl AsRef<str>,
    path: impl AsRef<Path>,
    opts: &DFOpts,
) -> Result<WorkspaceJsonDataFrameViewResponse, OxenError> {
    let workspace_id = workspace_id.as_ref();
    let path = path.as_ref();
    let file_path_str = path.to_string_lossy();
    let query_str = opts.to_http_query_params();
    let uri =
        format!("/workspaces/{workspace_id}/data_frames/resource/{file_path_str}?{query_str}");
    let url = api::endpoint::url_from_repo(remote_repo, &uri)?;

    let client = client::new_for_url(&url)?;
    let res = client.get(&url).send().await?;
    let body = client::parse_json_body(&url, res).await?;
    let response: Result<WorkspaceJsonDataFrameViewResponse, serde_json::Error> =
        serde_json::from_str(&body);
    match response {
        Ok(response) => Ok(response),
        Err(err) => {
            let err = format!(
                "workspaces::data_frames::get error parsing from {url}\n\nErr {err:?} \n\n{body}"
            );
            Err(OxenError::basic_str(err))
        }
    }
}

pub async fn download(
    remote_repo: &RemoteRepository,
    workspace_id: impl AsRef<str>,
    path: impl AsRef<Path>,
    opts: &DFOpts, // opts holds output path
) -> Result<(), OxenError> {
    let workspace_id = workspace_id.as_ref();
    let path = path.as_ref();
    let file_path_str = path.to_string_lossy();
    let query_str = opts.to_http_query_params();
    let uri =
        format!("/workspaces/{workspace_id}/data_frames/download/{file_path_str}?{query_str}");
    let url = api::endpoint::url_from_repo(remote_repo, &uri)?;

    // Download the file and save it to the output path
    let Some(output_path_str) = &opts.output else {
        return Err(OxenError::basic_str("output path is required"));
    };
    let output_path = Path::new(&output_path_str);
    let client = client::new_for_url(&url)?;
    let res = client.get(&url).send().await?;

    if !res.status().is_success() {
        let status = res.status();

        if status == reqwest::StatusCode::NOT_FOUND {
            return Err(OxenError::resource_not_found(file_path_str));
        }

        log::error!("api::client::workspaces::data_frames::download failed with status: {status}");
        let body = client::parse_json_body(&url, res).await?;
        return Err(OxenError::basic_str(format!(
            "Error: Could not download data frame {body:?}"
        )));
    }

    // Stream the response body to the output path
    log::debug!("Download writing output file {output_path:?}");
    let mut reader = StreamReader::new(
        res.bytes_stream()
            .map(|chunk| chunk.map_err(std::io::Error::other)),
    );
    AtomicFile::new(output_path).stream_async(&mut reader).await
}

pub async fn is_indexed(
    remote_repo: &RemoteRepository,
    workspace_id: &str,
    path: &Path,
) -> Result<bool, OxenError> {
    let res = get(remote_repo, workspace_id, path, &DFOpts::empty()).await?;
    Ok(res.is_indexed)
}

pub async fn list(
    remote_repo: &RemoteRepository,
    branch_name: &str,
    workspace_id: &str,
) -> Result<PaginatedMetadataEntriesResponse, OxenError> {
    let uri = format!("/workspaces/{workspace_id}/data_frames/branch/{branch_name}");
    let url = api::endpoint::url_from_repo(remote_repo, &uri)?;

    let client = client::new_for_url(&url)?;
    let res = client.get(&url).send().await?;
    let body = client::parse_json_body(&url, res).await?;
    let response: Result<PaginatedMetadataEntriesResponse, serde_json::Error> =
        serde_json::from_str(&body);
    match response {
        Ok(response) => Ok(response),
        Err(err) => {
            let err = format!(
                "api::workspaces::get_by_branch error parsing from {url}\n\nErr {err:?} \n\n{body}"
            );
            Err(OxenError::basic_str(err))
        }
    }
}

pub async fn index(
    remote_repo: &RemoteRepository,
    workspace_id: &str,
    path: impl AsRef<Path>,
) -> Result<StatusMessage, OxenError> {
    let path = util::fs::linux_path(path.as_ref());
    put(
        remote_repo,
        workspace_id,
        path,
        &serde_json::json!({"is_indexed": true}),
    )
    .await
}

pub async fn unindex(
    remote_repo: &RemoteRepository,
    workspace_id: &str,
    path: &Path,
) -> Result<StatusMessage, OxenError> {
    put(
        remote_repo,
        workspace_id,
        path,
        &serde_json::json!({"is_indexed": false}),
    )
    .await
}

pub async fn put(
    remote_repo: &RemoteRepository,
    workspace_id: impl AsRef<str>,
    path: impl AsRef<Path>,
    data: &serde_json::Value,
) -> Result<StatusMessage, OxenError> {
    let workspace_id = workspace_id.as_ref();
    let path = path.as_ref();
    let file_path_str = path.to_string_lossy();

    let uri = format!("/workspaces/{workspace_id}/data_frames/resource/{file_path_str}");
    let url = api::endpoint::url_from_repo(remote_repo, &uri)?;
    let params = serde_json::to_string(data)?;

    let client = client::new_for_url(&url)?;
    let res = client.put(&url).body(params).send().await?;
    let body = client::parse_json_body(&url, res).await?;
    let response: Result<StatusMessage, serde_json::Error> = serde_json::from_str(&body);
    match response {
        Ok(response) => Ok(response),
        Err(err) => {
            let err =
                format!("api::workspaces::put error parsing from {url}\n\nErr {err:?} \n\n{body}");
            Err(OxenError::basic_str(err))
        }
    }
}

pub async fn restore(
    remote_repo: &RemoteRepository,
    workspace_id: &str,
    path: impl AsRef<Path>,
) -> Result<(), OxenError> {
    let file_name = path.as_ref().to_string_lossy();
    let uri = format!("/workspaces/{workspace_id}/data_frames/resource/{file_name}");
    let url = api::endpoint::url_from_repo(remote_repo, &uri)?;
    log::debug!("workspaces::data_frames::restore {url}");
    let client = client::new_for_url(&url)?;
    let res = client.delete(&url).send().await?;
    let body = client::parse_json_body(&url, res).await?;
    log::debug!("workspaces::data_frames::restore got body: {body}");
    Ok(())
}

pub async fn rename_data_frame(
    remote_repo: &RemoteRepository,
    workspace_id: impl AsRef<str>,
    path: impl AsRef<Path>,
    new_path: impl AsRef<Path>,
) -> Result<StatusMessage, OxenError> {
    let workspace_id = workspace_id.as_ref();
    let path = path.as_ref();
    let file_path_str = path.to_string_lossy();

    let uri = format!("/workspaces/{workspace_id}/data_frames/rename/{file_path_str}");
    let url = api::endpoint::url_from_repo(remote_repo, &uri)?;
    let params = serde_json::to_string(&serde_json::json!({
        "new_path": new_path.as_ref().to_string_lossy()
    }))?;

    let client = client::new_for_url(&url)?;
    let res = client.put(&url).body(params).send().await?;
    let body = client::parse_json_body(&url, res).await?;
    let response: Result<StatusMessage, serde_json::Error> = serde_json::from_str(&body);
    match response {
        Ok(response) => Ok(response),
        Err(err) => {
            let err =
                format!("api::workspaces::put error parsing from {url}\n\nErr {err:?} \n\n{body}");
            Err(OxenError::basic_str(err))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::rows;
    use crate::api::client::branches::create_from_branch;
    use crate::api::client::{data_frames, workspaces};
    use crate::util::fs;

    use std::path::{Path, PathBuf};

    use crate::constants::DEFAULT_BRANCH_NAME;
    use crate::core::df::tabular;
    use crate::error::OxenError;
    use crate::model::{NewCommitBody, RemoteRepository};
    use crate::opts::DFOpts;
    use crate::repositories;
    use crate::test;

    use super::{download, get, index, list, rename_data_frame, restore, unindex};

    fn commit_body(message: &str) -> NewCommitBody {
        NewCommitBody {
            message: message.to_string(),
            author: "Test User".to_string(),
            email: "test@oxen.ai".to_string(),
        }
    }

    fn bbox_row(file: &str) -> String {
        format!(
            r#"{{"file": "{file}", "label": "dog", "min_x": 13, "min_y": 14, "width": 100, "height": 100}}"#
        )
    }

    async fn staged_height(
        remote_repo: &RemoteRepository,
        workspace_id: &str,
        path: &Path,
    ) -> Result<usize, OxenError> {
        let res = get(remote_repo, workspace_id, path, &DFOpts::empty()).await?;
        Ok(res
            .data_frame
            .expect("data frame should be present")
            .source
            .size
            .height)
    }

    async fn committed_height(
        remote_repo: &RemoteRepository,
        branch_name: &str,
        path: &Path,
    ) -> Result<usize, OxenError> {
        let df = data_frames::get(remote_repo, branch_name, path, DFOpts::empty()).await?;
        assert_eq!(df.status.status_message, "resource_found");
        Ok(df.data_frame.view.to_df().await.height())
    }

    #[cfg_attr(windows, ignore = "oxen-server is not supported on Windows")]
    #[tokio::test]
    async fn test_query_edit_and_commit_workspace_data_frames() -> Result<(), OxenError> {
        test::run_remote_repo_test_bounding_box_csv_pushed(|local_repo, remote_repo| async move {
            let path = Path::new("annotations")
                .join("train")
                .join("bounding_box.csv");
            let local_path = local_repo.path.join(&path);
            let remote_repo_copy = remote_repo.clone();

            // Query and download an indexed data frame
            workspaces::create(&remote_repo, DEFAULT_BRANCH_NAME, "query").await?;
            assert_eq!(index(&remote_repo, "query", &path).await?.status, "success");
            let res = get(&remote_repo, "query", &path, &DFOpts::empty()).await?;
            assert_eq!(res.status.status_message, "resource_found");
            assert_eq!(
                list(&remote_repo, DEFAULT_BRANCH_NAME, "query")
                    .await?
                    .entries
                    .entries
                    .len(),
                1
            );

            let dogs = "SELECT * FROM df WHERE label = 'dog'";
            let opts = DFOpts {
                sql: Some(dogs.to_string()),
                ..DFOpts::empty()
            };
            let res = get(&remote_repo, "query", &path, &opts).await?;
            assert_eq!(
                res.data_frame.expect("a page").view.to_df().await.height(),
                4
            );

            let query_repo = remote_repo.clone();
            let query_path = path.clone();
            test::run_empty_dir_test_async(|sync_dir| async move {
                let (remote_repo, path) = (query_repo, query_path);
                let output = sync_dir.join("download.csv");
                let opts = DFOpts {
                    output: Some(output.clone()),
                    ..DFOpts::empty()
                };
                download(&remote_repo, "query", &path, &opts).await?;
                assert_eq!(
                    fs::read_from_path(&output)?,
                    fs::read_from_path(&local_path)?
                );

                let output = sync_dir.join("download.jsonl");
                let opts = DFOpts {
                    output: Some(output.clone()),
                    ..DFOpts::empty()
                };
                download(&remote_repo, "query", &path, &opts).await?;
                let original = tabular::read_df(&local_path, DFOpts::empty()).await?;
                let downloaded = tabular::read_df(&output, DFOpts::empty()).await?;
                assert_eq!(
                    (downloaded.height(), downloaded.width()),
                    (original.height(), original.width())
                );

                for (sql, shape) in [
                    (dogs, (4, 6)),
                    ("SELECT label, COUNT(*) FROM df GROUP BY label", (2, 2)),
                ] {
                    let output = sync_dir.join("query.csv");
                    let opts = DFOpts {
                        sql: Some(sql.to_string()),
                        output: Some(output.clone()),
                        ..DFOpts::empty()
                    };
                    download(&remote_repo, "query", &path, &opts).await?;
                    let df = tabular::read_df(&output, DFOpts::empty()).await?;
                    assert_eq!((df.height(), df.width()), shape, "{sql}");
                }

                let output = sync_dir.join("should_not_exist.csv");
                let opts = DFOpts {
                    output: Some(output.clone()),
                    ..DFOpts::empty()
                };
                let result = download(
                    &remote_repo,
                    "query",
                    Path::new("this/path/does_not_exist.csv"),
                    &opts,
                )
                .await;
                assert!(
                    matches!(result, Err(OxenError::ResourceNotFound(_))),
                    "{result:?}"
                );
                assert!(!output.exists());
                Ok(())
            })
            .await?;
            assert_eq!(
                unindex(&remote_repo, "query", &path).await?.status,
                "success"
            );

            // Stage, delete, and discard row edits: the csv holds 6 committed rows
            workspaces::create(&remote_repo, DEFAULT_BRANCH_NAME, "edit").await?;
            index(&remote_repo, "edit", &path).await?;
            rows::add(&remote_repo, "edit", &path, bbox_row("image1.jpg")).await?;
            let (_df, added_id) =
                rows::add(&remote_repo, "edit", &path, bbox_row("image2.jpg")).await?;
            assert_eq!(staged_height(&remote_repo, "edit", &path).await?, 8);
            let added_id = added_id.expect("the added row's id");
            rows::delete(&remote_repo, "edit", &path, &added_id).await?;
            assert_eq!(staged_height(&remote_repo, "edit", &path).await?, 7);
            restore(&remote_repo, "edit", &path).await?;
            assert_eq!(
                staged_height(&remote_repo, "edit", &path).await?,
                6,
                "restore discards the staged edits"
            );

            // Rename a data frame, with and without a staged row, each on its own branch
            let og_height = committed_height(&remote_repo, DEFAULT_BRANCH_NAME, &path).await?;
            for (branch_name, new_path, add_row) in [
                (
                    "rename",
                    PathBuf::from("new/dir/bounding_box_renamed.csv"),
                    false,
                ),
                (
                    "edit-rename",
                    Path::new("annotations")
                        .join("train")
                        .join("bounding_box_edited.csv"),
                    true,
                ),
                (
                    "edit-rename-new-dir",
                    Path::new("annotations")
                        .join("train")
                        .join("nonexistent_folder")
                        .join("bounding_box_edited.csv"),
                    true,
                ),
            ] {
                create_from_branch(&remote_repo, branch_name, DEFAULT_BRANCH_NAME).await?;
                workspaces::create(&remote_repo, branch_name, branch_name).await?;
                index(&remote_repo, branch_name, &path).await?;
                if add_row {
                    rows::add(
                        &remote_repo,
                        branch_name,
                        &path,
                        bbox_row("train/dog_4.jpg"),
                    )
                    .await?;
                }
                let res = rename_data_frame(&remote_repo, branch_name, &path, &new_path).await?;
                assert_eq!(res.status, "success");
                workspaces::commit(
                    &remote_repo,
                    branch_name,
                    branch_name,
                    &commit_body(branch_name),
                )
                .await?;

                let expected = og_height + usize::from(add_row);
                assert_eq!(
                    committed_height(&remote_repo, branch_name, &new_path).await?,
                    expected,
                    "{branch_name}"
                );
                assert_eq!(
                    committed_height(&remote_repo, branch_name, &path).await?,
                    og_height,
                    "{branch_name}"
                );
            }

            // Index a parquet file with a binary column
            let binary_name = "binary_col.parquet";
            fs::copy(
                test::test_binary_column_parquet_file(),
                local_repo.path.join(binary_name),
            )?;
            // A data frame at the repo root, for the pull below
            let prompts_path = Path::new("prompts.jsonl");
            fs::write_to_path(
                local_repo.path.join(prompts_path),
                "{\"prompt\": \"What is the meaning of life?\", \"label\": \"42\"}\n",
            )?;
            // A data frame stored as a JSON array rather than JSON lines
            fs::write_to_path(
                local_repo.path.join("labels.json"),
                r#"[{"id": 1, "label": "cat"}, {"id": 2, "label": "dog"}]"#,
            )?;
            repositories::add(&local_repo, &local_repo.path).await?;
            repositories::commit(&local_repo, "Add a binary column parquet file and prompts")?;
            repositories::push(&local_repo).await?;
            workspaces::create(&remote_repo, DEFAULT_BRANCH_NAME, "binary").await?;
            assert_eq!(
                index(&remote_repo, "binary", binary_name).await?.status,
                "success"
            );

            // A clone fast-forwards on each pull of a data frame committed from a workspace, nested
            // or at the root
            test::run_empty_dir_test_async(|empty_dir| async move {
                let cloned_repo =
                    repositories::clone_url(&remote_repo.remote.url, &empty_dir.join("repo_b"))
                        .await?;
                for (workspace_id, df_path, row) in [
                    ("workspace_a", path.as_path(), bbox_row("d-o-double-g.jpg")),
                    (
                        "workspace_b",
                        prompts_path,
                        r#"{"prompt": "What is another meaning of life?", "label": "43"}"#
                            .to_string(),
                    ),
                ] {
                    let og =
                        tabular::read_df(cloned_repo.path.join(df_path), DFOpts::empty()).await?;
                    workspaces::create(&remote_repo, DEFAULT_BRANCH_NAME, workspace_id).await?;
                    index(&remote_repo, workspace_id, df_path).await?;
                    rows::add(&remote_repo, workspace_id, df_path, row).await?;
                    workspaces::commit(
                        &remote_repo,
                        DEFAULT_BRANCH_NAME,
                        workspace_id,
                        &commit_body(workspace_id),
                    )
                    .await?;

                    repositories::pull(&cloned_repo).await?;
                    let df =
                        tabular::read_df(cloned_repo.path.join(df_path), DFOpts::empty()).await?;
                    assert_eq!(df.height(), og.height() + 1, "{df_path:?}");
                }
                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }
}