Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 75 additions & 26 deletions crates/ov_cli/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ impl HttpClient {
extra_headers: Option<std::collections::HashMap<String, String>>,
) -> Self {
Self {
base: BaseClient::new(base_url, api_key, agent_id, account, user, timeout_secs, extra_headers),
base: BaseClient::new(
base_url,
api_key,
agent_id,
account,
user,
timeout_secs,
extra_headers,
),
}
}

Expand Down Expand Up @@ -108,16 +116,27 @@ impl HttpClient {
self.create_uploader().zip_directory(dir_path)
}

fn zip_directory_with_progress(&self, dir_path: &Path, verbose: bool) -> Result<tempfile::NamedTempFile> {
self.create_uploader().zip_directory_with_progress(dir_path, verbose)
fn zip_directory_with_progress(
&self,
dir_path: &Path,
verbose: bool,
) -> Result<tempfile::NamedTempFile> {
self.create_uploader()
.zip_directory_with_progress(dir_path, verbose)
}

async fn upload_temp_file(&self, file_path: &Path) -> Result<String> {
self.create_uploader().upload_temp_file(file_path).await
}

async fn upload_temp_file_with_progress(&self, file_path: &Path, verbose: bool) -> Result<String> {
self.create_uploader().upload_temp_file_with_progress(file_path, verbose).await
async fn upload_temp_file_with_progress(
&self,
file_path: &Path,
verbose: bool,
) -> Result<String> {
self.create_uploader()
.upload_temp_file_with_progress(file_path, verbose)
.await
}

// ============ Content Methods ============
Expand Down Expand Up @@ -165,12 +184,7 @@ impl HttpClient {
})
}

pub async fn reindex(
&self,
uri: &str,
mode: &str,
wait: bool,
) -> Result<serde_json::Value> {
pub async fn reindex(&self, uri: &str, mode: &str, wait: bool) -> Result<serde_json::Value> {
let body = serde_json::json!({
"uri": uri,
"mode": mode,
Expand All @@ -179,6 +193,13 @@ impl HttpClient {
self.post("/api/v1/content/reindex", &body).await
}

pub async fn consistency(&self, uri: &str) -> Result<serde_json::Value> {
let body = serde_json::json!({
"uri": uri,
});
self.post("/api/v1/system/consistency", &body).await
}

/// Download file as raw bytes
pub async fn get_bytes(&self, uri: &str) -> Result<Vec<u8>> {
let url = format!("{}/api/v1/content/download", self.base.base_url);
Expand Down Expand Up @@ -443,7 +464,8 @@ impl HttpClient {
self.zip_directory(path_obj)?
};
let temp_file_id = if show_progress {
self.upload_temp_file_with_progress(zip_file.path(), verbose).await?
self.upload_temp_file_with_progress(zip_file.path(), verbose)
.await?
} else {
self.upload_temp_file(zip_file.path()).await?
};
Expand All @@ -465,15 +487,19 @@ impl HttpClient {
"watch_interval": watch_interval,
}));

let dynamic_timeout = TimeoutConfig::for_resource_processing().calculate(zip_file.path())?;
self.base.post_with_timeout("/api/v1/resources", &body, dynamic_timeout).await
let dynamic_timeout =
TimeoutConfig::for_resource_processing().calculate(zip_file.path())?;
self.base
.post_with_timeout("/api/v1/resources", &body, dynamic_timeout)
.await
} else if path_obj.is_file() {
let source_name = path_obj
.file_name()
.and_then(|n| n.to_str())
.map(|s| s.to_string());
let temp_file_id = if show_progress {
self.upload_temp_file_with_progress(path_obj, verbose).await?
self.upload_temp_file_with_progress(path_obj, verbose)
.await?
} else {
self.upload_temp_file(path_obj).await?
};
Expand All @@ -495,8 +521,11 @@ impl HttpClient {
"watch_interval": watch_interval,
}));

let dynamic_timeout = TimeoutConfig::for_resource_processing().calculate(path_obj)?;
self.base.post_with_timeout("/api/v1/resources", &body, dynamic_timeout).await
let dynamic_timeout =
TimeoutConfig::for_resource_processing().calculate(path_obj)?;
self.base
.post_with_timeout("/api/v1/resources", &body, dynamic_timeout)
.await
} else {
let body = build_body(serde_json::json!({
"path": path,
Expand Down Expand Up @@ -555,8 +584,11 @@ impl HttpClient {
"wait": wait,
"timeout": timeout,
});
let dynamic_timeout = TimeoutConfig::for_resource_processing().calculate(zip_file.path())?;
self.base.post_with_timeout("/api/v1/skills", &body, dynamic_timeout).await
let dynamic_timeout =
TimeoutConfig::for_resource_processing().calculate(zip_file.path())?;
self.base
.post_with_timeout("/api/v1/skills", &body, dynamic_timeout)
.await
} else if path_obj.is_file() {
let temp_file_id = self.upload_temp_file(path_obj).await?;

Expand All @@ -565,8 +597,11 @@ impl HttpClient {
"wait": wait,
"timeout": timeout,
});
let dynamic_timeout = TimeoutConfig::for_resource_processing().calculate(path_obj)?;
self.base.post_with_timeout("/api/v1/skills", &body, dynamic_timeout).await
let dynamic_timeout =
TimeoutConfig::for_resource_processing().calculate(path_obj)?;
self.base
.post_with_timeout("/api/v1/skills", &body, dynamic_timeout)
.await
} else {
let body = serde_json::json!({
"data": data,
Expand Down Expand Up @@ -707,9 +742,15 @@ impl HttpClient {
Ok(final_path.to_string_lossy().to_string())
}

pub async fn export_ovpack(&self, uri: &str, to: &str) -> Result<String> {
pub async fn export_ovpack(
&self,
uri: &str,
to: &str,
include_vectors: bool,
) -> Result<String> {
let body = serde_json::json!({
"uri": uri,
"include_vectors": include_vectors,
});
let base_name = uri
.trim_end_matches('/')
Expand All @@ -720,10 +761,10 @@ impl HttpClient {
.await
}

pub async fn backup_ovpack(&self, to: &str) -> Result<String> {
pub async fn backup_ovpack(&self, to: &str, include_vectors: bool) -> Result<String> {
self.download_pack(
"/api/v1/pack/backup",
serde_json::json!({}),
serde_json::json!({"include_vectors": include_vectors}),
to,
"openviking-backup",
)
Expand All @@ -735,6 +776,7 @@ impl HttpClient {
file_path: &str,
parent: &str,
on_conflict: Option<&str>,
vector_mode: Option<&str>,
) -> Result<serde_json::Value> {
let file_path_obj = Path::new(file_path);

Expand All @@ -754,6 +796,7 @@ impl HttpClient {
"temp_file_id": temp_file_id,
"parent": parent,
"on_conflict": conflict_policy,
"vector_mode": vector_mode.unwrap_or("auto"),
});
self.post("/api/v1/pack/import", &body).await
}
Expand All @@ -762,6 +805,7 @@ impl HttpClient {
&self,
file_path: &str,
on_conflict: Option<&str>,
vector_mode: Option<&str>,
) -> Result<serde_json::Value> {
let file_path_obj = Path::new(file_path);

Expand All @@ -780,6 +824,7 @@ impl HttpClient {
let body = serde_json::json!({
"temp_file_id": temp_file_id,
"on_conflict": conflict_policy,
"vector_mode": vector_mode.unwrap_or("auto"),
});
self.post("/api/v1/pack/restore", &body).await
}
Expand Down Expand Up @@ -1035,11 +1080,15 @@ mod tests {
let headers = client.build_headers();

assert_eq!(
headers.get("X-API-Key").and_then(|value| value.to_str().ok()),
headers
.get("X-API-Key")
.and_then(|value| value.to_str().ok()),
Some("test-key")
);
assert_eq!(
headers.get("X-Custom-Header").and_then(|value| value.to_str().ok()),
headers
.get("X-Custom-Header")
.and_then(|value| value.to_str().ok()),
Some("custom-value")
);
}
Expand Down
14 changes: 10 additions & 4 deletions crates/ov_cli/src/commands/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ pub async fn export(
client: &HttpClient,
uri: &str,
to: &str,
include_vectors: bool,
format: OutputFormat,
compact: bool,
) -> Result<()> {
let file_path = client.export_ovpack(uri, to).await?;
let file_path = client.export_ovpack(uri, to, include_vectors).await?;

// Output success message with the file path
let result = serde_json::json!({
Expand All @@ -24,10 +25,11 @@ pub async fn export(
pub async fn backup(
client: &HttpClient,
to: &str,
include_vectors: bool,
format: OutputFormat,
compact: bool,
) -> Result<()> {
let file_path = client.backup_ovpack(to).await?;
let file_path = client.backup_ovpack(to, include_vectors).await?;

let result = serde_json::json!({
"file": file_path,
Expand All @@ -43,11 +45,12 @@ pub async fn import(
file_path: &str,
target: &str,
on_conflict: Option<&str>,
vector_mode: Option<&str>,
format: OutputFormat,
compact: bool,
) -> Result<()> {
let result = client
.import_ovpack(file_path, target, on_conflict)
.import_ovpack(file_path, target, on_conflict, vector_mode)
.await?;
output_success(&result, format, compact);
Ok(())
Expand All @@ -57,10 +60,13 @@ pub async fn restore(
client: &HttpClient,
file_path: &str,
on_conflict: Option<&str>,
vector_mode: Option<&str>,
format: OutputFormat,
compact: bool,
) -> Result<()> {
let result = client.restore_ovpack(file_path, on_conflict).await?;
let result = client
.restore_ovpack(file_path, on_conflict, vector_mode)
.await?;
output_success(&result, format, compact);
Ok(())
}
42 changes: 42 additions & 0 deletions crates/ov_cli/src/commands/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,48 @@ pub async fn status(client: &HttpClient, output_format: OutputFormat, compact: b
Ok(())
}

pub async fn consistency(
client: &HttpClient,
uri: &str,
output_format: OutputFormat,
compact: bool,
) -> Result<()> {
let response: serde_json::Value = client.consistency(uri).await?;
if matches!(output_format, OutputFormat::Table) {
output_consistency_table(&response, compact);
} else {
output_success(&response, output_format, compact);
}
Ok(())
}

fn output_consistency_table(response: &serde_json::Value, compact: bool) {
let summary = json!({
"ok": response.get("ok").and_then(|v| v.as_bool()).unwrap_or(false),
"expected_count": response.get("expected_count").and_then(|v| v.as_u64()).unwrap_or(0),
"missing_record_count": response
.get("missing_record_count")
.and_then(|v| v.as_u64())
.unwrap_or(0),
"missing_records_truncated": response
.get("missing_records_truncated")
.and_then(|v| v.as_bool())
.unwrap_or(false),
});
output_success(&summary, OutputFormat::Table, compact);

let Some(missing_records) = response.get("missing_records").and_then(|v| v.as_array()) else {
return;
};
if missing_records.is_empty() {
return;
}

println!();
println!("missing_records");
output_success(missing_records, OutputFormat::Table, compact);
}

pub async fn health(
client: &HttpClient,
output_format: OutputFormat,
Expand Down
Loading
Loading