1use crate::training::{CheckpointManager, WeightFormat, WeightSnapshot};
4use ::config::{Config, File};
5use hyprstream_rpc_std::registry_client::CreateTagRequest;
6use hyprstream_rpc_std::registry_client::RegistryClient;
7use reqwest::Client;
8use serde_json::{json, Value};
9use std::net::SocketAddr;
10use std::path::{Path, PathBuf};
11use std::time::{SystemTime, UNIX_EPOCH};
12use tracing::{debug, error, info};
13
14#[derive(Debug, Clone)]
16pub struct InferenceResponse {
17 pub lora_id: String,
18 pub output: String,
19 pub tokens_generated: usize,
20 pub latency_ms: f64,
21 pub finish_reason: String,
22}
23
24pub async fn execute_sparse_query(
27 addr: Option<SocketAddr>,
28 query: String,
29 _config: Option<&Config>,
30 verbose: bool,
31) -> Result<(), Box<dyn std::error::Error>> {
32 let addr = addr.unwrap_or_else(|| SocketAddr::from(([127, 0, 0, 1], 3000)));
33 let base_url = format!("http://{addr}");
34
35 if verbose {
36 info!("Executing sparse query: {}", query);
37 debug!("Connecting to REST API at: {}", base_url);
38 }
39
40 let embedding_query: serde_json::Value = serde_json::from_str(&query)?;
42
43 if verbose {
44 debug!("Parsed embedding query: {:?}", embedding_query);
45 }
46
47 let client = Client::new();
48
49 let lora_id = embedding_query
51 .get("lora_id")
52 .and_then(|v| v.as_str())
53 .unwrap_or("default");
54
55 let url = format!("{base_url}/v1/inference/{lora_id}/embeddings");
57 let empty_input = json!("");
58 let request_body = json!({
59 "input": embedding_query.get("input").unwrap_or(&empty_input),
60 "model": "text-embedding-ada-002"
61 });
62
63 if verbose {
64 debug!("Making request to: {}", url);
65 debug!("Request body: {}", request_body);
66 }
67
68 let response = client.post(&url).json(&request_body).send().await?;
69
70 if response.status().is_success() {
71 let result: Value = response.json().await?;
72 if verbose {
73 debug!("Response: {}", serde_json::to_string_pretty(&result)?);
74 }
75 info!("✅ Embedding query processed successfully");
76 } else {
77 let status_code = response.status();
78 let error_text = response.text().await?;
79 error!(
80 "❌ Query failed with status {}: {}",
81 status_code, error_text
82 );
83 }
84
85 Ok(())
86}
87
88pub async fn handle_embedding_query(
89 host: Option<String>,
90 query: &str,
91 tls_cert: Option<&Path>,
92 tls_key: Option<&Path>,
93 tls_ca: Option<&Path>,
94 _tls_skip_verify: bool,
95 verbose: bool,
96) -> Result<(), Box<dyn std::error::Error>> {
97 let addr = host.unwrap_or_else(|| "localhost:50051".to_owned());
98
99 let config = match (tls_cert, tls_key) {
101 (Some(cert_path), Some(key_path)) => {
102 let _cert = tokio::fs::read(cert_path).await?;
103 let _key = tokio::fs::read(key_path).await?;
104 let _ca = if let Some(ca_path) = tls_ca {
105 Some(tokio::fs::read(ca_path).await?)
106 } else {
107 None
108 };
109
110 let config = Config::builder().build()?;
111
112 Some(config)
113 }
114 _ => None,
115 };
116
117 let addr_parts: Vec<&str> = addr.split(':').collect();
119 if addr_parts.len() != 2 {
120 return Err("Invalid address format. Expected host:port".into());
121 }
122
123 let socket_addr = SocketAddr::new(addr_parts[0].parse()?, addr_parts[1].parse()?);
124
125 execute_sparse_query(
127 Some(socket_addr),
128 query.to_owned(),
129 config.as_ref(),
130 verbose,
131 )
132 .await?;
133
134 Ok(())
135}
136
137pub fn handle_config(config_path: Option<PathBuf>) -> Result<(), Box<dyn std::error::Error>> {
138 let mut builder = Config::builder()
139 .set_default("host", "127.0.0.1")?
140 .set_default("port", "50051")?;
141
142 if let Some(path) = config_path {
144 builder = builder.add_source(File::from(path));
145 }
146
147 let settings = builder.build()?;
148 info!("📁 Configuration loaded successfully");
149 debug!(settings = ?settings, "Current configuration settings");
150 Ok(())
151}
152
153pub fn create_http_client() -> Client {
154 Client::builder()
155 .timeout(std::time::Duration::from_secs(300)) .build()
157 .unwrap_or_else(|_| Client::new())
158}
159
160pub async fn create_lora_via_api(
162 base_url: &str,
163 name: Option<String>,
164 base_model: &str,
165 rank: usize,
166 alpha: f32,
167 target_modules: &[String],
168 sparsity: f32,
169) -> Result<Value, Box<dyn std::error::Error>> {
170 let client = create_http_client();
171 let url = format!("{base_url}/v1/lora/create");
172
173 let request_body = json!({
174 "name": name,
175 "base_model": base_model,
176 "rank": rank,
177 "alpha": alpha,
178 "target_modules": target_modules,
179 "sparsity_ratio": sparsity,
180 "auto_regressive": true
181 });
182
183 let response = client.post(&url).json(&request_body).send().await?;
184
185 if response.status().is_success() {
186 let result: Value = response.json().await?;
187 Ok(result)
188 } else {
189 let status_code = response.status();
190 let error_text = response.text().await?;
191 Err(format!("Failed to create LoRA: HTTP {status_code} - {error_text}").into())
192 }
193}
194
195pub async fn list_lora_via_api(base_url: &str) -> Result<Value, Box<dyn std::error::Error>> {
197 let client = create_http_client();
198 let url = format!("{base_url}/v1/lora/list");
199
200 let response = client.get(&url).send().await?;
201
202 if response.status().is_success() {
203 let result: Value = response.json().await?;
204 Ok(result)
205 } else {
206 let status_code = response.status();
207 let error_text = response.text().await?;
208 Err(format!("Failed to list LoRA adapters: HTTP {status_code} - {error_text}").into())
209 }
210}
211
212pub async fn get_lora_info_via_api(
214 base_url: &str,
215 lora_id: &str,
216) -> Result<Value, Box<dyn std::error::Error>> {
217 let client = create_http_client();
218 let url = format!("{base_url}/v1/lora/{lora_id}/info");
219
220 let response = client.get(&url).send().await?;
221
222 if response.status().is_success() {
223 let result: Value = response.json().await?;
224 Ok(result)
225 } else {
226 let status_code = response.status();
227 let error_text = response.text().await?;
228 Err(format!("Failed to get LoRA info: HTTP {status_code} - {error_text}").into())
229 }
230}
231
232pub async fn chat_completion_via_api(
234 base_url: &str,
235 lora_id: &str,
236 messages: &[serde_json::Value],
237 max_tokens: Option<usize>,
238 temperature: Option<f32>,
239 stream: bool,
240) -> Result<Value, Box<dyn std::error::Error>> {
241 let client = create_http_client();
242 let url = format!("{base_url}/v1/inference/{lora_id}/chat/completions");
243
244 let request_body = json!({
245 "model": format!("lora-{}", lora_id),
246 "messages": messages,
247 "max_tokens": max_tokens,
248 "temperature": temperature,
249 "stream": stream
250 });
251
252 let response = client.post(&url).json(&request_body).send().await?;
253
254 if response.status().is_success() {
255 let result: Value = response.json().await?;
256 Ok(result)
257 } else {
258 let status_code = response.status();
259 let error_text = response.text().await?;
260 Err(format!("Failed to perform chat completion: HTTP {status_code} - {error_text}").into())
261 }
262}
263
264pub async fn handle_pretrain(
266 model_id: String,
267 _learning_rate: f32,
268 _steps: usize,
269 _warmup_steps: usize,
270 _batch_size: usize,
271 _checkpoint_every: Option<usize>,
272 _resume_from: Option<String>,
273) -> Result<(), Box<dyn std::error::Error>> {
274 error!("Pre-training is not supported for model: {}", model_id);
275 error!("The base model doesn't expose its VarStore for training");
276 error!("Please use LoRA fine-tuning instead with 'hyprstream lora create' and 'hyprstream lora train start'");
277
278 Err("Pre-training not supported - use LoRA fine-tuning instead".into())
279}
280
281pub async fn handle_write_checkpoint(
283 registry: &RegistryClient,
284 model_id: String,
285 _name: Option<String>,
286 step: Option<usize>,
287) -> Result<(), Box<dyn std::error::Error>> {
288 info!("Writing checkpoint for model: {}", model_id);
289
290 let model_ref = crate::storage::ModelRef::parse(&model_id)?;
292 let branch_override = model_ref.git_ref_str();
293 let tracked = registry.get_by_name(model_ref.name()).await?;
294 let repo_client = registry.repo(&tracked.id);
295 let branch_name = match branch_override.as_deref() {
296 Some(b) => b.to_owned(),
297 None => repo_client.get_head().await?,
298 };
299 let worktrees = repo_client.list_worktrees().await?;
300 if !worktrees.iter().any(|wt| wt.branch_name == branch_name) {
301 return Err(format!(
302 "worktree for {}:{} not found",
303 model_ref.name(),
304 branch_name
305 )
306 .into());
307 }
308 let storage_paths = crate::storage::StoragePaths::new()?;
310 let model_path = storage_paths.worktree_path(model_ref.name(), &branch_name)?;
311
312 let checkpoint_mgr = CheckpointManager::new(model_path.clone())?;
314
315 let step = step.unwrap_or_else(|| {
317 SystemTime::now()
319 .duration_since(UNIX_EPOCH)
320 .map(|d| d.as_secs() as usize)
321 .unwrap_or(0)
322 });
323
324 let weights = WeightSnapshot::Memory {
326 data: vec![], format: WeightFormat::SafeTensors,
328 };
329
330 let checkpoint_path = checkpoint_mgr.write_checkpoint(weights, step, None).await?;
332
333 info!("✅ Checkpoint written to: {}", checkpoint_path.display());
334
335 Ok(())
336}
337
338pub async fn handle_commit_checkpoint(
340 registry: &RegistryClient,
341 checkpoint_path: String,
342 message: Option<String>,
343 branch: Option<String>,
344 tag: Option<String>,
345) -> Result<(), Box<dyn std::error::Error>> {
346 info!("Committing checkpoint: {}", checkpoint_path);
347
348 let checkpoint_path = PathBuf::from(&checkpoint_path);
349
350 let model_path = checkpoint_path
352 .parent()
353 .and_then(|p| {
354 if p.file_name() == Some(std::ffi::OsStr::new(".checkpoints")) {
355 p.parent()
356 } else {
357 Some(p)
358 }
359 })
360 .ok_or("Invalid checkpoint path")?;
361
362 let model_name = model_path
364 .file_name()
365 .ok_or("Invalid model path")?
366 .to_string_lossy()
367 .to_string();
368
369 let repo_client = match registry.get_by_name(&model_name).await {
371 Ok(tracked) => Some(registry.repo(&tracked.id)),
372 Err(e) => {
373 error!(
374 "Could not get RepositoryClient for '{}': {} — git commit disabled",
375 model_name, e
376 );
377 None
378 }
379 };
380
381 let checkpoint_mgr = CheckpointManager::with_config(
383 model_path.to_path_buf(),
384 Default::default(),
385 None,
386 repo_client.clone(),
387 )?;
388
389 let commit_id = checkpoint_mgr
391 .commit_checkpoint(&checkpoint_path, message, branch)
392 .await?;
393
394 info!("✅ Checkpoint committed: {}", commit_id);
395
396 if let Some(tag_name) = tag {
398 if let Some(client) = &repo_client {
399 client
400 .create_tag(&CreateTagRequest {
401 name: tag_name.clone(),
402 target: String::new(),
403 })
404 .await
405 .map_err(|e| {
406 error!("Failed to create tag '{}': {}", tag_name, e);
407 e
408 })?;
409 info!("✅ Tag created: {}", tag_name);
410 } else {
411 error!("Cannot create tag: no RepositoryClient available");
412 }
413 }
414
415 Ok(())
416}