switchyard-translation 0.4.0

Pure Rust request, response, and stream translation for Switchyard
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Registry-backed translation engine for buffered and streaming translation.

use std::collections::BTreeMap;
use std::pin::Pin;
use std::sync::Arc;

use async_stream::try_stream;
use futures::io::AsyncBufReadExt;
use futures::{Stream, StreamExt, TryStreamExt};
use serde_json::Value;
use switchyard_protocol::LlmClientError;

use crate::codecs::FormatCodec;
use crate::codecs::anthropic::AnthropicMessagesCodec;
use crate::codecs::openai_chat::OpenAiChatCodec;
use crate::codecs::responses::OpenAiResponsesCodec;
use crate::codecs::stream::{
    StreamCodecRegistry, StreamTranslationState, encode_response_stream_event,
};
use crate::diagnostic::TranslationDiagnostic;
use crate::error::{Result, TranslationError};
use crate::format::FormatId;
use crate::llm::{AggLlmResponse, LlmRequest, ProviderExtensions};
use crate::policy::TranslationPolicy;
use crate::sse;
use crate::{LlmResponseChunk, LlmResponseStream, LlmResponseStreamEvent, LlmStreamError};

/// A stream of wire-format event objects in one format — the unframed body of an
/// SSE response. The serving layer frames each `Value` (e.g. as an SSE
/// `data:`/`event:` block).
pub type RawEventStream =
    Pin<Box<dyn Stream<Item = std::result::Result<Value, LlmStreamError>> + Send>>;

/// Encoded translation result with any diagnostics emitted along the way.
#[derive(Debug)]
pub struct TranslationOutput {
    pub body: Value,
    pub diagnostics: Vec<TranslationDiagnostic>,
}

/// Decoded request IR plus diagnostics.
#[derive(Debug)]
pub struct RequestIrOutput {
    pub request: LlmRequest,
    pub diagnostics: Vec<TranslationDiagnostic>,
}

/// Decoded response IR plus diagnostics.
#[derive(Debug)]
pub struct ResponseIrOutput {
    pub response: AggLlmResponse,
    pub diagnostics: Vec<TranslationDiagnostic>,
}

/// Registry mapping wire formats to buffered codecs.
#[derive(Default)]
pub struct FormatRegistry {
    codecs: BTreeMap<FormatId, Arc<dyn FormatCodec>>,
}

impl FormatRegistry {
    /// Creates an empty format registry.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a registry populated with the built-in provider codecs.
    pub fn with_builtins() -> Self {
        let mut registry = Self::new();
        registry.register(OpenAiChatCodec);
        registry.register(AnthropicMessagesCodec);
        registry.register(OpenAiResponsesCodec);
        registry
    }

    /// Registers or replaces a codec for its declared format.
    pub fn register(&mut self, codec: impl FormatCodec + 'static) {
        self.codecs.insert(codec.format(), Arc::new(codec));
    }

    /// Looks up a codec by format identifier.
    pub fn codec(&self, format: impl Into<FormatId>) -> Result<Arc<dyn FormatCodec>> {
        let format = format.into();
        self.codecs
            .get(&format)
            .cloned()
            .ok_or_else(|| TranslationError::Other(format!("no codec registered for {format}")))
    }
}

/// Stateless request/response translator that routes through the neutral IR.
pub struct TranslationEngine {
    registry: FormatRegistry,
    stream_registry: StreamCodecRegistry,
}

impl Default for TranslationEngine {
    fn default() -> Self {
        Self {
            registry: FormatRegistry::with_builtins(),
            stream_registry: StreamCodecRegistry::with_builtins(),
        }
    }
}

impl TranslationEngine {
    /// Creates an engine from an explicit buffered codec registry.
    pub fn new(registry: FormatRegistry) -> Self {
        Self {
            registry,
            stream_registry: StreamCodecRegistry::with_builtins(),
        }
    }

    /// Creates an engine from explicit buffered and streaming codec registries.
    pub fn with_registries(registry: FormatRegistry, stream_registry: StreamCodecRegistry) -> Self {
        Self {
            registry,
            stream_registry,
        }
    }

    /// Decodes a request body into the neutral request IR.
    pub fn decode_request(
        &self,
        source: impl Into<FormatId>,
        body: &Value,
        policy: &TranslationPolicy,
    ) -> Result<RequestIrOutput> {
        let source = source.into();
        let mut decoded = self.registry.codec(source)?.decode_request(body, policy)?;
        crate::normalize_compaction(&mut decoded.request, None);
        Ok(RequestIrOutput {
            request: decoded.request,
            diagnostics: decoded.diagnostics,
        })
    }

    /// Encodes a neutral request IR into a target wire format.
    ///
    /// Preserved target bodies take precedence over normalized fields, including
    /// `output.is_schema_enforced`. Clear the target's preservation entry or use
    /// [`crate::policy::PreservationPolicy::Disabled`] to encode IR edits.
    pub fn encode_request(
        &self,
        target: impl Into<FormatId>,
        request: &LlmRequest,
        policy: &TranslationPolicy,
    ) -> Result<TranslationOutput> {
        let target = target.into();
        let encoded = self
            .registry
            .codec(target)?
            .encode_request(request, policy)?;
        Ok(TranslationOutput {
            body: encoded.body,
            diagnostics: encoded.diagnostics,
        })
    }

    /// Translates a request body from source format to target format.
    pub fn translate_request(
        &self,
        source: impl Into<FormatId>,
        target: impl Into<FormatId>,
        body: &Value,
        policy: &TranslationPolicy,
    ) -> Result<TranslationOutput> {
        let source = source.into();
        let target = target.into();
        let decoded = self
            .registry
            .codec(source.clone())?
            .decode_request(body, policy)?;
        let encoded = self
            .registry
            .codec(target.clone())?
            .encode_request(&decoded.request, policy)?;
        Ok(TranslationOutput {
            body: encoded.body,
            diagnostics: with_formats(decoded.diagnostics, encoded.diagnostics, source, target),
        })
    }

    /// Decodes a response body into the neutral response IR.
    pub fn decode_response(
        &self,
        source: impl Into<FormatId>,
        body: &Value,
        policy: &TranslationPolicy,
    ) -> Result<ResponseIrOutput> {
        let source = source.into();
        let decoded = self.registry.codec(source)?.decode_response(body, policy)?;
        Ok(ResponseIrOutput {
            response: decoded.response,
            diagnostics: decoded.diagnostics,
        })
    }

    /// Encodes a neutral response IR into a target wire format.
    pub fn encode_response(
        &self,
        target: impl Into<FormatId>,
        response: &AggLlmResponse,
        policy: &TranslationPolicy,
    ) -> Result<TranslationOutput> {
        let target = target.into();
        let encoded = self
            .registry
            .codec(target)?
            .encode_response(response, policy)?;
        Ok(TranslationOutput {
            body: encoded.body,
            diagnostics: encoded.diagnostics,
        })
    }

    /// Encodes a neutral response IR while applying metadata retained from its request.
    ///
    /// Request extensions are used to restore caller-specific response details, such as
    /// Codex tool namespaces, without exposing those details to the upstream provider.
    pub fn encode_response_with_extensions(
        &self,
        target: impl Into<FormatId>,
        response: &AggLlmResponse,
        request_extensions: &ProviderExtensions,
        policy: &TranslationPolicy,
    ) -> Result<TranslationOutput> {
        let mut output = self.encode_response(target, response, policy)?;
        if !request_extensions.fields.is_empty() {
            crate::codex_namespaces::restore_qualified_tool_names(
                &mut output.body,
                &crate::codex_namespaces::qualified_tool_origins(request_extensions),
            );
            crate::codex_custom_tools::restore_custom_tool_calls(
                &mut output.body,
                &crate::codex_custom_tools::custom_tool_names(request_extensions),
            );
        }
        Ok(output)
    }

    /// Translates a response body from source format to target format.
    pub fn translate_response(
        &self,
        source: impl Into<FormatId>,
        target: impl Into<FormatId>,
        body: &Value,
        policy: &TranslationPolicy,
    ) -> Result<TranslationOutput> {
        self.translate_response_with_extensions(
            source,
            target,
            body,
            &ProviderExtensions::default(),
            policy,
        )
    }

    /// Translates a response while restoring details from the original request's extensions.
    ///
    /// Request extensions apply after encoding, including when the target body is preserved.
    pub fn translate_response_with_extensions(
        &self,
        source: impl Into<FormatId>,
        target: impl Into<FormatId>,
        body: &Value,
        request_extensions: &ProviderExtensions,
        policy: &TranslationPolicy,
    ) -> Result<TranslationOutput> {
        let source = source.into();
        let target = target.into();
        let decoded = self
            .registry
            .codec(source.clone())?
            .decode_response(body, policy)?;
        let encoded = self.encode_response_with_extensions(
            target.clone(),
            &decoded.response,
            request_extensions,
            policy,
        )?;
        Ok(TranslationOutput {
            body: encoded.body,
            diagnostics: with_formats(decoded.diagnostics, encoded.diagnostics, source, target),
        })
    }

    /// Translates one streaming source event into zero or more target events.
    pub fn translate_event(
        &self,
        state: &mut StreamTranslationState,
        source: impl Into<FormatId>,
        target: impl Into<FormatId>,
        event: &Value,
    ) -> Result<Vec<Value>> {
        let source = source.into();
        let target = target.into();
        let source_codec = self.stream_registry.codec(source.clone())?;
        let target_codec = self.stream_registry.codec(target.clone())?;
        if let Err(error) =
            crate::codecs::responses::validate_stream_output(&source, &target, event)
        {
            return Ok(target_codec.encode_event(
                state,
                crate::LlmResponseChunk::DecodeError {
                    message: error.to_string(),
                },
            ));
        }
        let canonical = source_codec.decode_event(state, event);
        state.source = Some(source);
        state.target = Some(target);
        Ok(canonical
            .into_iter()
            .flat_map(|event| target_codec.encode_event(state, event))
            .collect())
    }

    /// Decodes one provider event while retaining its parsed source JSON value.
    ///
    /// Takes ownership of `event` so preservation does not deep-copy provider JSON
    /// on the per-event streaming path.
    pub fn decode_stream_event(
        &self,
        state: &mut StreamTranslationState,
        source: impl Into<FormatId>,
        event: Value,
    ) -> Result<LlmResponseStreamEvent> {
        let source = source.into();
        let source_codec = self.stream_registry.codec(source.clone())?;
        state.source = Some(source.clone());
        let normalized = source_codec.decode_event(state, &event);
        Ok(LlmResponseStreamEvent::preserved(source, event, normalized))
    }

    /// Encodes one neutral or preserved stream event for a target provider.
    ///
    /// A preserved event replays its retained JSON value unchanged when its
    /// source and target formats match. Cross-format encoding intentionally
    /// uses only its normalized events.
    pub fn encode_stream_event(
        &self,
        state: &mut StreamTranslationState,
        target: impl Into<FormatId>,
        event: LlmResponseStreamEvent,
    ) -> Result<Vec<Value>> {
        let target = target.into();
        let target_codec = self.stream_registry.codec(target.clone())?;
        state.target = Some(target.clone());
        Ok(encode_response_stream_event(
            state,
            target_codec.as_ref(),
            &target,
            event,
        ))
    }

    /// Finishes target-provider stream emission after the source stream closes.
    pub fn finish_stream(
        &self,
        state: &mut StreamTranslationState,
        target: impl Into<FormatId>,
    ) -> Result<Vec<Value>> {
        let target = target.into();
        let target_codec = self.stream_registry.codec(target)?;
        Ok(target_codec.finish(state))
    }

    /// Encodes a stream of IR chunks into a stream of target-format wire events.
    ///
    /// `served_model` is exposed as the response model (via the stream state's
    /// `target_model`); `None` falls back to the id observed on the source stream.
    /// The target stream codec is resolved once and reused per chunk; terminal events
    /// (`message_stop` / `response.completed`) come from `finish`.
    pub fn encode_stream(
        &self,
        target: impl Into<FormatId>,
        chunks: LlmResponseStream,
        served_model: Option<String>,
    ) -> std::result::Result<RawEventStream, LlmClientError> {
        self.encode_stream_with_extensions(
            target,
            chunks,
            served_model,
            &ProviderExtensions::default(),
        )
    }

    /// Encodes a response stream, honouring request extensions.
    ///
    /// Identical to [`Self::encode_stream`] except that Codex tool namespaces recorded on
    /// the request are restored on each encoded event.
    pub fn encode_stream_with_extensions(
        &self,
        target: impl Into<FormatId>,
        chunks: LlmResponseStream,
        served_model: Option<String>,
        request_extensions: &ProviderExtensions,
    ) -> std::result::Result<RawEventStream, LlmClientError> {
        let origins = crate::codex_namespaces::qualified_tool_origins(request_extensions);
        let custom_tools = crate::codex_custom_tools::custom_tool_names(request_extensions);
        let mut custom_state = crate::codex_custom_tools::CustomToolCallStreamState::default();
        let target_format: FormatId = target.into();
        let codec = self
            .stream_registry
            .codec(target_format.clone())
            // Currently the only error is that the codec is missing, which is Configuration
            .map_err(|err| LlmClientError::Configuration {
                message: err.to_string(),
            })?;

        let served_model_for_events = served_model.clone();
        let mut state = StreamTranslationState {
            target: Some(target_format.clone()),
            target_model: served_model,
            ..Default::default()
        };
        let mut chunks = chunks;

        let events = try_stream! {
            while let Some(item) = chunks.next().await {
                let (mut encoded, client_error) = match item {
                    Ok(event) => (
                        encode_response_stream_event(&mut state, codec.as_ref(), &target_format, event),
                        None,
                    ),
                    Err(error) => {
                        // Let the codec flush pending content before propagating the
                        // original transport error, without emitting success events.
                        let encoded = codec.encode_event(&mut state, LlmResponseChunk::StreamError {
                            message: error.to_string(),
                        });
                        (encoded, Some(error))
                    }
                };
                for value in &mut encoded {
                    if let Some(model) = served_model_for_events.as_deref() {
                        codec.stamp_streamed_response_model(value, model);
                    }
                    crate::codex_namespaces::restore_qualified_tool_names(value, &origins);
                }
                // Argument deltas for a freeform tool cannot be expressed on the wire; the
                // rewritten completed item carries the input instead.
                let mut encoded: Vec<Value> = encoded
                    .into_iter()
                    .filter_map(|mut value| {
                        crate::codex_custom_tools::restore_custom_tool_calls_in_event(
                            &mut value,
                            &custom_tools,
                            &mut custom_state,
                        )
                        .then_some(value)
                    })
                    .collect();
                let terminal = encoded.pop();
                for value in encoded {
                    yield value;
                }
                // The codec's final error frame is replaced by the typed client error.
                if let Some(error) = client_error {
                    Err(LlmStreamError::Client(error))?;
                }
                match (state.errored, terminal) {
                    (false, Some(value)) => yield value,
                    (true, Some(value)) => Err(LlmStreamError::Upstream(value))?,
                    (true, None) => Err(LlmStreamError::Client(LlmClientError::ResponseTranslation(
                        "stream ended after an error without a terminal event".to_string(),
                    )))?,
                    _ => ()
                }
            }
            for mut value in codec.finish(&mut state) {
                if let Some(model) = served_model_for_events.as_deref() {
                    codec.stamp_streamed_response_model(&mut value, model);
                }
                crate::codex_namespaces::restore_qualified_tool_names(&mut value, &origins);
                if crate::codex_custom_tools::restore_custom_tool_calls_in_event(
                    &mut value,
                    &custom_tools,
                    &mut custom_state,
                ) {
                    yield value;
                }
            }
        };

        Ok(Box::pin(events))
    }

    /// Decodes provider SSE bytes into normalized stream events.
    ///
    /// Operates on raw bytes, not any HTTP client type: the caller adapts its
    /// transport's body stream into `Stream<Item = Result<Vec<u8>, _>>`. Frames are
    /// buffered across chunks (a partial frame waits for its boundary); the source
    /// stream codec is resolved once and reused for every frame.
    ///
    /// Codecs can opt in to stopping after the decoder yields a terminal JSON event.
    /// Other codecs keep reading to include trailing events before `[DONE]` or EOF.
    /// If EOF arrives without a required terminal event, the stream yields a deferred
    /// [`LlmClientError::ResponseTranslation`] error after any valid decoded events.
    pub fn decode_stream<S>(
        &self,
        source: impl Into<FormatId>,
        bytes: S,
    ) -> std::result::Result<LlmResponseStream, LlmClientError>
    where
        S: Stream<Item = std::result::Result<Vec<u8>, LlmClientError>> + Send + 'static,
    {
        let marker = sse::done_marker();
        let source_format: FormatId = source.into();
        let codec = self
            .stream_registry
            .codec(source_format.clone())
            .map_err(|error| LlmClientError::ResponseTranslation(error.to_string()))?;
        // Adapt the byte-chunk stream into an async line reader. The BufReader
        // reassembles data split across network chunks (including multi-byte UTF-8),
        // and `lines()` yields one SSE field line at a time. The stream is boxed to
        // an `io::Error` item so `into_async_read`'s error bound resolves cleanly. The
        // source error is boxed intact rather than stringified, so
        // `llm_client_error_from_io` can recover its original variant on the way out.
        let io_bytes: Pin<Box<dyn Stream<Item = std::io::Result<Vec<u8>>> + Send>> =
            Box::pin(bytes.map(|item| item.map_err(std::io::Error::other)));
        let lines = futures::io::BufReader::new(io_bytes.into_async_read()).lines();

        let mut state = StreamTranslationState {
            source: Some(source_format.clone()),
            ..StreamTranslationState::default()
        };
        let mut frame = String::new();
        let mut saw_terminal = false;
        let mut saw_error = false;
        let stream = Box::pin(try_stream! {
            futures::pin_mut!(lines);
            let mut first_line = true;
            while let Some(line) = lines.next().await {
                let line = line.map_err(llm_client_error_from_io)?;
                // SSE permits one optional BOM at the start of the stream.
                // https://html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream
                let line = if first_line {
                    first_line = false;
                    line.strip_prefix(sse::UTF8_BOM).unwrap_or(&line)
                } else {
                    &line
                };
                // A blank line (allowing a bare CR for CRLF streams) ends the frame.
                if line.trim_end().is_empty() {
                    let parsed = sse::parse_json_sse_frame(&frame, marker)
                        .map_err(|error| LlmClientError::ResponseTranslation(error.to_string()))?;
                    frame.clear();
                    match parsed {
                        sse::SseFrame::Empty => {}
                        sse::SseFrame::Done => {
                            saw_terminal |= codec.sse_done_is_terminal();
                            break;
                        }
                        sse::SseFrame::Data(value) => {
                            saw_terminal |= codec.is_terminal_event(&value);
                            let normalized = codec.decode_event(&mut state, &value);
                            saw_error |= normalized.iter().any(|chunk| matches!(
                                chunk,
                                LlmResponseChunk::DecodeError { .. }
                                    | LlmResponseChunk::StreamError { .. }
                            ));
                            yield LlmResponseStreamEvent::preserved(
                                source_format.clone(),
                                value,
                                normalized,
                            );
                            if saw_terminal && codec.stops_at_terminal_event() {
                                break;
                            }
                        }
                    }
                } else {
                    frame.push_str(line);
                    frame.push('\n');
                }
            }

            // A non-standard upstream might omit the final blank line; parse a trailing
            // complete frame instead of losing its last chunk.
            #[allow(clippy::collapsible_if)]
            if !frame.trim_end().is_empty() {
                let parsed = sse::parse_json_sse_frame(&frame, marker)
                    .map_err(|error| LlmClientError::ResponseTranslation(error.to_string()))?;
                match parsed {
                    sse::SseFrame::Done => {
                        saw_terminal |= codec.sse_done_is_terminal();
                    }
                    sse::SseFrame::Data(value) => {
                        saw_terminal |= codec.is_terminal_event(&value);
                        let normalized = codec.decode_event(&mut state, &value);
                        saw_error |= normalized.iter().any(|chunk| matches!(
                            chunk,
                            LlmResponseChunk::DecodeError { .. }
                                | LlmResponseChunk::StreamError { .. }
                        ));
                        yield LlmResponseStreamEvent::preserved(source_format.clone(), value, normalized);
                    }
                    sse::SseFrame::Empty => {}
                }
            }

            if !saw_terminal && !saw_error {
                Err(LlmClientError::ResponseTranslation(format!(
                    "{source_format} stream ended before a terminal event"
                )))?;
            }
        });
        Ok(stream)
    }
}

// Recover transport errors wrapped for `AsyncRead`; other reader failures are
// invalid upstream responses.
fn llm_client_error_from_io(error: std::io::Error) -> LlmClientError {
    let kind = error.kind();
    let message = error.to_string();
    match error.into_inner() {
        Some(source) => match source.downcast::<LlmClientError>() {
            Ok(error) => *error,
            Err(source) => LlmClientError::InvalidResponse { source },
        },
        None => LlmClientError::InvalidResponse {
            source: Box::new(std::io::Error::new(kind, message)),
        },
    }
}

// Attaches source and target formats to every diagnostic emitted across both passes.
fn with_formats(
    decoded: Vec<TranslationDiagnostic>,
    encoded: Vec<TranslationDiagnostic>,
    source: FormatId,
    target: FormatId,
) -> Vec<TranslationDiagnostic> {
    decoded
        .into_iter()
        .chain(encoded)
        .map(|diagnostic| diagnostic.with_formats(source.clone(), target.clone()))
        .collect()
}