aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / spark-harvester / src / client.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | use aien_provenance::{GrantPermissions, SourceGrant}; |
| 17 | use reqwest::Client; |
| 18 | use serde_json::{json, Value}; |
| 19 | use std::process::Command; |
| 20 | use thiserror::Error; |
| 21 | |
| 22 | #[derive(Debug, Error)] |
| 23 | pub enum ClientError { |
| 24 | #[error("Network request failed: {0}")] |
| 25 | Reqwest(#[from] reqwest::Error), |
| 26 | #[error("Vault key resolution failed for {0}: {1}")] |
| 27 | VaultKey(String, String), |
| 28 | #[error("API returned error: {0}")] |
| 29 | Api(String), |
| 30 | #[error("Rights verification failed: {0}")] |
| 31 | RightsDenied(String), |
| 32 | } |
| 33 | |
| 34 | pub struct HarvesterClient { |
| 35 | http: Client, |
| 36 | } |
| 37 | |
| 38 | impl Default for HarvesterClient { |
| 39 | fn default() -> Self { |
| 40 | Self::new() |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | impl HarvesterClient { |
| 45 | pub fn new() -> Self { |
| 46 | Self { |
| 47 | http: Client::builder() |
| 48 | .timeout(std::time::Duration::from_secs(60)) |
| 49 | .build() |
| 50 | .unwrap(), |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub fn resolve_vault_key(key_name: &str) -> Result<String, ClientError> { |
| 55 | let home = std::env::var("HOME") |
| 56 | .or_else(|_| std::env::var("USERPROFILE")) |
| 57 | .unwrap_or_else(|_| ".".to_string()); |
| 58 | let vault_candidate = format!("{}/.local/bin/atlas-vault", home); |
| 59 | let bin = if std::path::Path::new(&vault_candidate).is_file() { |
| 60 | &vault_candidate |
| 61 | } else { |
| 62 | "atlas-vault" |
| 63 | }; |
| 64 | let output = Command::new(bin).arg("get").arg(key_name).output(); |
| 65 | |
| 66 | match output { |
| 67 | Ok(out) if out.status.success() => { |
| 68 | let secret = String::from_utf8_lossy(&out.stdout).trim().to_string(); |
| 69 | if secret.is_empty() { |
| 70 | Err(ClientError::VaultKey( |
| 71 | key_name.to_string(), |
| 72 | "empty secret".to_string(), |
| 73 | )) |
| 74 | } else { |
| 75 | Ok(secret) |
| 76 | } |
| 77 | } |
| 78 | Ok(out) => Err(ClientError::VaultKey( |
| 79 | key_name.to_string(), |
| 80 | String::from_utf8_lossy(&out.stderr).trim().to_string(), |
| 81 | )), |
| 82 | Err(e) => Err(ClientError::VaultKey(key_name.to_string(), e.to_string())), |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | pub(crate) async fn query_openai_compat( |
| 87 | &self, |
| 88 | endpoint_url: &str, |
| 89 | api_key: &str, |
| 90 | model: &str, |
| 91 | prompt: &str, |
| 92 | ) -> Result<String, ClientError> { |
| 93 | let payload = json!({ |
| 94 | "model": model, |
| 95 | "messages": [{"role": "user", "content": prompt}], |
| 96 | "temperature": 0.6 |
| 97 | }); |
| 98 | |
| 99 | let res = self |
| 100 | .http |
| 101 | .post(endpoint_url) |
| 102 | .header("Authorization", format!("Bearer {}", api_key)) |
| 103 | .header("Content-Type", "application/json") |
| 104 | .json(&payload) |
| 105 | .send() |
| 106 | .await?; |
| 107 | |
| 108 | if !res.status().is_success() { |
| 109 | let status = res.status(); |
| 110 | let text = res.text().await.unwrap_or_default(); |
| 111 | return Err(ClientError::Api(format!("{}: {}", status, text))); |
| 112 | } |
| 113 | |
| 114 | let body: Value = res.json().await?; |
| 115 | let content = body["choices"][0]["message"]["content"] |
| 116 | .as_str() |
| 117 | .unwrap_or("") |
| 118 | .to_string(); |
| 119 | Ok(content) |
| 120 | } |
| 121 | |
| 122 | pub async fn query_with_grant( |
| 123 | &self, |
| 124 | endpoint_url: &str, |
| 125 | api_key: &str, |
| 126 | model: &str, |
| 127 | prompt: &str, |
| 128 | grant: &SourceGrant, |
| 129 | authority_verifying_key: &p256::ecdsa::VerifyingKey, |
| 130 | ) -> Result<String, ClientError> { |
| 131 | let now = chrono::Utc::now().timestamp() as u64; |
| 132 | grant |
| 133 | .verify_validity(now) |
| 134 | .map_err(|e| ClientError::RightsDenied(e.to_string()))?; |
| 135 | |
| 136 | grant |
| 137 | .verify_signature(authority_verifying_key) |
| 138 | .map_err(|e| ClientError::RightsDenied(e.to_string()))?; |
| 139 | |
| 140 | grant |
| 141 | .verify_permission(GrantPermissions::TRAIN | GrantPermissions::DISTILL) |
| 142 | .or_else(|_| grant.verify_permission(GrantPermissions::EVALUATE)) |
| 143 | .map_err(|e| ClientError::RightsDenied(e.to_string()))?; |
| 144 | |
| 145 | self.query_openai_compat(endpoint_url, api_key, model, prompt) |
| 146 | .await |
| 147 | } |
| 148 | } |