turbospark-server 0.3.0

OpenAI Chat Completions and Anthropic Messages server on loopback: request/response envelopes, SSE streaming framing, and the axum router, wired to the raw-completion loop.
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
//! `POST /v1/audio/transcriptions`: WAV in, transcript out.
//!
//! Accepts OpenAI-style `multipart/form-data` (`file`, `model`, ...) or a raw
//! `audio/wav` body with the options in the query string. Only WAV decodes
//! (the audio crate has no other container), so anything else is a 415.
//! Options this stack cannot honour are refused with a 400 naming the field
//! rather than silently ignored.

use std::convert::Infallible;

use axum::body::Body;
use axum::extract::{FromRequest, Multipart, Query, Request, State};
use axum::http::{header, HeaderValue, StatusCode};
use axum::response::{IntoResponse, Response};
use axum::Json;
use tokio_stream::wrappers::ReceiverStream;
use turbospark_audio::conversion::{to_mono_resampled, MonoResampleStrategy};
use turbospark_audio::error::AudioError as WavError;
use turbospark_audio::resample::SincHannOptions;

use super::jobs::{JobFailure, JobOutput, SubmitError};
use super::{
    audio_error, audio_error_parts, error, timed, unavailable, AudioState, AudioTask,
    TranscribeRequest, Transcription, MAX_AUDIO_UPLOAD_BYTES, MAX_TRANSCRIBE_SECONDS,
};
use crate::ServerState;

/// Every STT runner consumes this rate.
pub(crate) const STT_SAMPLE_RATE: u32 = 16_000;

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum Format {
    Json,
    VerboseJson,
    Text,
    Srt,
    Vtt,
}

impl Format {
    pub(crate) fn parse(raw: &str) -> Option<Self> {
        Some(match raw {
            "json" => Self::Json,
            "verbose_json" => Self::VerboseJson,
            "text" => Self::Text,
            "srt" => Self::Srt,
            "vtt" => Self::Vtt,
            _ => return None,
        })
    }
}

pub(crate) struct Params {
    pub(crate) model: Option<String>,
    pub(crate) language: Option<String>,
    pub(crate) format: Format,
    pub(crate) stream: bool,
    pub(crate) asynchronous: bool,
}

impl Default for Params {
    fn default() -> Self {
        Self {
            model: None,
            language: None,
            format: Format::Json,
            stream: false,
            asynchronous: false,
        }
    }
}

fn parse_bool(name: &str, value: &str) -> Result<bool, Response> {
    match value.trim().to_ascii_lowercase().as_str() {
        "true" | "1" => Ok(true),
        "false" | "0" | "" => Ok(false),
        _ => Err(error(
            StatusCode::BAD_REQUEST,
            format!("{name} must be true or false"),
            Some(name),
        )),
    }
}

pub(crate) fn parse_language(value: &str) -> Result<Option<String>, Response> {
    let v = value.trim();
    if v.is_empty() || v.eq_ignore_ascii_case("auto") {
        return Ok(None);
    }
    if v.len() > 16
        || !v
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
    {
        return Err(error(
            StatusCode::BAD_REQUEST,
            "language must be a short tag such as \"en\" or \"auto\"",
            Some("language"),
        ));
    }
    Ok(Some(v.to_ascii_lowercase()))
}

impl Params {
    fn set(&mut self, name: &str, value: &str) -> Result<(), Response> {
        let unsupported = |what: &str| {
            error(
                StatusCode::BAD_REQUEST,
                format!("{what} is not supported by this server"),
                Some(name),
            )
        };
        match name {
            "model" => self.model = Some(value.trim().to_string()),
            "language" => self.language = parse_language(value)?,
            "response_format" => {
                self.format = Format::parse(value.trim()).ok_or_else(|| {
                    error(
                        StatusCode::BAD_REQUEST,
                        "response_format must be json, verbose_json, text, srt or vtt",
                        Some("response_format"),
                    )
                })?
            }
            "stream" => self.stream = parse_bool(name, value)?,
            "async" => self.asynchronous = parse_bool(name, value)?,
            // Runners take no prompt or sampling controls. An empty prompt and
            // temperature 0 are what clients send by default, so they pass.
            "prompt" if value.trim().is_empty() => {}
            "prompt" => return Err(unsupported("a transcription prompt")),
            "temperature" => match value.trim().parse::<f64>() {
                Ok(0.0) => {}
                _ => return Err(unsupported("a non-zero temperature")),
            },
            "timestamp_granularities[]" | "timestamp_granularities" => {
                if value.trim() != "segment" {
                    return Err(unsupported("word-level timestamps"));
                }
            }
            other => return Err(unsupported(&format!("the field {other:?}"))),
        }
        Ok(())
    }
}

fn multipart_error(e: axum::extract::multipart::MultipartError) -> Response {
    let status = e.status();
    let message = if status == StatusCode::PAYLOAD_TOO_LARGE {
        format!(
            "request body too large; audio uploads are limited to {} MiB",
            MAX_AUDIO_UPLOAD_BYTES / (1024 * 1024)
        )
    } else {
        e.body_text()
    };
    error(status, message, None)
}

/// The WAV bytes and options of a request, from either encoding.
async fn read_input(state: &ServerState, request: Request) -> Result<(Params, Vec<u8>), Response> {
    let content_type = request
        .headers()
        .get(header::CONTENT_TYPE)
        .and_then(|v| v.to_str().ok())
        .unwrap_or("")
        .to_ascii_lowercase();
    let mut params = Params::default();

    if content_type.starts_with("multipart/form-data") {
        let mut multipart = Multipart::from_request(request, state)
            .await
            .map_err(|e| error(StatusCode::BAD_REQUEST, e.body_text(), None))?;
        let mut file: Option<Vec<u8>> = None;
        while let Some(field) = multipart.next_field().await.map_err(multipart_error)? {
            let name = field.name().unwrap_or("").to_string();
            if name == "file" {
                if file.is_some() {
                    return Err(error(
                        StatusCode::BAD_REQUEST,
                        "send exactly one file part",
                        Some("file"),
                    ));
                }
                file = Some(field.bytes().await.map_err(multipart_error)?.to_vec());
            } else {
                let value = field.text().await.map_err(multipart_error)?;
                params.set(&name, &value)?;
            }
        }
        let file = file.ok_or_else(|| {
            error(
                StatusCode::BAD_REQUEST,
                "missing the file part",
                Some("file"),
            )
        })?;
        return Ok((params, file));
    }

    let raw_ok = content_type.is_empty()
        || [
            "audio/wav",
            "audio/x-wav",
            "audio/wave",
            "audio/vnd.wave",
            "application/octet-stream",
        ]
        .iter()
        .any(|t| content_type.starts_with(t));
    if !raw_ok {
        return Err(error(
            StatusCode::UNSUPPORTED_MEDIA_TYPE,
            "send multipart/form-data, or a raw audio/wav body; only WAV is supported",
            None,
        ));
    }
    let Query(pairs) = Query::<Vec<(String, String)>>::try_from_uri(request.uri())
        .map_err(|e| error(StatusCode::BAD_REQUEST, e.body_text(), None))?;
    for (name, value) in pairs {
        params.set(&name, &value)?;
    }
    let body = axum::body::to_bytes(request.into_body(), MAX_AUDIO_UPLOAD_BYTES)
        .await
        .map_err(|_| {
            error(
                StatusCode::PAYLOAD_TOO_LARGE,
                format!(
                    "request body too large; audio uploads are limited to {} MiB",
                    MAX_AUDIO_UPLOAD_BYTES / (1024 * 1024)
                ),
                None,
            )
        })?;
    Ok((params, body.to_vec()))
}

/// WAV bytes to the mono 16 kHz f32 every runner takes. CPU-bound, so callers
/// run it on the blocking pool.
pub(crate) fn decode_wav_16k(bytes: &[u8]) -> Result<Vec<f32>, (StatusCode, String)> {
    let wave = turbospark_audio::wav::read_wav_f32_bytes(bytes).map_err(|e| {
        let status = match e {
            WavError::NotWav { .. } | WavError::UnsupportedWavFormat { .. } => {
                StatusCode::UNSUPPORTED_MEDIA_TYPE
            }
            _ => StatusCode::BAD_REQUEST,
        };
        (status, format!("{e}; only WAV (PCM or float) is supported"))
    })?;
    if wave.frame_count() == 0 {
        return Err((
            StatusCode::BAD_REQUEST,
            "the audio contains no samples".into(),
        ));
    }
    if wave.duration_seconds() > MAX_TRANSCRIBE_SECONDS {
        return Err((
            StatusCode::BAD_REQUEST,
            format!(
                "audio is {:.0} s; the limit is {:.0} s",
                wave.duration_seconds(),
                MAX_TRANSCRIBE_SECONDS
            ),
        ));
    }
    // Sinc-Hann for the usual 44.1/48 kHz downsample: linear interpolation
    // aliases and costs recognition accuracy.
    to_mono_resampled(
        &wave,
        STT_SAMPLE_RATE,
        &MonoResampleStrategy::SincHann(SincHannOptions::default()),
    )
    .map_err(|e| (StatusCode::BAD_REQUEST, e.to_string()))
}

pub(crate) async fn decode_blocking(bytes: Vec<u8>) -> Result<Vec<f32>, Response> {
    tokio::task::spawn_blocking(move || decode_wav_16k(&bytes))
        .await
        .map_err(|_| {
            error(
                StatusCode::INTERNAL_SERVER_ERROR,
                "audio decode failed",
                None,
            )
        })?
        .map_err(|(status, message)| error(status, message, Some("file")))
}

fn timestamp(seconds: f64, comma: bool) -> String {
    let total_ms = (seconds.max(0.0) * 1000.0).round() as u64;
    let (h, m, s, ms) = (
        total_ms / 3_600_000,
        total_ms / 60_000 % 60,
        total_ms / 1000 % 60,
        total_ms % 1000,
    );
    let sep = if comma { ',' } else { '.' };
    format!("{h:02}:{m:02}:{s:02}{sep}{ms:03}")
}

pub(crate) fn render(t: &Transcription, format: Format, duration: f64) -> JobOutput {
    const TEXT: &str = "text/plain; charset=utf-8";
    match format {
        Format::Json => JobOutput {
            content_type: "application/json",
            body: serde_json::json!({"text": t.text}).to_string().into_bytes(),
        },
        Format::VerboseJson => {
            let segments: Vec<_> = t
                .segments
                .iter()
                .enumerate()
                .map(|(i, s)| {
                    serde_json::json!({
                        "id": i,
                        "start": s.start_seconds,
                        "end": s.end_seconds,
                        "text": s.text,
                    })
                })
                .collect();
            JobOutput {
                content_type: "application/json",
                body: serde_json::json!({
                    "task": "transcribe",
                    "language": t.language,
                    "duration": duration,
                    "text": t.text,
                    "segments": segments,
                })
                .to_string()
                .into_bytes(),
            }
        }
        Format::Text => JobOutput {
            content_type: TEXT,
            body: format!("{}\n", t.text).into_bytes(),
        },
        Format::Srt => {
            let mut out = String::new();
            for (i, s) in t.segments.iter().enumerate() {
                out.push_str(&format!(
                    "{}\n{} --> {}\n{}\n\n",
                    i + 1,
                    timestamp(s.start_seconds, true),
                    timestamp(s.end_seconds, true),
                    s.text.trim()
                ));
            }
            JobOutput {
                content_type: TEXT,
                body: out.into_bytes(),
            }
        }
        Format::Vtt => {
            let mut out = String::from("WEBVTT\n\n");
            for s in &t.segments {
                out.push_str(&format!(
                    "{} --> {}\n{}\n\n",
                    timestamp(s.start_seconds, false),
                    timestamp(s.end_seconds, false),
                    s.text.trim()
                ));
            }
            JobOutput {
                content_type: "text/vtt; charset=utf-8",
                body: out.into_bytes(),
            }
        }
    }
}

fn output_response(out: JobOutput) -> Response {
    let mut response = (StatusCode::OK, out.body).into_response();
    response.headers_mut().insert(
        header::CONTENT_TYPE,
        HeaderValue::from_static(out.content_type),
    );
    response
}

pub(crate) async fn translations_unsupported() -> Response {
    error(
        StatusCode::NOT_IMPLEMENTED,
        "audio translation is unsupported; use /v1/audio/transcriptions",
        None,
    )
}

pub(crate) async fn transcriptions(State(state): State<ServerState>, request: Request) -> Response {
    let Some(audio) = state.audio.clone() else {
        return unavailable();
    };
    let metrics_handle = audio.clone();
    timed(
        &metrics_handle,
        "transcriptions",
        run(audio, state, request),
    )
    .await
}

async fn run(audio: AudioState, state: ServerState, request: Request) -> Response {
    let (params, bytes) = match read_input(&state, request).await {
        Ok(v) => v,
        Err(r) => return r,
    };
    let Some(model) = params.model.clone().filter(|m| !m.is_empty()) else {
        return error(StatusCode::BAD_REQUEST, "model is required", Some("model"));
    };
    if params.stream && params.asynchronous {
        return error(
            StatusCode::BAD_REQUEST,
            "stream and async cannot be combined",
            Some("stream"),
        );
    }
    if let Err(r) = audio.model_for(&model, AudioTask::SpeechToText) {
        return r;
    }
    let samples = match decode_blocking(bytes).await {
        Ok(s) => s,
        Err(r) => return r,
    };
    let duration = samples.len() as f64 / f64::from(STT_SAMPLE_RATE);
    let request = TranscribeRequest {
        model: model.clone(),
        samples,
        language: params.language,
    };
    let format = params.format;
    let provider = audio.provider.clone();

    if params.asynchronous {
        let work = Box::pin(async move {
            match provider.transcribe(request).await {
                Ok(t) => Ok(render(&t, format, duration)),
                Err(e) => {
                    let (status, message) = audio_error_parts(&e);
                    Err(JobFailure { status, message })
                }
            }
        });
        return match audio.jobs.submit("transcription", model, work) {
            Ok(id) => (
                StatusCode::ACCEPTED,
                Json(serde_json::json!({
                    "job_id": id,
                    "id": id,
                    "object": "audio.job",
                    "status": "running",
                    "poll_url": format!("/v1/audio/jobs/{id}"),
                })),
            )
                .into_response(),
            Err(SubmitError::Full) => audio_error(super::AudioError::Busy),
        };
    }

    if params.stream {
        return ndjson(provider, request, duration);
    }

    match provider.transcribe(request).await {
        Ok(t) => output_response(render(&t, format, duration)),
        Err(e) => audio_error(e),
    }
}

/// Segment-granular NDJSON. The runner returns a whole transcript, so lines
/// for segments arrive together when it finishes; `start` goes out at once so
/// a proxy sees bytes during a long decode. A client that disconnects while
/// the request waits drops the provider future, which cancels queued work.
fn ndjson(
    provider: std::sync::Arc<dyn super::AudioProvider>,
    request: TranscribeRequest,
    duration: f64,
) -> Response {
    let (tx, rx) = tokio::sync::mpsc::channel::<Result<String, Infallible>>(16);
    let model = request.model.clone();
    tokio::spawn(async move {
        let line = |v: serde_json::Value| Ok::<_, Infallible>(format!("{v}\n"));
        if tx
            .send(line(serde_json::json!({"type": "start", "model": model})))
            .await
            .is_err()
        {
            return;
        }
        let result = tokio::select! {
            r = provider.transcribe(request) => r,
            _ = tx.closed() => return,
        };
        match result {
            Ok(t) => {
                for (i, s) in t.segments.iter().enumerate() {
                    let _ = tx
                        .send(line(serde_json::json!({
                            "type": "segment", "id": i,
                            "start": s.start_seconds, "end": s.end_seconds, "text": s.text,
                        })))
                        .await;
                }
                let _ = tx
                    .send(line(serde_json::json!({
                        "type": "complete", "text": t.text,
                        "language": t.language, "duration": duration,
                    })))
                    .await;
            }
            Err(e) => {
                let (status, message) = audio_error_parts(&e);
                let _ = tx
                    .send(line(serde_json::json!({
                        "type": "error",
                        "error": {"status": status.as_u16(), "message": message},
                    })))
                    .await;
            }
        }
    });
    let mut response = Response::new(Body::from_stream(ReceiverStream::new(rx)));
    response.headers_mut().insert(
        header::CONTENT_TYPE,
        HeaderValue::from_static("application/x-ndjson"),
    );
    response
        .headers_mut()
        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
    response
}