Skip to main content

switchyard_protocol/
envelope.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The request/response envelope: the normalized [`LlmRequest`]/[`LlmResponse`] paired
5//! with the original provider payload and correlation [`Metadata`].
6
7use crate::{LlmRequest, LlmResponse, Metadata};
8
9/// A request an algorithm routes: the normalized [`LlmRequest`] plus optional
10/// host-owned raw data and correlation [`Metadata`].
11#[derive(Clone, Default)]
12pub struct Request {
13    /// The normalized request an algorithm routes.
14    pub llm_request: LlmRequest,
15    /// An optional whole request body retained by the host. libsy and the translation
16    /// codecs do not read it. Exact same-format codec replay instead uses
17    /// [`LlmRequest::preservation`].
18    pub raw_request: Option<serde_json::Value>,
19    /// Correlation metadata carried through the request.
20    pub metadata: Option<Metadata>,
21}
22
23impl Request {
24    /// Returns the model name supplied by the inbound request, when present.
25    ///
26    /// This is not necessarily the target selected by a routing decision.
27    pub fn requested_model(&self) -> Option<&str> {
28        self.llm_request.model.as_deref()
29    }
30}
31
32/// A response an algorithm returns: the [`LlmResponse`] (streamed or aggregate) plus
33/// optional correlation [`Metadata`].
34///
35/// Not `Clone` — `llm_response` may own a live stream.
36pub struct Response {
37    /// The neutral model response — a chunk stream or the buffered aggregate.
38    pub llm_response: LlmResponse,
39    /// Correlation metadata carried through the response.
40    pub metadata: Option<Metadata>,
41}
42
43impl Response {
44    /// Returns the model recorded by a buffered response.
45    ///
46    /// Returns `None` for a live stream because inspecting its `MessageStart`
47    /// event would require consuming the stream.
48    pub fn selected_model(&self) -> Option<&str> {
49        self.llm_response.selected_model()
50    }
51}