interplane.Get Omega
Read in your language

harvester

Zero-telemetry documentation and knowledge extraction engine

Brought by drakestapleton

branch main Branches Tags History

harvester / src / client.rs

93 lines · 2688 bytes Raw Blame History
1use reqwest::Client;
2use serde_json::{json, Value};
3use std::process::Command;
4use thiserror::Error;
5
6#[derive(Debug, Error)]
7pub enum ClientError {
8 #[error("Network request failed: {0}")]
9 Reqwest(#[from] reqwest::Error),
10 #[error("Vault key resolution failed for {0}: {1}")]
11 VaultKey(String, String),
12 #[error("API returned error: {0}")]
13 Api(String),
14}
15
16pub struct HarvesterClient {
17 http: Client,
18}
19
20impl Default for HarvesterClient {
21 fn default() -> Self {
22 Self::new()
23 }
24}
25
26impl HarvesterClient {
27 pub fn new() -> Self {
28 Self {
29 http: Client::builder()
30 .timeout(std::time::Duration::from_secs(60))
31 .build()
32 .unwrap(),
33 }
34 }
35
36 pub fn resolve_vault_key(key_name: &str) -> Result<String, ClientError> {
37 let output = Command::new("/home/drakestapleton/.local/bin/atlas-vault")
38 .arg("get")
39 .arg(key_name)
40 .output();
41
42 match output {
43 Ok(out) if out.status.success() => {
44 let secret = String::from_utf8_lossy(&out.stdout).trim().to_string();
45 if secret.is_empty() {
46 Err(ClientError::VaultKey(key_name.to_string(), "empty secret".to_string()))
47 } else {
48 Ok(secret)
49 }
50 }
51 Ok(out) => Err(ClientError::VaultKey(
52 key_name.to_string(),
53 String::from_utf8_lossy(&out.stderr).trim().to_string(),
54 )),
55 Err(e) => Err(ClientError::VaultKey(key_name.to_string(), e.to_string())),
56 }
57 }
58
59 pub async fn query_openai_compat(
60 &self,
61 endpoint_url: &str,
62 api_key: &str,
63 model: &str,
64 prompt: &str,
65 ) -> Result<String, ClientError> {
66 let payload = json!({
67 "model": model,
68 "messages": [{"role": "user", "content": prompt}],
69 "temperature": 0.6
70 });
71
72 let res = self.http
73 .post(endpoint_url)
74 .header("Authorization", format!("Bearer {}", api_key))
75 .header("Content-Type", "application/json")
76 .json(&payload)
77 .send()
78 .await?;
79
80 if !res.status().is_success() {
81 let status = res.status();
82 let text = res.text().await.unwrap_or_default();
83 return Err(ClientError::Api(format!("{}: {}", status, text)));
84 }
85
86 let body: Value = res.json().await?;
87 let content = body["choices"][0]["message"]["content"]
88 .as_str()
89 .unwrap_or("")
90 .to_string();
91 Ok(content)
92 }
93}