nabugate 1.0.0

Official Rust SDK for NabuGate, the OpenAI-compatible AI gateway
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! Official Rust client for NabuGate, the OpenAI-compatible AI gateway.
//!
//! The gateway passes request bodies through to the upstream provider untouched,
//! so [`ChatRequest`] carries an `extra` map alongside its typed fields. Anything
//! placed there reaches the provider as-is, which means a new provider parameter
//! needs no release of this crate.
//!
//! ```no_run
//! # async fn run() -> Result<(), nabugate::Error> {
//! use nabugate::{ChatRequest, Message, NabuGateClient};
//!
//! let client = NabuGateClient::new(std::env::var("NABUGATE_API_KEY").unwrap());
//! let text = client
//!     .complete_text(ChatRequest::new(vec![Message::user("Summarise this quarter.")]))
//!     .await?;
//! println!("{text}");
//! # Ok(())
//! # }
//! ```

use std::collections::HashMap;

use futures_util::StreamExt;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// The hosted gateway.
pub const DEFAULT_BASE_URL: &str = "https://gate.nabuxai.com/v1";

/// Anything that can go wrong talking to the gateway.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("nabugate: transport error: {0}")]
    Transport(#[from] reqwest::Error),
    #[error("nabugate: request failed ({status}): {body}")]
    Api { status: u16, body: String },
    #[error("nabugate: could not decode the response: {0}")]
    Decode(#[from] serde_json::Error),
}

/// One chat message. `content` is a [`Value`] so multimodal parts and tool
/// results pass through unchanged.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    pub role: String,
    pub content: Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_call_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_calls: Option<Value>,
}

impl Message {
    fn text(role: &str, content: impl Into<String>) -> Self {
        Self {
            role: role.to_string(),
            content: Value::String(content.into()),
            name: None,
            tool_call_id: None,
            tool_calls: None,
        }
    }

    pub fn system(content: impl Into<String>) -> Self {
        Self::text("system", content)
    }

    pub fn user(content: impl Into<String>) -> Self {
        Self::text("user", content)
    }

    pub fn assistant(content: impl Into<String>) -> Self {
        Self::text("assistant", content)
    }

    /// The message text, when the content is a plain string.
    pub fn as_text(&self) -> &str {
        self.content.as_str().unwrap_or_default()
    }
}

/// A chat completion request.
#[derive(Debug, Clone, Default, Serialize)]
pub struct ChatRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    pub messages: Vec<Message>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub seed: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_choice: Option<Value>,
    /// Asks the gateway to replay a stored conversation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub conversation_id: Option<String>,

    /// Every parameter this struct does not name, merged into the body as-is.
    #[serde(flatten)]
    pub extra: HashMap<String, Value>,
}

impl ChatRequest {
    pub fn new(messages: Vec<Message>) -> Self {
        Self {
            messages,
            ..Default::default()
        }
    }

    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = Some(model.into());
        self
    }

    pub fn temperature(mut self, temperature: f32) -> Self {
        self.temperature = Some(temperature);
        self
    }

    pub fn max_tokens(mut self, max_tokens: u32) -> Self {
        self.max_tokens = Some(max_tokens);
        self
    }

    /// Sets a parameter this struct does not name.
    pub fn param(mut self, key: impl Into<String>, value: Value) -> Self {
        self.extra.insert(key.into(), value);
        self
    }
}

/// Token consumption for a request.
#[derive(Debug, Clone, Default, Deserialize)]
pub struct Usage {
    #[serde(default)]
    pub prompt_tokens: u32,
    #[serde(default)]
    pub completion_tokens: u32,
    #[serde(default)]
    pub total_tokens: u32,
}

/// One completion alternative.
#[derive(Debug, Clone, Deserialize)]
pub struct ChatChoice {
    #[serde(default)]
    pub index: u32,
    pub message: Message,
    #[serde(default)]
    pub finish_reason: Option<String>,
}

/// A chat completion response.
#[derive(Debug, Clone, Deserialize)]
pub struct ChatResponse {
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub model: String,
    pub choices: Vec<ChatChoice>,
    #[serde(default)]
    pub usage: Usage,
}

impl ChatResponse {
    /// The first choice's text, when it is a plain string.
    pub fn text(&self) -> &str {
        self.choices
            .first()
            .map(|c| c.message.as_text())
            .unwrap_or_default()
    }
}

/// One server-sent event from a streaming completion.
#[derive(Debug, Clone, Deserialize)]
pub struct StreamChunk {
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub choices: Vec<StreamChoice>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct StreamChoice {
    #[serde(default)]
    pub delta: Delta,
    #[serde(default)]
    pub finish_reason: Option<String>,
}

#[derive(Debug, Clone, Default, Deserialize)]
pub struct Delta {
    #[serde(default)]
    pub role: Option<String>,
    #[serde(default)]
    pub content: Option<String>,
    #[serde(default)]
    pub tool_calls: Option<Value>,
}

/// A vector embedding request.
#[derive(Debug, Clone, Serialize)]
pub struct EmbeddingsRequest {
    pub model: String,
    /// A string or an array of strings.
    pub input: Value,
    /// Pins the vector width. Set it whenever the vectors are stored: a
    /// fixed-width column cannot accept whatever the provider defaults to.
    /// Leave it `None` for ad-hoc search, since providers without the field
    /// reject it.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dimensions: Option<u32>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct Embedding {
    #[serde(default)]
    pub index: u32,
    pub embedding: Vec<f32>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct EmbeddingsResponse {
    #[serde(default)]
    pub model: String,
    pub data: Vec<Embedding>,
    #[serde(default)]
    pub usage: Usage,
}

#[derive(Debug, Clone, Deserialize)]
pub struct ModelInfo {
    pub id: String,
    #[serde(default)]
    pub object: String,
    #[serde(default)]
    pub owned_by: String,
}

/// Client for a NabuGate deployment.
#[derive(Clone)]
pub struct NabuGateClient {
    api_key: String,
    base_url: String,
    default_model: String,
    http: reqwest::Client,
}

impl NabuGateClient {
    /// Builds a client against the hosted gateway.
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            api_key: api_key.into(),
            base_url: DEFAULT_BASE_URL.to_string(),
            default_model: "nabu-smart".to_string(),
            http: reqwest::Client::new(),
        }
    }

    /// Points the client at a different deployment.
    pub fn base_url(mut self, base_url: impl Into<String>) -> Self {
        self.base_url = base_url.into().trim_end_matches('/').to_string();
        self
    }

    /// Sets the alias or agent used when a request names none.
    pub fn default_model(mut self, model: impl Into<String>) -> Self {
        self.default_model = model.into();
        self
    }

    /// Supplies a pre-configured HTTP client (proxies, tracing, timeouts).
    pub fn http_client(mut self, http: reqwest::Client) -> Self {
        self.http = http;
        self
    }

    /// Performs a chat completion.
    pub async fn chat(&self, request: ChatRequest) -> Result<ChatResponse, Error> {
        let body = self.chat_body(request, false)?;
        self.post_json("/chat/completions", &body).await
    }

    /// Performs a chat completion and returns only the text.
    pub async fn complete_text(&self, request: ChatRequest) -> Result<String, Error> {
        Ok(self.chat(request).await?.text().trim().to_string())
    }

    /// Streams a chat completion, invoking `on_chunk` for each event.
    ///
    /// Returning `false` from the callback stops the stream.
    pub async fn stream<F>(&self, request: ChatRequest, mut on_chunk: F) -> Result<(), Error>
    where
        F: FnMut(StreamChunk) -> bool,
    {
        let body = self.chat_body(request, true)?;
        let response = self.send("/chat/completions", &body).await?;
        let mut stream = response.bytes_stream();
        let mut buffer = String::new();

        while let Some(bytes) = stream.next().await {
            buffer.push_str(&String::from_utf8_lossy(&bytes?));

            // Keep the trailing partial line for the next read; parsing it early
            // is how streaming clients drop the tail of a chunk.
            while let Some(newline) = buffer.find('\n') {
                let line = buffer[..newline].trim().to_string();
                buffer.drain(..=newline);

                let Some(payload) = line.strip_prefix("data:") else {
                    continue;
                };
                let payload = payload.trim();
                if payload == "[DONE]" {
                    return Ok(());
                }
                // Skip an unparseable payload rather than failing the stream.
                if let Ok(chunk) = serde_json::from_str::<StreamChunk>(payload) {
                    if !on_chunk(chunk) {
                        return Ok(());
                    }
                }
            }
        }
        Ok(())
    }

    /// Streams a chat completion, reduced to text deltas.
    pub async fn stream_text<F>(&self, request: ChatRequest, mut on_text: F) -> Result<(), Error>
    where
        F: FnMut(&str) -> bool,
    {
        self.stream(request, |chunk| {
            for choice in &chunk.choices {
                if let Some(content) = &choice.delta.content {
                    if !content.is_empty() && !on_text(content) {
                        return false;
                    }
                }
            }
            true
        })
        .await
    }

    /// Creates embeddings.
    pub async fn embeddings(
        &self,
        input: Value,
        model: Option<&str>,
        dimensions: Option<u32>,
    ) -> Result<EmbeddingsResponse, Error> {
        let request = EmbeddingsRequest {
            model: model.unwrap_or("nabu-embed").to_string(),
            input,
            dimensions,
        };
        self.post_json("/embeddings", &request).await
    }

    /// Generates images. Results carry base64 data in `data[].b64_json`.
    pub async fn images(&self, prompt: &str, model: Option<&str>) -> Result<Value, Error> {
        let body = serde_json::json!({
            "model": model.unwrap_or("nabu-image"),
            "prompt": prompt,
        });
        self.post_json("/images/generations", &body).await
    }

    /// Synthesises speech and returns the raw audio bytes.
    pub async fn speech(
        &self,
        input: &str,
        model: Option<&str>,
        voice: Option<&str>,
    ) -> Result<Vec<u8>, Error> {
        let mut body = serde_json::json!({
            "model": model.unwrap_or("nabu-voice"),
            "input": input,
        });
        if let Some(voice) = voice {
            body["voice"] = Value::String(voice.to_string());
        }
        let response = self.send("/audio/speech", &body).await?;
        Ok(response.bytes().await?.to_vec())
    }

    /// Lists every model, alias and agent this key may call.
    pub async fn models(&self) -> Result<Vec<ModelInfo>, Error> {
        #[derive(Deserialize)]
        struct Catalogue {
            #[serde(default)]
            data: Vec<ModelInfo>,
        }
        let catalogue: Catalogue = self.get_json("/models").await?;
        Ok(catalogue.data)
    }

    /// Returns token and cost usage for this key.
    pub async fn usage(&self) -> Result<Value, Error> {
        self.get_json("/usage").await
    }

    // ------------------------------------------------------------ internals

    fn chat_body(&self, mut request: ChatRequest, stream: bool) -> Result<Value, Error> {
        if request.model.is_none() {
            request.model = Some(self.default_model.clone());
        }
        let mut body = serde_json::to_value(request)?;
        if let Some(map) = body.as_object_mut() {
            map.insert("stream".to_string(), Value::Bool(stream));
        }
        Ok(body)
    }

    async fn send<B: Serialize>(&self, path: &str, body: &B) -> Result<reqwest::Response, Error> {
        let response = self
            .http
            .post(format!("{}{}", self.base_url, path))
            .bearer_auth(&self.api_key)
            .json(body)
            .send()
            .await?;
        Self::check(response).await
    }

    async fn check(response: reqwest::Response) -> Result<reqwest::Response, Error> {
        let status = response.status();
        if status.is_success() {
            return Ok(response);
        }
        let body = response.text().await.unwrap_or_default();
        Err(Error::Api {
            status: status.as_u16(),
            body,
        })
    }

    async fn post_json<B: Serialize, T: for<'de> Deserialize<'de>>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T, Error> {
        let text = self.send(path, body).await?.text().await?;
        Ok(serde_json::from_str(&text)?)
    }

    async fn get_json<T: for<'de> Deserialize<'de>>(&self, path: &str) -> Result<T, Error> {
        let response = self
            .http
            .get(format!("{}{}", self.base_url, path))
            .bearer_auth(&self.api_key)
            .send()
            .await?;
        let text = Self::check(response).await?.text().await?;
        Ok(serde_json::from_str(&text)?)
    }
}