arknet-sdk 1.0.6

Public Rust SDK for building applications on arknet.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Public Rust SDK for arknet.
//!
//! Provides an async `Client` with OpenAI-compatible methods:
//! - `chat_completion` — non-streaming completions
//! - `chat_completion_stream` — streaming SSE completions
//! - `list_models` — query the on-chain model registry
//!
//! # Example
//!
//! ```rust,no_run
//! # async fn demo() -> arknet_sdk::Result<()> {
//! let client = arknet_sdk::Client::new("http://127.0.0.1:3000")?;
//! let resp = client.chat_completion(arknet_sdk::ChatRequest {
//!     model: "meta-llama/Llama-3-8B".into(),
//!     messages: vec![arknet_sdk::Message {
//!         role: "user".into(),
//!         content: "Hello!".into(),
//!     }],
//!     max_tokens: Some(64),
//!     ..Default::default()
//! }).await?;
//! println!("{}", resp.choices[0].message.content);
//! # Ok(())
//! # }
//! ```

#![forbid(unsafe_code)]
#![warn(missing_docs)]

pub mod errors;

use serde::{Deserialize, Serialize};

pub use errors::{Result, SdkError};

/// arknet SDK client. Wraps an HTTP connection to a node's
/// OpenAI-compatible API surface.
pub struct Client {
    base_url: String,
    http: reqwest::Client,
    api_key: Option<String>,
}

impl Client {
    /// Create a new client pointing at an arknet node.
    ///
    /// `base_url` should be the node's HTTP root, e.g.
    /// `http://127.0.0.1:3000`. The client appends `/v1/...` paths.
    /// Create a new client pointing at an arknet node.
    ///
    /// Reads wallet address from `ARKNET_WALLET` env var if not
    /// provided explicitly via [`ConnectOptions`].
    pub fn new(base_url: &str) -> Result<Self> {
        let base_url = base_url.trim_end_matches('/').to_string();
        let http = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(120))
            .build()
            .map_err(|e| SdkError::Http(e.to_string()))?;
        let api_key = std::env::var("ARKNET_WALLET").ok();
        Ok(Self {
            base_url,
            http,
            api_key,
        })
    }

    /// Auto-discover a gateway from the on-chain registry.
    ///
    /// Fetches the live seed list from `seeds.json` on the arknet
    /// website, then contacts each seed's `/v1/gateways`. If the
    /// seed list is unreachable, falls back to the hardcoded list.
    /// No code changes needed to add new seeds — just edit seeds.json.
    pub async fn connect(opts: ConnectOptions) -> Result<Self> {
        let seeds = if opts.seeds.is_empty() {
            fetch_seeds().await
        } else {
            opts.seeds
        };
        let http = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(10))
            .build()
            .map_err(|e| SdkError::Http(e.to_string()))?;

        for seed in &seeds {
            let url = format!("{}/v1/gateways", seed.trim_end_matches('/'));
            let resp = match http.get(&url).send().await {
                Ok(r) if r.status().is_success() => r,
                _ => continue,
            };
            let body: serde_json::Value = match resp.json().await {
                Ok(v) => v,
                Err(_) => continue,
            };
            let gateways = body["gateways"].as_array().cloned().unwrap_or_default();
            // Sort HTTPS first.
            let mut sorted = gateways;
            sorted.sort_by_key(|g| {
                if g["https"].as_bool() == Some(true) {
                    0
                } else {
                    1
                }
            });
            for gw in &sorted {
                let is_https = gw["https"].as_bool() == Some(true);
                if opts.require_https && !is_https {
                    continue;
                }
                if let Some(gw_url) = gw["url"].as_str() {
                    return Self::new(gw_url);
                }
            }
        }
        Err(SdkError::Http("no reachable gateway found".into()))
    }

    /// Non-streaming chat completion.
    pub async fn chat_completion(&self, req: ChatRequest) -> Result<ChatResponse> {
        let url = format!("{}/v1/chat/completions", self.base_url);
        let mut builder = self.http.post(&url).json(&req);
        if let Some(key) = &self.api_key {
            builder = builder.header("Authorization", format!("Bearer {key}"));
        }
        let resp = builder
            .send()
            .await
            .map_err(|e| SdkError::Http(e.to_string()))?;

        if !resp.status().is_success() {
            let status = resp.status().as_u16();
            let body = resp.text().await.unwrap_or_default();
            return Err(SdkError::Api { status, body });
        }

        resp.json::<ChatResponse>()
            .await
            .map_err(|e| SdkError::Http(e.to_string()))
    }

    /// List models from the on-chain registry.
    pub async fn list_models(&self) -> Result<ModelsResponse> {
        let url = format!("{}/v1/models", self.base_url);
        let mut builder = self.http.get(&url);
        if let Some(key) = &self.api_key {
            builder = builder.header("Authorization", format!("Bearer {key}"));
        }
        let resp = builder
            .send()
            .await
            .map_err(|e| SdkError::Http(e.to_string()))?;

        if !resp.status().is_success() {
            let status = resp.status().as_u16();
            let body = resp.text().await.unwrap_or_default();
            return Err(SdkError::Api { status, body });
        }

        resp.json::<ModelsResponse>()
            .await
            .map_err(|e| SdkError::Http(e.to_string()))
    }
}

const SEEDS_JSON_URL: &str = "https://arknet.arkengel.com/seeds.json";
const FALLBACK_SEEDS: &[&str] = &["https://api.arknet.arkengel.com"];

/// Fetch the live seed list from the static seeds.json file.
/// Falls back to the hardcoded list if unreachable.
async fn fetch_seeds() -> Vec<String> {
    let client = match reqwest::Client::builder()
        .timeout(std::time::Duration::from_secs(5))
        .build()
    {
        Ok(c) => c,
        Err(_) => return FALLBACK_SEEDS.iter().map(|s| s.to_string()).collect(),
    };
    let resp = match client.get(SEEDS_JSON_URL).send().await {
        Ok(r) if r.status().is_success() => r,
        _ => return FALLBACK_SEEDS.iter().map(|s| s.to_string()).collect(),
    };
    let body: serde_json::Value = match resp.json().await {
        Ok(v) => v,
        Err(_) => return FALLBACK_SEEDS.iter().map(|s| s.to_string()).collect(),
    };
    let urls: Vec<String> = body["seeds"]
        .as_array()
        .map(|arr| {
            arr.iter()
                .filter_map(|s| s["url"].as_str().map(String::from))
                .collect()
        })
        .unwrap_or_default();
    if urls.is_empty() {
        FALLBACK_SEEDS.iter().map(|s| s.to_string()).collect()
    } else {
        urls
    }
}

// ─── Request / response types ───────────────────────────────────────

/// Options for [`Client::connect`] auto-discovery.
#[derive(Clone, Debug, Default)]
pub struct ConnectOptions {
    /// Seed URLs to discover gateways. Defaults to the arknet seed list.
    pub seeds: Vec<String>,
    /// Only connect to HTTPS gateways.
    pub require_https: bool,
}

/// Chat completion request.
#[derive(Clone, Debug, Default, Serialize)]
pub struct ChatRequest {
    /// Model identifier.
    pub model: String,
    /// Conversation messages.
    pub messages: Vec<Message>,
    /// Maximum tokens to generate.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens: Option<u32>,
    /// Sampling temperature.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f64>,
    /// Whether to stream.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,
    /// Stop sequences.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop: Option<Vec<String>>,
    /// Route only to TEE-capable nodes (confidential inference).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prefer_tee: Option<bool>,
    /// Route only through HTTPS gateways.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub require_https: Option<bool>,
}

/// A chat message.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Message {
    /// Role: "system", "user", or "assistant".
    pub role: String,
    /// Message content.
    pub content: String,
}

/// Chat completion response.
#[derive(Clone, Debug, Deserialize)]
pub struct ChatResponse {
    /// Request identifier.
    pub id: String,
    /// Completions.
    pub choices: Vec<ChatChoice>,
    /// Token usage.
    pub usage: Option<TokenUsage>,
}

/// A single chat choice.
#[derive(Clone, Debug, Deserialize)]
pub struct ChatChoice {
    /// Index.
    pub index: u32,
    /// Generated message.
    pub message: Message,
    /// Why generation stopped.
    pub finish_reason: Option<String>,
}

/// Token counts.
#[derive(Clone, Debug, Deserialize)]
pub struct TokenUsage {
    /// Input tokens.
    pub prompt_tokens: u32,
    /// Output tokens.
    pub completion_tokens: u32,
    /// Total.
    pub total_tokens: u32,
}

/// Models list response.
#[derive(Clone, Debug, Deserialize)]
pub struct ModelsResponse {
    /// Model entries.
    pub data: Vec<ModelInfo>,
}

/// A model entry.
#[derive(Clone, Debug, Deserialize)]
pub struct ModelInfo {
    /// Model identifier.
    pub id: String,
    /// Owner.
    pub owned_by: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn client_trims_trailing_slash() {
        let c = Client::new("http://localhost:3000/").unwrap();
        assert_eq!(c.base_url, "http://localhost:3000");
    }

    #[test]
    fn chat_request_serializes() {
        let req = ChatRequest {
            model: "test".into(),
            messages: vec![Message {
                role: "user".into(),
                content: "hi".into(),
            }],
            max_tokens: Some(10),
            ..Default::default()
        };
        let json = serde_json::to_string(&req).unwrap();
        assert!(json.contains("\"model\":\"test\""));
        assert!(json.contains("\"max_tokens\":10"));
        assert!(!json.contains("stream"));
    }

    #[test]
    fn chat_response_deserializes() {
        let json = r#"{
            "id": "chatcmpl-test",
            "choices": [{
                "index": 0,
                "message": {"role": "assistant", "content": "hello"},
                "finish_reason": "stop"
            }],
            "usage": {
                "prompt_tokens": 5,
                "completion_tokens": 1,
                "total_tokens": 6
            }
        }"#;
        let resp: ChatResponse = serde_json::from_str(json).unwrap();
        assert_eq!(resp.choices.len(), 1);
        assert_eq!(resp.choices[0].message.content, "hello");
    }

    #[test]
    fn models_response_deserializes() {
        let json = r#"{
            "object": "list",
            "data": [
                {"id": "llama-3-8b", "object": "model", "created": 0, "owned_by": "user"}
            ]
        }"#;
        let resp: ModelsResponse = serde_json::from_str(json).unwrap();
        assert_eq!(resp.data.len(), 1);
        assert_eq!(resp.data[0].id, "llama-3-8b");
    }

    #[test]
    fn api_key_from_env() {
        std::env::set_var("ARKNET_WALLET", "ark1fromenv");
        let c = Client::new("http://localhost:1234").unwrap();
        assert_eq!(c.api_key.as_deref(), Some("ark1fromenv"));
        std::env::remove_var("ARKNET_WALLET");
    }

    #[test]
    fn prefer_tee_serialized_when_set() {
        let req = ChatRequest {
            model: "test".into(),
            messages: vec![],
            prefer_tee: Some(true),
            ..Default::default()
        };
        let json = serde_json::to_string(&req).unwrap();
        assert!(json.contains("\"prefer_tee\":true"));
    }

    #[test]
    fn prefer_tee_omitted_when_none() {
        let req = ChatRequest {
            model: "test".into(),
            messages: vec![],
            ..Default::default()
        };
        let json = serde_json::to_string(&req).unwrap();
        assert!(!json.contains("prefer_tee"));
    }

    #[test]
    fn require_https_serialized_when_set() {
        let req = ChatRequest {
            model: "test".into(),
            messages: vec![],
            require_https: Some(true),
            ..Default::default()
        };
        let json = serde_json::to_string(&req).unwrap();
        assert!(json.contains("\"require_https\":true"));
    }

    #[test]
    fn connect_options_defaults() {
        let opts = ConnectOptions::default();
        assert!(opts.seeds.is_empty());
        assert!(!opts.require_https);
    }
}