use crate::credentials::{Credential, Credentials};
const ANSI_BOLD: &str = "\x1b[1m";
const ANSI_DIM: &str = "\x1b[2m";
const ANSI_RESET: &str = "\x1b[0m";
pub fn run_proxy_info() -> i32 {
let creds = match Credentials::load() {
Some(c) => c,
None => {
eprintln!(
"Not logged in. Run `tracevault login --server-url <url>` first \
to obtain a TraceVault session token, then try again."
);
eprintln!(
"Credentials file expected at: {}",
Credentials::path_for_display()
);
return 1;
}
};
let creds_path = Credentials::path_for_display();
let credential = match creds.credential() {
Some(c) => c,
None => {
eprintln!(
"The credentials file at {} holds no usable credential (no API key and no \
Keycloak session).",
creds_path
);
eprintln!(
"Run `tracevault login --server-url <url>`, or set TRACEVAULT_API_KEY to a \
TraceVault API key (tvk_...)."
);
return 1;
}
};
let server_url = creds.server_url.trim_end_matches('/');
let proxy_url = format!("{server_url}/proxy/anthropic");
println!("{ANSI_BOLD}TraceVault LLM Proxy{ANSI_RESET}");
println!();
println!(" Server: {server_url}");
println!(" Proxy base URL: {ANSI_BOLD}{proxy_url}{ANSI_RESET}");
println!(" Credentials file: {}", creds_path);
println!();
println!("{ANSI_BOLD}Setup{ANSI_RESET}");
println!();
println!(" 1. Configure your Anthropic API key once at:");
println!(" {server_url}/me/proxy");
println!();
println!(" 2. Set these environment variables for your AI tool:");
println!();
println!(" {ANSI_BOLD}export ANTHROPIC_BASE_URL=\"{proxy_url}\"{ANSI_RESET}");
println!(
" {ANSI_BOLD}export ANTHROPIC_API_KEY=\"<your TraceVault session token>\"{ANSI_RESET}"
);
println!();
match credential {
Credential::ApiKey(_) => {
println!(
" {ANSI_DIM}Your TraceVault token lives in {} as the \"token\" field.{ANSI_RESET}",
creds_path
);
}
Credential::Keycloak(_) => {
println!(
" {ANSI_DIM}This machine is signed in with a Keycloak session, whose access \
token expires every few minutes — it cannot be pasted here as a static value. \
Use a TraceVault API key (tvk_...) for the proxy instead.{ANSI_RESET}"
);
}
}
println!();
println!(" 3. Run your AI tool as usual. Requests go through TraceVault and are");
println!(" forwarded to api.anthropic.com using the Anthropic key you stored");
println!(" in step 1.");
0
}
#[cfg(test)]
mod tests {
use super::run_proxy_info;
use std::fs;
fn exit_code_for(body: Option<&str>) -> i32 {
let _env_lock = crate::test_helpers::lock_env_mutation_sync();
let dir = tempfile::tempdir().unwrap();
let mut _guard = crate::test_helpers::EnvVarGuard::new();
_guard.set("XDG_CONFIG_HOME", dir.path());
if let Some(body) = body {
let creds_dir = dir.path().join("tracevault");
fs::create_dir_all(&creds_dir).unwrap();
fs::write(creds_dir.join("credentials.json"), body).unwrap();
}
run_proxy_info()
}
#[test]
fn a_file_with_no_usable_credential_exits_non_zero() {
let code = exit_code_for(Some(
r#"{"server_url":"https://example.com","email":"a@b.com"}"#,
));
assert_eq!(
code, 1,
"a file with no usable credential must not report success"
);
}
#[test]
fn an_api_key_file_still_succeeds() {
let code = exit_code_for(Some(
r#"{"server_url":"https://example.com","token":"tvk_abc","email":"a@b.com"}"#,
));
assert_eq!(code, 0);
}
#[test]
fn a_keycloak_file_still_succeeds() {
let code = exit_code_for(Some(
r#"{"server_url":"https://example.com","email":"a@b.com","auth":{"issuer":"i","client_id":"c","refresh_token":"rt","access_token":"at","access_expires_at":1}}"#,
));
assert_eq!(code, 0);
}
#[test]
fn a_missing_credentials_file_exits_non_zero() {
assert_eq!(exit_code_for(None), 1);
}
}