typesafe-ai-sdk 0.2.0

Rust client for the TypeSafe AI System One API (Noul, Choice and Score questions)
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//! The asynchronous client.

use std::future::{Future, IntoFuture};
use std::pin::Pin;
use std::sync::Arc;
use std::time::{Duration, Instant};

use http::header::{
    ACCEPT, AUTHORIZATION, CONTENT_TYPE, HeaderMap, HeaderName, HeaderValue, USER_AGENT,
};
use http::{Method, StatusCode};
use serde::Serialize;
use serde_json::{Map, Value};

use crate::constants::*;
use crate::error::{ApiError, Error, ResponseValidationError, Result, lenient_body};
use crate::question::Questions;
use crate::response::{
    DecodeFailure, DecodedSystemOne, ListModelsResponse, ResponseMeta, SystemOneResponse,
    decode_models, decode_system_one,
};
use crate::retry::RetryPolicy;

type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

/// Builder for [`Client`]. Explicit settings win over environment variables; empty or
/// whitespace-only environment values are ignored.
#[derive(Debug, Default)]
pub struct ClientBuilder {
    api_key: Option<String>,
    base_url: Option<String>,
    model: Option<String>,
    timeout: Option<Duration>,
    retry: Option<RetryPolicy>,
    headers: HeaderMap,
    http: Option<reqwest::Client>,
}

impl ClientBuilder {
    /// API key (else `TYPESAFE_API_KEY`).
    pub fn api_key(mut self, key: impl Into<String>) -> Self {
        self.api_key = Some(key.into());
        self
    }

    /// API root (else `TYPESAFE_BASE_URL`, else `https://api.typesafe.ai`).
    pub fn base_url(mut self, url: impl Into<String>) -> Self {
        self.base_url = Some(url.into());
        self
    }

    /// Default model (else `TYPESAFE_DEFAULT_MODEL`, else `jev-latest`).
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = Some(model.into());
        self
    }

    /// Per-attempt timeout (default 10s).
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Default retry policy.
    pub fn retry(mut self, policy: RetryPolicy) -> Self {
        self.retry = Some(policy);
        self
    }

    /// Extra header sent with every request. Authentication and SDK identification headers
    /// cannot be overridden.
    pub fn header(mut self, name: HeaderName, value: HeaderValue) -> Self {
        self.headers.insert(name, value);
        self
    }

    /// Use your own `reqwest::Client` (proxies, TLS, connection pools…). Requires the
    /// `reqwest-client` feature.
    #[cfg(feature = "reqwest-client")]
    pub fn http_client(mut self, client: reqwest::Client) -> Self {
        self.http = Some(client);
        self
    }

    /// Build the client.
    pub fn build(self) -> Result<Client> {
        let api_key = resolve(self.api_key, API_KEY_ENV, None).ok_or_else(|| {
            Error::Config(format!(
                "No API key was provided. Pass api_key or set the {API_KEY_ENV} environment variable."
            ))
        })?;
        let base_url = resolve(self.base_url, BASE_URL_ENV, Some(DEFAULT_BASE_URL))
            .unwrap_or_default()
            .trim_end_matches('/')
            .to_owned();
        check_base_url(&base_url)?;
        let model = resolve(self.model, DEFAULT_MODEL_ENV, Some(DEFAULT_MODEL)).unwrap_or_default();
        let timeout = check_timeout(self.timeout.unwrap_or(DEFAULT_TIMEOUT))?;
        let retry = self.retry.unwrap_or_default();
        retry.validate()?;

        let mut auth = HeaderValue::from_str(&format!("Bearer {api_key}")).map_err(|_| {
            Error::Config("The API key contains characters not allowed in a header.".into())
        })?;
        auth.set_sensitive(true);

        let mut protected = HeaderMap::new();
        protected.insert(AUTHORIZATION, auth);
        protected.insert(ACCEPT, HeaderValue::from_static("application/json"));
        let ident = HeaderValue::from_str(&format!("{SDK_NAME}/{VERSION}")).expect("ascii");
        protected.insert(USER_AGENT, ident.clone());
        protected.insert(HeaderName::from_static(SDK_HEADER), ident);
        protected.insert(
            HeaderName::from_static(RUNTIME_HEADER),
            HeaderValue::from_str(&format!(
                "rust ({}; {})",
                std::env::consts::OS,
                std::env::consts::ARCH
            ))
            .expect("ascii"),
        );

        Ok(Client {
            inner: Arc::new(Inner {
                http: self.http.unwrap_or_default(),
                base_url,
                model,
                timeout,
                retry,
                default_headers: self.headers,
                protected,
            }),
        })
    }
}

fn resolve(explicit: Option<String>, env: &str, default: Option<&str>) -> Option<String> {
    explicit
        .or_else(|| {
            std::env::var(env)
                .ok()
                .map(|v| v.trim().to_owned())
                .filter(|v| !v.is_empty())
        })
        .or_else(|| default.map(str::to_owned))
}

fn check_base_url(url: &str) -> Result<()> {
    match reqwest::Url::parse(url) {
        Ok(u) if matches!(u.scheme(), "http" | "https") && u.has_host() => Ok(()),
        Ok(_) => Err(Error::Config(format!(
            "base_url must be an http(s) URL with a host, got {url:?}."
        ))),
        Err(e) => Err(Error::Config(format!(
            "base_url {url:?} is not a valid URL: {e}."
        ))),
    }
}

fn check_timeout(t: Duration) -> Result<Duration> {
    if t.is_zero() {
        Err(Error::Config("timeout must be a positive duration.".into()))
    } else {
        Ok(t)
    }
}

#[derive(Debug)]
struct Inner {
    http: reqwest::Client,
    base_url: String,
    model: String,
    timeout: Duration,
    retry: RetryPolicy,
    default_headers: HeaderMap,
    protected: HeaderMap,
}

/// TypeSafe API client. Cheap to clone; clones share the connection pool.
///
/// ```no_run
/// use typesafe::{Client, Choice, Noul, Questions, Score};
///
/// # async fn run() -> typesafe::Result<()> {
/// let client = Client::from_env()?;
/// let res = client
///     .system_one(
///         "I've been trying to connect Stripe for 3 days. Please help ASAP.",
///         Questions::new()
///             .with("department", Choice::new("Which team should handle this")
///                 .option("billing", "Payment or subscription issues")
///                 .option("technical", "Bugs or integration problems"))
///             .with("frustration", Score::new("How frustrated", ["Calm", "Frustrated", "Very angry"]))
///             .with("is_urgent", Noul::new("The message conveys urgency")),
///     )
///     .await?;
/// println!("{}", res.choice("department").unwrap().choice);
/// # Ok(()) }
/// ```
#[derive(Debug, Clone)]
pub struct Client {
    inner: Arc<Inner>,
}

impl Client {
    /// Start configuring a client.
    pub fn builder() -> ClientBuilder {
        ClientBuilder::default()
    }

    /// A client configured entirely from the environment.
    pub fn from_env() -> Result<Self> {
        Self::builder().build()
    }

    /// The default model.
    pub fn default_model(&self) -> &str {
        &self.inner.model
    }

    /// Ask typed questions about `state` (a string, or anything `Serialize` that becomes a JSON
    /// object/array). Returns a request builder: `.await` it directly or set per-call options first.
    pub fn system_one<S: Serialize>(
        &self,
        state: S,
        questions: impl Into<Questions>,
    ) -> SystemOneRequest {
        SystemOneRequest {
            client: self.clone(),
            state: serde_json::to_value(state)
                .map_err(|e| format!("The state could not be encoded as JSON: {e}")),
            questions: questions.into(),
            model: None,
            extra_body: Map::new(),
            opts: CallOptions::default(),
        }
    }

    /// The Models resource.
    pub fn models(&self) -> Models {
        Models {
            client: self.clone(),
        }
    }

    async fn execute(
        &self,
        method: Method,
        path: &str,
        body: Option<Vec<u8>>,
        opts: CallOptions,
    ) -> Result<(Vec<u8>, ResponseMeta, String)> {
        let inner = &self.inner;
        let retry = opts.retry.as_ref().unwrap_or(&inner.retry);
        retry.validate()?;
        let timeout = check_timeout(opts.timeout.unwrap_or(inner.timeout))?;
        let url = format!("{}{}", inner.base_url, path);
        let endpoint = format!("{method} {}", redact_url(&url));

        let mut headers = inner.default_headers.clone();
        headers.extend(opts.headers);
        headers.remove(RETRY_COUNT_HEADER);
        headers.extend(inner.protected.clone());
        if body.is_some() {
            headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
        }

        let started = Instant::now();
        let mut attempts: u32 = 0;
        loop {
            let mut h = headers.clone();
            if attempts > 0 {
                h.insert(
                    HeaderName::from_static(RETRY_COUNT_HEADER),
                    HeaderValue::from(attempts),
                );
                tracing::info!(%endpoint, retry = attempts, "retrying");
            }
            attempts += 1;
            let result = self
                .attempt(&method, &url, &endpoint, h, body.clone(), timeout)
                .await;
            match result {
                Ok((bytes, status, resp_headers)) => {
                    let meta = ResponseMeta {
                        status,
                        headers: resp_headers,
                        attempts,
                    };
                    return Ok((bytes, meta, endpoint));
                }
                Err(err) => {
                    if !retry.is_retryable(&err) {
                        return Err(err);
                    }
                    let delay = retry.delay(attempts, &err);
                    if retry.should_stop(attempts, started.elapsed(), delay) {
                        return Err(err);
                    }
                    if !delay.is_zero() {
                        tokio::time::sleep(delay).await;
                    }
                }
            }
        }
    }

    async fn attempt(
        &self,
        method: &Method,
        url: &str,
        endpoint: &str,
        headers: HeaderMap,
        body: Option<Vec<u8>>,
        timeout: Duration,
    ) -> Result<(Vec<u8>, u16, HeaderMap)> {
        let t0 = Instant::now();
        tracing::debug!(%endpoint, "->");
        if tracing::enabled!(tracing::Level::TRACE) {
            tracing::trace!(%endpoint, headers = ?redacted(&headers),
                body = %body.as_deref().map(String::from_utf8_lossy).unwrap_or_default(), "->");
        }
        let mut req = self
            .inner
            .http
            .request(method.clone(), url)
            .headers(headers)
            .timeout(timeout);
        if let Some(b) = body {
            req = req.body(b);
        }
        let map_err = |e: reqwest::Error| {
            tracing::debug!(%endpoint, error = %e, "<- transport error");
            if e.is_timeout() {
                Error::Timeout(timeout)
            } else {
                Error::Connection(Box::new(e))
            }
        };
        let resp = req.send().await.map_err(map_err)?;
        let status = resp.status();
        let resp_headers = resp.headers().clone();
        let bytes = resp.bytes().await.map_err(map_err)?;

        tracing::debug!(
            %endpoint,
            status = status.as_u16(),
            elapsed_ms = t0.elapsed().as_millis() as u64,
            request_id = resp_headers.get(REQUEST_ID_HEADER).and_then(|v| v.to_str().ok()).unwrap_or("-"),
            "<-"
        );
        if tracing::enabled!(tracing::Level::TRACE) {
            tracing::trace!(%endpoint, headers = ?redacted(&resp_headers),
                body = %String::from_utf8_lossy(&bytes), "<-");
        }

        if !status.is_success() {
            return Err(Error::Api(Box::new(ApiError::new(
                status.as_u16(),
                lenient_body(&bytes),
                resp_headers,
                Some(endpoint.to_owned()),
            ))));
        }
        Ok((bytes.to_vec(), status.as_u16(), resp_headers))
    }
}

fn validation_error(
    status: StatusCode,
    body: Option<Value>,
    headers: HeaderMap,
    endpoint: &str,
    f: DecodeFailure,
) -> Error {
    Error::ResponseValidation(Box::new(ResponseValidationError {
        status: status.as_u16(),
        field_path: f.path,
        detail: f.detail,
        body,
        headers,
        endpoint: Some(endpoint.to_owned()),
    }))
}

fn redact_url(url: &str) -> String {
    match reqwest::Url::parse(url) {
        Ok(mut u) => {
            let _ = u.set_username("");
            let _ = u.set_password(None);
            u.set_query(None);
            u.set_fragment(None);
            u.to_string()
        }
        Err(_) => url.to_owned(),
    }
}

fn redacted(headers: &HeaderMap) -> Vec<(String, String)> {
    headers
        .iter()
        .map(|(k, v)| {
            let value = if SECRET_HEADERS.contains(&k.as_str()) {
                "[REDACTED]".to_owned()
            } else {
                v.to_str().unwrap_or("<binary>").to_owned()
            };
            (k.as_str().to_owned(), value)
        })
        .collect()
}

#[derive(Debug, Default, Clone)]
struct CallOptions {
    retry: Option<RetryPolicy>,
    timeout: Option<Duration>,
    headers: HeaderMap,
}

macro_rules! call_option_methods {
    () => {
        /// Override the retry policy for this call.
        pub fn retry(mut self, policy: RetryPolicy) -> Self {
            self.opts.retry = Some(policy);
            self
        }

        /// Override the per-attempt timeout for this call.
        pub fn timeout(mut self, timeout: Duration) -> Self {
            self.opts.timeout = Some(timeout);
            self
        }

        /// Add a header for this call (protected headers still win).
        pub fn header(mut self, name: HeaderName, value: HeaderValue) -> Self {
            self.opts.headers.insert(name, value);
            self
        }
    };
}

/// A pending `POST /v1/systemone`. Configure it, then `.await` it (or call [`send`](Self::send)).
#[must_use = "requests do nothing until awaited"]
#[derive(Debug)]
pub struct SystemOneRequest {
    client: Client,
    state: std::result::Result<Value, String>,
    questions: Questions,
    model: Option<String>,
    extra_body: Map<String, Value>,
    opts: CallOptions,
}

impl SystemOneRequest {
    call_option_methods!();

    /// Override the model for this call.
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = Some(model.into());
        self
    }

    /// Add a top-level body field. A key named `state`, `model` or `questions` replaces the
    /// standard field. Useful for API fields this SDK version does not model yet.
    pub fn extra_body(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
        self.extra_body.insert(key.into(), value.into());
        self
    }

    /// Send the request.
    pub async fn send(self) -> Result<SystemOneResponse> {
        let state = self.state.map_err(Error::InvalidRequest)?;
        self.questions.validate()?;
        let model = self
            .model
            .unwrap_or_else(|| self.client.inner.model.clone());
        // Serialized as a struct (not via `serde_json::Value`) so question order reaches the wire
        // unchanged. `extra_body` keys replace the standard field of the same name.
        let extra = &self.extra_body;
        let body = SystemOneBody {
            state: (!extra.contains_key("state")).then_some(&state),
            model: (!extra.contains_key("model")).then_some(&model),
            questions: (!extra.contains_key("questions")).then_some(&self.questions),
            extra,
        };
        let bytes = serde_json::to_vec(&body).map_err(|e| {
            Error::InvalidRequest(format!(
                "The request body could not be encoded as JSON: {e}"
            ))
        })?;

        let (bytes, meta, endpoint) = self
            .client
            .execute(Method::POST, SYSTEM_ONE_PATH, Some(bytes), self.opts)
            .await?;
        match decode_system_one(&bytes) {
            Ok(DecodedSystemOne {
                model,
                usage,
                answers,
                raw,
            }) => Ok(SystemOneResponse {
                model,
                usage,
                answers,
                raw,
                meta,
            }),
            Err(f) => Err(validation_error(
                StatusCode::from_u16(meta.status).unwrap_or(StatusCode::OK),
                lenient_body(&bytes),
                meta.headers,
                &endpoint,
                f,
            )),
        }
    }
}

#[derive(Serialize)]
struct SystemOneBody<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    state: Option<&'a Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    model: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    questions: Option<&'a Questions>,
    #[serde(flatten)]
    extra: &'a Map<String, Value>,
}

impl IntoFuture for SystemOneRequest {
    type Output = Result<SystemOneResponse>;
    type IntoFuture = BoxFuture<'static, Self::Output>;

    fn into_future(self) -> Self::IntoFuture {
        Box::pin(self.send())
    }
}

/// The Models resource.
#[derive(Debug, Clone)]
pub struct Models {
    client: Client,
}

impl Models {
    /// `GET /v1/models`.
    pub fn list(&self) -> ListModelsRequest {
        ListModelsRequest {
            client: self.client.clone(),
            opts: CallOptions::default(),
        }
    }
}

/// A pending `GET /v1/models`.
#[must_use = "requests do nothing until awaited"]
#[derive(Debug)]
pub struct ListModelsRequest {
    client: Client,
    opts: CallOptions,
}

impl ListModelsRequest {
    call_option_methods!();

    /// Send the request.
    pub async fn send(self) -> Result<ListModelsResponse> {
        let (bytes, meta, endpoint) = self
            .client
            .execute(Method::GET, MODELS_PATH, None, self.opts)
            .await?;
        match decode_models(&bytes) {
            Ok((models, raw)) => Ok(ListModelsResponse { models, raw, meta }),
            Err(f) => Err(validation_error(
                StatusCode::from_u16(meta.status).unwrap_or(StatusCode::OK),
                lenient_body(&bytes),
                meta.headers,
                &endpoint,
                f,
            )),
        }
    }
}

impl IntoFuture for ListModelsRequest {
    type Output = Result<ListModelsResponse>;
    type IntoFuture = BoxFuture<'static, Self::Output>;

    fn into_future(self) -> Self::IntoFuture {
        Box::pin(self.send())
    }
}