tracevault_cli/commands/
proxy.rs1use crate::credentials::{Credential, Credentials};
9
10const ANSI_BOLD: &str = "\x1b[1m";
11const ANSI_DIM: &str = "\x1b[2m";
12const ANSI_RESET: &str = "\x1b[0m";
13
14pub fn run_proxy_info() -> i32 {
17 let creds = match Credentials::load() {
18 Some(c) => c,
19 None => {
20 eprintln!(
21 "Not logged in. Run `tracevault login --server-url <url>` first \
22 to obtain a TraceVault session token, then try again."
23 );
24 eprintln!(
25 "Credentials file expected at: {}",
26 Credentials::path_for_display()
27 );
28 return 1;
29 }
30 };
31
32 let creds_path = Credentials::path_for_display();
33
34 let credential = match creds.credential() {
38 Some(c) => c,
39 None => {
40 eprintln!(
41 "The credentials file at {} holds no usable credential (no API key and no \
42 Keycloak session).",
43 creds_path
44 );
45 eprintln!(
46 "Run `tracevault login --server-url <url>`, or set TRACEVAULT_API_KEY to a \
47 TraceVault API key (tvk_...)."
48 );
49 return 1;
50 }
51 };
52
53 let server_url = creds.server_url.trim_end_matches('/');
54 let proxy_url = format!("{server_url}/proxy/anthropic");
55
56 println!("{ANSI_BOLD}TraceVault LLM Proxy{ANSI_RESET}");
57 println!();
58 println!(" Server: {server_url}");
59 println!(" Proxy base URL: {ANSI_BOLD}{proxy_url}{ANSI_RESET}");
60 println!(" Credentials file: {}", creds_path);
61 println!();
62 println!("{ANSI_BOLD}Setup{ANSI_RESET}");
63 println!();
64 println!(" 1. Configure your Anthropic API key once at:");
65 println!(" {server_url}/me/proxy");
66 println!();
67 println!(" 2. Set these environment variables for your AI tool:");
68 println!();
69 println!(" {ANSI_BOLD}export ANTHROPIC_BASE_URL=\"{proxy_url}\"{ANSI_RESET}");
70 println!(
71 " {ANSI_BOLD}export ANTHROPIC_API_KEY=\"<your TraceVault session token>\"{ANSI_RESET}"
72 );
73 println!();
74 match credential {
78 Credential::ApiKey(_) => {
79 println!(
80 " {ANSI_DIM}Your TraceVault token lives in {} as the \"token\" field.{ANSI_RESET}",
81 creds_path
82 );
83 }
84 Credential::Keycloak(_) => {
87 println!(
88 " {ANSI_DIM}This machine is signed in with a Keycloak session, whose access \
89 token expires every few minutes — it cannot be pasted here as a static value. \
90 Use a TraceVault API key (tvk_...) for the proxy instead.{ANSI_RESET}"
91 );
92 }
93 }
94 println!();
95 println!(" 3. Run your AI tool as usual. Requests go through TraceVault and are");
96 println!(" forwarded to api.anthropic.com using the Anthropic key you stored");
97 println!(" in step 1.");
98
99 0
100}
101
102#[cfg(test)]
103mod tests {
104 use super::run_proxy_info;
105 use std::fs;
106
107 fn exit_code_for(body: Option<&str>) -> i32 {
111 let _env_lock = crate::test_helpers::lock_env_mutation_sync();
112 let dir = tempfile::tempdir().unwrap();
113 let mut _guard = crate::test_helpers::EnvVarGuard::new();
114 _guard.set("XDG_CONFIG_HOME", dir.path());
115
116 if let Some(body) = body {
117 let creds_dir = dir.path().join("tracevault");
118 fs::create_dir_all(&creds_dir).unwrap();
119 fs::write(creds_dir.join("credentials.json"), body).unwrap();
120 }
121 run_proxy_info()
122 }
123
124 #[test]
128 fn a_file_with_no_usable_credential_exits_non_zero() {
129 let code = exit_code_for(Some(
130 r#"{"server_url":"https://example.com","email":"a@b.com"}"#,
131 ));
132 assert_eq!(
133 code, 1,
134 "a file with no usable credential must not report success"
135 );
136 }
137
138 #[test]
139 fn an_api_key_file_still_succeeds() {
140 let code = exit_code_for(Some(
141 r#"{"server_url":"https://example.com","token":"tvk_abc","email":"a@b.com"}"#,
142 ));
143 assert_eq!(code, 0);
144 }
145
146 #[test]
147 fn a_keycloak_file_still_succeeds() {
148 let code = exit_code_for(Some(
149 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}}"#,
150 ));
151 assert_eq!(code, 0);
152 }
153
154 #[test]
156 fn a_missing_credentials_file_exits_non_zero() {
157 assert_eq!(exit_code_for(None), 1);
158 }
159}