Skip to content
Draft
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
5 changes: 2 additions & 3 deletions crates/ov_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ Create `~/.openviking/ovcli.conf`:
"url": "http://localhost:1933",
"api_key": "your-api-key",
"account": "acme",
"user": "alice",
"agent_id": "assistant-1"
"user": "alice"
}
```

Expand Down Expand Up @@ -132,7 +131,7 @@ ov find "API authentication" --threshold 0.7 --limit 5
ov ls viking://resources --recursive

# Temporarily override identity from CLI flags
ov --account acme --user alice --agent-id assistant-2 ls viking://
ov --account acme --user alice ls viking://

# Glob search
ov glob "**/*.md" --uri viking://resources
Expand Down
80 changes: 30 additions & 50 deletions crates/ov_cli/src/base_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ pub struct BaseClient {
pub(crate) api_key: Option<String>,
pub(crate) account: Option<String>,
pub(crate) user: Option<String>,
pub(crate) agent_id: Option<String>,
pub(crate) profile_enabled: bool,
pub(crate) extra_headers: Option<std::collections::HashMap<String, String>>,
}
Expand All @@ -164,7 +163,6 @@ impl BaseClient {
api_key: None,
account: None,
user: None,
agent_id: None,
profile_enabled: false,
extra_headers: None,
}
Expand All @@ -173,7 +171,6 @@ impl BaseClient {
pub fn new(
base_url: impl Into<String>,
api_key: Option<String>,
agent_id: Option<String>,
account: Option<String>,
user: Option<String>,
timeout_secs: f64,
Expand All @@ -191,7 +188,6 @@ impl BaseClient {
api_key,
account,
user,
agent_id,
profile_enabled,
extra_headers,
}
Expand All @@ -213,10 +209,6 @@ impl BaseClient {
self.user.as_deref()
}

pub fn agent_id(&self) -> Option<&str> {
self.agent_id.as_deref()
}

pub fn api_key(&self) -> Option<&str> {
self.api_key.as_deref()
}
Expand All @@ -232,11 +224,6 @@ impl BaseClient {
headers.insert("X-API-Key", value);
}
}
if let Some(agent_id) = &self.agent_id {
if let Ok(value) = reqwest::header::HeaderValue::from_str(agent_id) {
headers.insert("X-OpenViking-Agent", value);
}
}
if let Some(account) = &self.account {
if let Ok(value) = reqwest::header::HeaderValue::from_str(account) {
headers.insert("X-OpenViking-Account", value);
Expand Down Expand Up @@ -315,7 +302,10 @@ impl BaseClient {
})
}

pub(crate) fn create_client_with_timeout(&self, timeout: std::time::Duration) -> Result<ReqwestClient> {
pub(crate) fn create_client_with_timeout(
&self,
timeout: std::time::Duration,
) -> Result<ReqwestClient> {
ReqwestClient::builder()
.timeout(timeout)
.build()
Expand Down Expand Up @@ -386,10 +376,7 @@ impl BaseClient {
let url = format!("{}{}", self.base_url, path);
let client = self.create_client_with_timeout(timeout)?;

let request = client
.post(&url)
.headers(self.build_headers())
.json(body);
let request = client.post(&url).headers(self.build_headers()).json(body);
let request = if self.profile_enabled {
request.query(&[("profile", "1")])
} else {
Expand All @@ -409,11 +396,7 @@ impl BaseClient {
body: &B,
) -> Result<T> {
let url = format!("{}{}", self.base_url, path);
let request = self
.http
.put(&url)
.headers(self.build_headers())
.json(body);
let request = self.http.put(&url).headers(self.build_headers()).json(body);
let request = if self.profile_enabled {
request.query(&[("profile", "1")])
} else {
Expand Down Expand Up @@ -587,18 +570,10 @@ mod tests {

#[test]
fn append_profile_query_adds_flag_when_enabled() {
let client = BaseClient::new(
"http://localhost:1933",
None,
None,
None,
None,
5.0,
true,
None,
);
let client = BaseClient::new("http://localhost:1933", None, None, None, 5.0, true, None);

let params = client.append_profile_query(&[("to_uri".to_string(), "viking://x".to_string())]);
let params =
client.append_profile_query(&[("to_uri".to_string(), "viking://x".to_string())]);

assert_eq!(
params,
Expand All @@ -611,16 +586,7 @@ mod tests {

#[test]
fn append_profile_query_keeps_existing_profile_flag() {
let client = BaseClient::new(
"http://localhost:1933",
None,
None,
None,
None,
5.0,
true,
None,
);
let client = BaseClient::new("http://localhost:1933", None, None, None, 5.0, true, None);

let params = client.append_profile_query(&[("profile".to_string(), "1".to_string())]);

Expand Down Expand Up @@ -649,7 +615,11 @@ impl<'a> FileUploader<'a> {
self
}

pub fn zip_directory(&self, dir_path: &Path, ignore_dirs: Option<&str>) -> Result<NamedTempFile> {
pub fn zip_directory(
&self,
dir_path: &Path,
ignore_dirs: Option<&str>,
) -> Result<NamedTempFile> {
if !dir_path.is_dir() {
return Err(Error::Network(format!(
"Path {} is not a directory",
Expand Down Expand Up @@ -778,7 +748,10 @@ impl<'a> FileUploader<'a> {

if let Some(pb) = pb {
if total_size > 0 {
pb.finish_with_message(format!("Compression complete: {:.2} MiB → {:.2} MiB", original_size_mb, zip_size_mb));
pb.finish_with_message(format!(
"Compression complete: {:.2} MiB → {:.2} MiB",
original_size_mb, zip_size_mb
));
} else {
pb.finish_with_message(format!("Compression complete: {:.2} MiB", zip_size_mb));
}
Expand Down Expand Up @@ -849,15 +822,22 @@ impl<'a> FileUploader<'a> {
let file_size = file_content.len() as u64;

if verbose {
eprintln!("Uploading: {} ({:.2} MB)", file_name, file_size as f64 / 1024.0 / 1024.0);
eprintln!(
"Uploading: {} ({:.2} MB)",
file_name,
file_size as f64 / 1024.0 / 1024.0
);
}

let pb = ProgressBar::new_spinner();
pb.set_message(format!("Uploading {} ({:.2} MB)...", file_name, file_size as f64 / 1024.0 / 1024.0));
pb.set_message(format!(
"Uploading {} ({:.2} MB)...",
file_name,
file_size as f64 / 1024.0 / 1024.0
));
pb.enable_steady_tick(std::time::Duration::from_millis(100));

let part = reqwest::multipart::Part::bytes(file_content)
.file_name(file_name.to_string());
let part = reqwest::multipart::Part::bytes(file_content).file_name(file_name.to_string());

let part = part
.mime_str("application/octet-stream")
Expand Down
17 changes: 8 additions & 9 deletions crates/ov_cli/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ impl HttpClient {
pub fn new(
base_url: impl Into<String>,
api_key: Option<String>,
agent_id: Option<String>,
account: Option<String>,
user: Option<String>,
timeout_secs: f64,
Expand All @@ -30,7 +29,6 @@ impl HttpClient {
base: BaseClient::new(
base_url,
api_key,
agent_id,
account,
user,
timeout_secs,
Expand All @@ -44,10 +42,6 @@ impl HttpClient {
self.base.user_id()
}

pub fn agent_id(&self) -> Option<&str> {
self.base.agent_id()
}

pub fn api_key(&self) -> Option<&str> {
self.base.api_key()
}
Expand Down Expand Up @@ -146,7 +140,8 @@ impl HttpClient {
verbose: bool,
ignore_dirs: Option<&str>,
) -> Result<tempfile::NamedTempFile> {
self.create_uploader().zip_directory_with_progress(dir_path, verbose, ignore_dirs)
self.create_uploader()
.zip_directory_with_progress(dir_path, verbose, ignore_dirs)
}

async fn upload_temp_file(&self, file_path: &Path) -> Result<String> {
Expand Down Expand Up @@ -377,6 +372,7 @@ impl HttpClient {
until: Option<String>,
time_field: Option<String>,
level: Option<Vec<i32>>,
peer_id: Option<String>,
) -> Result<serde_json::Value> {
let body = serde_json::json!({
"query": query,
Expand All @@ -387,6 +383,7 @@ impl HttpClient {
"until": until,
"time_field": time_field,
"level": level,
"peer_id": peer_id,
});
self.post("/api/v1/search/find", &body).await
}
Expand All @@ -402,6 +399,7 @@ impl HttpClient {
until: Option<String>,
time_field: Option<String>,
level: Option<Vec<i32>>,
peer_id: Option<String>,
) -> Result<serde_json::Value> {
let body = serde_json::json!({
"query": query,
Expand All @@ -413,6 +411,7 @@ impl HttpClient {
"until": until,
"time_field": time_field,
"level": level,
"peer_id": peer_id,
});
self.post("/api/v1/search/search", &body).await
}
Expand Down Expand Up @@ -1154,7 +1153,8 @@ impl HttpClient {
pub async fn trigger_watch_by_uri(&self, to_uri: &str) -> Result<serde_json::Value> {
let params = vec![("to_uri".to_string(), to_uri.to_string())];
let empty = serde_json::json!({});
self.post_with_query("/api/v1/watches/trigger", &empty, &params).await
self.post_with_query("/api/v1/watches/trigger", &empty, &params)
.await
}
}

Expand Down Expand Up @@ -1190,7 +1190,6 @@ mod tests {
let client = BaseClient::new(
"http://localhost:1933",
Some("test-key".to_string()),
Some("assistant-1".to_string()),
Some("acme".to_string()),
Some("alice".to_string()),
5.0,
Expand Down
4 changes: 3 additions & 1 deletion crates/ov_cli/src/commands/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ pub async fn list_users(
output_format: OutputFormat,
compact: bool,
) -> Result<()> {
let response = client.admin_list_users(account_id, limit, name, role).await?;
let response = client
.admin_list_users(account_id, limit, name, role)
.await?;
output_success(&response, output_format, compact);
Ok(())
}
Expand Down
22 changes: 2 additions & 20 deletions crates/ov_cli/src/commands/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,7 @@ pub async fn write(
output_format: OutputFormat,
compact: bool,
) -> Result<()> {
let result = client
.write(
uri,
content,
mode,
wait,
timeout,
)
.await?;
let result = client.write(uri, content, mode, wait, timeout).await?;
crate::output::output_success(result, output_format, compact);
Ok(())
}
Expand Down Expand Up @@ -133,17 +125,7 @@ mod tests {

assert_eq!(
rendered,
Some(
[
"content",
"",
"profile",
"line one",
"line two",
"",
]
.join("\n")
)
Some(["content", "", "profile", "line one", "line two", "",].join("\n"))
);
}
}
10 changes: 8 additions & 2 deletions crates/ov_cli/src/commands/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ pub async fn rm(
) -> Result<()> {
let result = client.rm(uri, recursive).await?;

let message = if let Some(count) = result.get("estimated_deleted_count").and_then(|v| v.as_u64()) {
let message = if let Some(count) = result
.get("estimated_deleted_count")
.and_then(|v| v.as_u64())
{
format!("Removed: {} ({} items)", uri, count)
} else {
format!("Removed: {}", uri)
Expand Down Expand Up @@ -128,7 +131,10 @@ fn output_message_result(
match output_format {
OutputFormat::Json => output_success(result, output_format, compact),
OutputFormat::Table => {
println!("{}", crate::output::append_profile_to_rendered(message, &result));
println!(
"{}",
crate::output::append_profile_to_rendered(message, &result)
);
}
}
}
Expand Down
Loading
Loading