nanocodex-oai-api 0.3.0

Tower-native OpenAI Responses API and managed context for Nanocodex
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
//! Registry-publishable WebSocket connection policy.
//!
//! The proxy and Happy Eyeballs behavior is adapted from the MIT-licensed
//! `openai-oss-forks/{tokio-tungstenite,tungstenite-rs}` revisions previously
//! pinned by this workspace. Keeping it here preserves that behavior while the
//! public crate depends only on crates.io packages.

use std::{collections::VecDeque, env, future::Future, io, net::SocketAddr, time::Duration};

use base64::{Engine, engine::general_purpose::STANDARD};
use futures_util::{StreamExt, stream::FuturesUnordered};
use http::Uri;
use tokio::{
    io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
    net::TcpStream,
    time::{Instant, sleep_until},
};
use tokio_tungstenite::{
    MaybeTlsStream, WebSocketStream, client_async_tls_with_config,
    tungstenite::{
        Error, Result,
        handshake::client::{Request, Response},
    },
};

const HAPPY_EYEBALLS_DELAY: Duration = Duration::from_millis(250);
const MAX_CONNECT_RESPONSE_SIZE: usize = 8_192;

pub(crate) async fn connect_async(
    request: Request,
) -> Result<(WebSocketStream<MaybeTlsStream<TcpStream>>, Response)> {
    let host = request
        .uri()
        .host()
        .ok_or_else(|| invalid_input("WebSocket URL has no host"))?
        .to_owned();
    let secure = match request.uri().scheme_str() {
        Some("wss") => true,
        Some("ws") => false,
        _ => return Err(invalid_input("unsupported WebSocket URL scheme")),
    };
    let port = request
        .uri()
        .port_u16()
        .unwrap_or(if secure { 443 } else { 80 });

    let socket = if let Some(proxy) = ProxyConfig::from_env(request.uri(), secure)? {
        let socket = connect_happy_eyeballs(proxy.authority()).await?;
        connect_via_proxy(socket, &proxy, &host, port).await?
    } else {
        connect_happy_eyeballs(format!("{host}:{port}")).await?
    };

    client_async_tls_with_config(request, socket, None, None).await
}

#[derive(Clone, Copy)]
enum ProxyScheme {
    Http,
    Socks5,
    Socks5h,
}

struct ProxyAuth {
    username: String,
    password: String,
}

struct ProxyConfig {
    scheme: ProxyScheme,
    host: String,
    port: u16,
    auth: Option<ProxyAuth>,
}

impl ProxyConfig {
    fn from_env(uri: &Uri, secure: bool) -> Result<Option<Self>> {
        let host = uri
            .host()
            .ok_or_else(|| invalid_input("WebSocket URL has no host"))?;
        let port = uri.port_u16().unwrap_or(if secure { 443 } else { 80 });
        if should_bypass_proxy(host, port) {
            return Ok(None);
        }

        let value = if secure {
            get_env_first(&["HTTPS_PROXY", "https_proxy"])
                .or_else(|| get_env_first(&["HTTP_PROXY", "http_proxy"]))
        } else {
            get_env_first(&["HTTP_PROXY", "http_proxy"])
        }
        .or_else(|| get_env_first(&["ALL_PROXY", "all_proxy"]));

        value.map(|value| Self::parse(&value)).transpose()
    }

    fn parse(value: &str) -> Result<Self> {
        let value = value.trim();
        let uri = value
            .parse::<Uri>()
            .map_err(|_| invalid_input(format!("invalid proxy URL {value:?}")))?;
        let scheme = match uri.scheme_str() {
            Some("http") => ProxyScheme::Http,
            Some("socks5") => ProxyScheme::Socks5,
            Some("socks5h") => ProxyScheme::Socks5h,
            _ => return Err(invalid_input("unsupported proxy URL scheme")),
        };
        let authority = uri
            .authority()
            .ok_or_else(|| invalid_input("proxy URL has no authority"))?
            .as_str();
        let (userinfo, host_port) = authority
            .rsplit_once('@')
            .map_or((None, authority), |(userinfo, host_port)| {
                (Some(userinfo), host_port)
            });
        let host_uri = format!("http://{host_port}")
            .parse::<Uri>()
            .map_err(|_| invalid_input("proxy URL has an invalid host"))?;
        let host = host_uri
            .host()
            .ok_or_else(|| invalid_input("proxy URL has no host"))?
            .to_owned();
        let port = host_uri.port_u16().unwrap_or(match scheme {
            ProxyScheme::Http => 80,
            ProxyScheme::Socks5 | ProxyScheme::Socks5h => 1080,
        });
        let auth = userinfo.map(parse_userinfo).transpose()?;
        Ok(Self {
            scheme,
            host,
            port,
            auth,
        })
    }

    fn authority(&self) -> String {
        format!("{}:{}", self.host, self.port)
    }
}

fn get_env_first(keys: &[&str]) -> Option<String> {
    keys.iter()
        .find_map(|key| env::var(key).ok())
        .filter(|value| !value.is_empty())
}

fn should_bypass_proxy(host: &str, port: u16) -> bool {
    let Some(no_proxy) = get_env_first(&["NO_PROXY", "no_proxy"]) else {
        return false;
    };
    let no_proxy = no_proxy.trim();
    if no_proxy == "*" {
        return true;
    }
    if no_proxy.is_empty() {
        return false;
    }

    let host = normalize_host(host);
    no_proxy.split(',').any(|token| {
        let token = token.trim();
        if token.is_empty() {
            return false;
        }
        let (token_host, token_port) = split_host_port(token);
        if token_port.is_some_and(|token_port| token_port != port) {
            return false;
        }
        let token_host = normalize_host(token_host);
        let token_host = token_host.strip_prefix('.').unwrap_or(token_host);
        host == token_host || host.ends_with(&format!(".{token_host}"))
    })
}

fn split_host_port(token: &str) -> (&str, Option<u16>) {
    if token.starts_with('[') {
        if let Some(close) = token.find(']') {
            let host = &token[..=close];
            let port = token[close + 1..]
                .strip_prefix(':')
                .and_then(|port| port.parse().ok());
            return (host, port);
        }
        return (token, None);
    }
    if token.matches(':').count() == 1 {
        let (host, port) = token.rsplit_once(':').unwrap_or((token, ""));
        return (host, port.parse().ok());
    }
    (token, None)
}

fn normalize_host(host: &str) -> &str {
    host.strip_prefix('[')
        .and_then(|host| host.strip_suffix(']'))
        .unwrap_or(host)
}

fn parse_userinfo(userinfo: &str) -> Result<ProxyAuth> {
    let (username, password) = userinfo.split_once(':').unwrap_or((userinfo, ""));
    Ok(ProxyAuth {
        username: percent_decode(username)?,
        password: percent_decode(password)?,
    })
}

fn percent_decode(value: &str) -> Result<String> {
    let mut output = Vec::with_capacity(value.len());
    let mut bytes = value.bytes();
    while let Some(byte) = bytes.next() {
        if byte == b'%' {
            let high = bytes
                .next()
                .ok_or_else(|| invalid_input("invalid proxy credential encoding"))?;
            let low = bytes
                .next()
                .ok_or_else(|| invalid_input("invalid proxy credential encoding"))?;
            output.push((from_hex(high)? << 4) | from_hex(low)?);
        } else {
            output.push(byte);
        }
    }
    String::from_utf8(output).map_err(|_| invalid_input("proxy credentials are not UTF-8"))
}

fn from_hex(byte: u8) -> Result<u8> {
    match byte {
        b'0'..=b'9' => Ok(byte - b'0'),
        b'a'..=b'f' => Ok(byte - b'a' + 10),
        b'A'..=b'F' => Ok(byte - b'A' + 10),
        _ => Err(invalid_input("invalid proxy credential encoding")),
    }
}

async fn connect_via_proxy<S>(
    mut stream: S,
    proxy: &ProxyConfig,
    host: &str,
    port: u16,
) -> Result<S>
where
    S: AsyncRead + AsyncWrite + Unpin,
{
    match proxy.scheme {
        ProxyScheme::Http => http_connect(&mut stream, host, port, proxy.auth.as_ref()).await?,
        ProxyScheme::Socks5 | ProxyScheme::Socks5h => {
            socks5_connect(&mut stream, host, port, proxy.auth.as_ref()).await?;
        }
    }
    Ok(stream)
}

async fn http_connect<S>(
    stream: &mut S,
    host: &str,
    port: u16,
    auth: Option<&ProxyAuth>,
) -> Result<()>
where
    S: AsyncRead + AsyncWrite + Unpin,
{
    let authority = format!("{host}:{port}");
    let mut request = format!(
        "CONNECT {authority} HTTP/1.1\r\nHost: {authority}\r\nProxy-Connection: Keep-Alive\r\n"
    );
    if let Some(auth) = auth {
        let token = STANDARD.encode(format!("{}:{}", auth.username, auth.password));
        request.push_str("Proxy-Authorization: Basic ");
        request.push_str(&token);
        request.push_str("\r\n");
    }
    request.push_str("\r\n");
    stream
        .write_all(request.as_bytes())
        .await
        .map_err(Error::Io)?;
    stream.flush().await.map_err(Error::Io)?;

    let response = read_connect_response(stream).await?;
    let response = std::str::from_utf8(&response)
        .map_err(|_| invalid_input("HTTP CONNECT response is not UTF-8"))?;
    let status = response
        .lines()
        .next()
        .and_then(|line| line.split_whitespace().nth(1))
        .and_then(|status| status.parse::<u16>().ok())
        .ok_or_else(|| invalid_input("HTTP CONNECT response has no valid status"))?;
    if !(200..300).contains(&status) {
        return Err(invalid_input(format!(
            "HTTP CONNECT failed with status {status}"
        )));
    }
    Ok(())
}

async fn read_connect_response<S>(stream: &mut S) -> Result<Vec<u8>>
where
    S: AsyncRead + Unpin,
{
    let mut response = Vec::new();
    let mut chunk = [0; 512];
    loop {
        if response.len() >= MAX_CONNECT_RESPONSE_SIZE {
            return Err(invalid_input("HTTP CONNECT response is too large"));
        }
        let read = stream.read(&mut chunk).await.map_err(Error::Io)?;
        if read == 0 {
            break;
        }
        response.extend_from_slice(&chunk[..read]);
        if response.windows(4).any(|window| window == b"\r\n\r\n") {
            break;
        }
    }
    Ok(response)
}

async fn socks5_connect<S>(
    stream: &mut S,
    host: &str,
    port: u16,
    auth: Option<&ProxyAuth>,
) -> Result<()>
where
    S: AsyncRead + AsyncWrite + Unpin,
{
    let methods: &[u8] = if auth.is_some() {
        &[0x05, 0x02, 0x00, 0x02]
    } else {
        &[0x05, 0x01, 0x00]
    };
    stream.write_all(methods).await.map_err(Error::Io)?;
    stream.flush().await.map_err(Error::Io)?;

    let mut choice = [0; 2];
    stream.read_exact(&mut choice).await.map_err(Error::Io)?;
    if choice[0] != 0x05 {
        return Err(invalid_input("SOCKS5 proxy returned an invalid version"));
    }
    match choice[1] {
        0x00 => {}
        0x02 => socks5_authenticate(stream, auth).await?,
        0xff => return Err(invalid_input("SOCKS5 proxy rejected authentication")),
        _ => return Err(invalid_input("SOCKS5 proxy selected an unsupported method")),
    }

    let host = host.as_bytes();
    if host.len() > usize::from(u8::MAX) {
        return Err(invalid_input("SOCKS5 destination host is too long"));
    }
    let host_length = u8::try_from(host.len())
        .map_err(|_| invalid_input("SOCKS5 destination host is too long"))?;
    let mut request = Vec::with_capacity(host.len() + 7);
    request.extend_from_slice(&[0x05, 0x01, 0x00, 0x03, host_length]);
    request.extend_from_slice(host);
    request.extend_from_slice(&port.to_be_bytes());
    stream.write_all(&request).await.map_err(Error::Io)?;
    stream.flush().await.map_err(Error::Io)?;

    let mut header = [0; 4];
    stream.read_exact(&mut header).await.map_err(Error::Io)?;
    if header[0] != 0x05 || header[1] != 0x00 {
        return Err(invalid_input(format!(
            "SOCKS5 connection failed with code {}",
            header[1]
        )));
    }
    let address_len = match header[3] {
        0x01 => 4,
        0x03 => {
            let mut length = [0];
            stream.read_exact(&mut length).await.map_err(Error::Io)?;
            usize::from(length[0])
        }
        0x04 => 16,
        _ => {
            return Err(invalid_input(
                "SOCKS5 proxy returned an invalid address type",
            ));
        }
    };
    let mut remainder = vec![0; address_len + 2];
    stream.read_exact(&mut remainder).await.map_err(Error::Io)?;
    Ok(())
}

async fn socks5_authenticate<S>(stream: &mut S, auth: Option<&ProxyAuth>) -> Result<()>
where
    S: AsyncRead + AsyncWrite + Unpin,
{
    let auth = auth.ok_or_else(|| invalid_input("SOCKS5 proxy requested credentials"))?;
    let username = auth.username.as_bytes();
    let password = auth.password.as_bytes();
    if username.len() > usize::from(u8::MAX) || password.len() > usize::from(u8::MAX) {
        return Err(invalid_input("SOCKS5 proxy credentials are too long"));
    }
    let username_length = u8::try_from(username.len())
        .map_err(|_| invalid_input("SOCKS5 proxy username is too long"))?;
    let password_length = u8::try_from(password.len())
        .map_err(|_| invalid_input("SOCKS5 proxy password is too long"))?;
    let mut request = Vec::with_capacity(username.len() + password.len() + 3);
    request.extend_from_slice(&[0x01, username_length]);
    request.extend_from_slice(username);
    request.push(password_length);
    request.extend_from_slice(password);
    stream.write_all(&request).await.map_err(Error::Io)?;
    stream.flush().await.map_err(Error::Io)?;

    let mut response = [0; 2];
    stream.read_exact(&mut response).await.map_err(Error::Io)?;
    if response != [0x01, 0x00] {
        return Err(invalid_input("SOCKS5 authentication failed"));
    }
    Ok(())
}

async fn connect_happy_eyeballs(address: impl tokio::net::ToSocketAddrs) -> Result<TcpStream> {
    let addresses = tokio::net::lookup_host(address)
        .await
        .map_err(Error::Io)?
        .collect();
    happy_eyeballs_connect(addresses, TcpStream::connect)
        .await
        .map_err(Error::Io)
}

async fn happy_eyeballs_connect<T, F, Fut>(
    addresses: Vec<SocketAddr>,
    mut connect: F,
) -> io::Result<T>
where
    F: FnMut(SocketAddr) -> Fut,
    Fut: Future<Output = io::Result<T>>,
{
    let mut addresses = interleave_addresses(addresses);
    let Some(first) = addresses.pop_front() else {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "host did not resolve to any address",
        ));
    };
    let mut attempts = FuturesUnordered::new();
    attempts.push(connect(first));
    let mut next_attempt = Instant::now() + HAPPY_EYEBALLS_DELAY;
    let mut last_error = None;

    loop {
        if addresses.is_empty() {
            match attempts.next().await {
                Some(Ok(stream)) => return Ok(stream),
                Some(Err(error)) => {
                    last_error = Some(error);
                    if attempts.is_empty() {
                        break;
                    }
                }
                None => break,
            }
            continue;
        }

        tokio::select! {
            result = attempts.next() => {
                if let Some(Ok(stream)) = result {
                    return Ok(stream);
                }
                if let Some(Err(error)) = result {
                    last_error = Some(error);
                }
            }
            () = sleep_until(next_attempt) => {}
        }
        if let Some(address) = addresses.pop_front() {
            attempts.push(connect(address));
            next_attempt = Instant::now() + HAPPY_EYEBALLS_DELAY;
        }
    }

    Err(last_error.unwrap_or_else(|| {
        io::Error::new(
            io::ErrorKind::ConnectionRefused,
            "all connection attempts failed",
        )
    }))
}

fn interleave_addresses(addresses: Vec<SocketAddr>) -> VecDeque<SocketAddr> {
    let mut addresses = addresses.into_iter();
    let Some(first) = addresses.next() else {
        return VecDeque::new();
    };
    let first_is_ipv4 = first.is_ipv4();
    let mut preferred = VecDeque::from([first]);
    let mut alternate = VecDeque::new();
    for address in addresses {
        if address.is_ipv4() == first_is_ipv4 {
            preferred.push_back(address);
        } else {
            alternate.push_back(address);
        }
    }

    let mut interleaved = VecDeque::new();
    while !preferred.is_empty() || !alternate.is_empty() {
        if let Some(address) = preferred.pop_front() {
            interleaved.push_back(address);
        }
        if let Some(address) = alternate.pop_front() {
            interleaved.push_back(address);
        }
    }
    interleaved
}

fn invalid_input(message: impl Into<String>) -> Error {
    Error::Io(io::Error::new(io::ErrorKind::InvalidInput, message.into()))
}

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

    #[test]
    fn interleaves_address_families() {
        let v6_a = "[::1]:1".parse().unwrap();
        let v6_b = "[::1]:2".parse().unwrap();
        let v4_a = "127.0.0.1:1".parse().unwrap();
        let v4_b = "127.0.0.1:2".parse().unwrap();
        assert_eq!(
            interleave_addresses(vec![v6_a, v6_b, v4_a, v4_b])
                .into_iter()
                .collect::<Vec<_>>(),
            vec![v6_a, v4_a, v6_b, v4_b]
        );
    }

    #[test]
    fn parses_proxy_authentication() {
        let proxy = ProxyConfig::parse("http://user:p%40ss@proxy.local:3128").unwrap();
        assert_eq!(proxy.host, "proxy.local");
        assert_eq!(proxy.port, 3128);
        let auth = proxy.auth.unwrap();
        assert_eq!(auth.username, "user");
        assert_eq!(auth.password, "p@ss");
    }
}