Skip to main content

switchyard_protocol/
llm.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Provider-neutral conversation types shared by routing, clients, and translation.
5
6use std::collections::BTreeMap;
7
8use serde::{Deserialize, Serialize};
9use serde_json::{Map, Value};
10
11use crate::format::FormatId;
12
13/// Actor role normalized across provider APIs.
14#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
15#[serde(rename_all = "snake_case")]
16pub enum Role {
17    /// System-level instructions.
18    System,
19    /// Developer-level instructions supported by some provider APIs.
20    Developer,
21    /// End-user input.
22    User,
23    /// Model-generated output.
24    Assistant,
25    /// Tool execution output.
26    Tool,
27}
28
29/// Instruction content separated from normal conversation messages.
30#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
31pub struct InstructionBlock {
32    /// Instruction authority, normally [`Role::System`] or [`Role::Developer`].
33    pub role: Role,
34    /// Ordered instruction content.
35    pub content: Vec<ContentBlock>,
36}
37
38/// One normalized conversation message.
39#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
40pub struct Message {
41    /// Actor that produced the message.
42    pub role: Role,
43    /// Ordered message content.
44    pub content: Vec<ContentBlock>,
45}
46
47impl Message {
48    /// Creates a text-only message for the given role.
49    pub fn text(role: Role, text: impl Into<String>) -> Self {
50        Self {
51            role,
52            content: vec![ContentBlock::Text { text: text.into() }],
53        }
54    }
55
56    /// Concatenates text-like content blocks when the message has any.
57    pub fn text_content(&self, separator: &str) -> Option<String> {
58        let parts = self
59            .content
60            .iter()
61            .filter_map(|block| match block {
62                ContentBlock::Text { text } => Some(text.as_str()),
63                ContentBlock::Refusal { text } => Some(text.as_str()),
64                _ => None,
65            })
66            .collect::<Vec<_>>();
67        if parts.is_empty() {
68            None
69        } else {
70            Some(parts.join(separator))
71        }
72    }
73}
74
75/// Normalized content block variants carried by messages and tool results.
76#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
77#[serde(tag = "type", rename_all = "snake_case")]
78pub enum ContentBlock {
79    /// Plain text content.
80    Text {
81        /// Text value.
82        text: String,
83    },
84    /// Model reasoning or thinking content.
85    Reasoning {
86        /// Reasoning text.
87        text: String,
88        /// Provider signature used to validate or continue the reasoning block.
89        signature: Option<String>,
90    },
91    /// Image content.
92    Image {
93        /// Image location or inline payload.
94        source: ImageSource,
95    },
96    /// Audio content.
97    Audio {
98        /// Audio location or inline payload.
99        source: MediaSource,
100    },
101    /// Video content.
102    Video {
103        /// Video location or inline payload.
104        source: MediaSource,
105    },
106    /// File content.
107    File {
108        /// File identifier or inline payload.
109        source: FileSource,
110    },
111    /// Tool invocation requested by the assistant.
112    ToolCall(ToolCall),
113    /// Result of an earlier tool invocation.
114    ToolResult(ToolResult),
115    /// Provider refusal content.
116    Refusal {
117        /// Human-readable refusal text.
118        text: String,
119    },
120    /// Provider block that has no normalized representation.
121    Unknown {
122        /// Wire format that supplied the block.
123        provider: FormatId,
124        /// Exact provider block.
125        raw: Value,
126    },
127}
128
129/// Image payload forms supported by the conversation model.
130#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
131#[serde(tag = "type", content = "data", rename_all = "snake_case")]
132pub enum ImageSource {
133    /// Image fetched from a URL.
134    Url {
135        /// Image URL.
136        url: String,
137        /// Optional provider-specific image detail level.
138        detail: Option<String>,
139    },
140    /// Base64-encoded image bytes.
141    Base64 {
142        /// MIME type, when supplied.
143        media_type: Option<String>,
144        /// Base64-encoded bytes.
145        data: String,
146    },
147    /// Provider image source with no normalized representation.
148    Raw(Value),
149}
150
151/// File payload forms supported by the conversation model.
152#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
153#[serde(tag = "type", content = "data", rename_all = "snake_case")]
154pub enum FileSource {
155    /// Provider-managed file identifier.
156    FileId(String),
157    /// Inline file payload.
158    FileData {
159        /// Encoded or textual file data as supplied by the provider format.
160        data: String,
161        /// Original filename, when supplied.
162        filename: Option<String>,
163    },
164    /// Provider file source with no normalized representation.
165    Raw(Value),
166}
167
168/// Audio and video payload forms supported by the conversation model.
169#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
170#[serde(tag = "type", content = "data", rename_all = "snake_case")]
171pub enum MediaSource {
172    /// Media fetched from a URL.
173    Url {
174        /// Media URL.
175        url: String,
176        /// MIME type, when supplied.
177        media_type: Option<String>,
178    },
179    /// Base64-encoded media bytes.
180    Base64 {
181        /// MIME type, when supplied.
182        media_type: Option<String>,
183        /// Base64-encoded bytes.
184        data: String,
185    },
186    /// Provider media source with no normalized representation.
187    Raw(Value),
188}
189
190/// Normalized assistant tool call.
191#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
192pub struct ToolCall {
193    /// Provider tool-call identifier used to pair the result.
194    pub id: String,
195    /// Tool name.
196    pub name: String,
197    /// Parsed tool arguments.
198    pub arguments: Value,
199}
200
201/// Normalized tool result message content.
202#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
203pub struct ToolResult {
204    /// Identifier of the [`ToolCall`] this result answers.
205    pub tool_call_id: String,
206    /// Ordered tool output content.
207    pub content: Vec<ContentBlock>,
208    /// Whether tool execution failed, when the provider reports it.
209    pub is_error: Option<bool>,
210}
211
212/// Normalized tool definition.
213#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
214pub struct ToolDefinition {
215    /// Tool name exposed to the model.
216    pub name: String,
217    /// Human-readable tool description.
218    pub description: Option<String>,
219    /// JSON Schema describing accepted arguments.
220    pub parameters: Value,
221    /// Whether the provider should enforce the schema strictly.
222    pub strict: Option<bool>,
223}
224
225/// Normalized tool choice policy.
226#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
227#[serde(tag = "type", content = "data", rename_all = "snake_case")]
228pub enum ToolChoice {
229    /// Let the model decide whether to call a tool.
230    Auto,
231    /// Require at least one tool call.
232    Required,
233    /// Disallow tool calls.
234    None,
235    /// Require a specific tool.
236    Tool {
237        /// Required tool name.
238        name: String,
239    },
240    /// Provider tool-choice value with no normalized representation.
241    Raw(Value),
242}
243
244/// Provider sampling parameters with common cross-provider names.
245#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
246pub struct SamplingParams {
247    /// Sampling temperature.
248    pub temperature: Option<f64>,
249    /// Nucleus-sampling probability mass.
250    pub top_p: Option<f64>,
251    /// Maximum number of candidate tokens considered at each step.
252    pub top_k: Option<i64>,
253}
254
255/// Output budget and structured-output options.
256#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
257pub struct OutputParams {
258    /// Maximum tokens the model may generate.
259    pub max_output_tokens: Option<u64>,
260    /// Provider-neutral or provider-specific structured-output configuration.
261    pub response_format: Option<Value>,
262}
263
264/// Provider reasoning controls preserved by translation.
265#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
266pub struct ReasoningParams {
267    /// Requested reasoning effort or level.
268    pub effort: Option<String>,
269    /// Provider reasoning controls without a normalized field.
270    pub raw: Option<Value>,
271}
272
273/// Provider-specific fields that do not have first-class conversation fields.
274#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
275#[serde(default)]
276pub struct ProviderExtensions {
277    /// Provider fields keyed by their original wire names.
278    ///
279    /// Codecs use these fields when translating to another format. Exact
280    /// same-format replay is controlled separately by [`PreservationMetadata`].
281    pub fields: Map<String, Value>,
282}
283
284/// Exact source payloads retained for lossless same-format round trips.
285///
286/// Translation's default preservation policy prefers a stored same-format body
287/// over reconstructing one from normalized fields. A caller that mutates the IR
288/// must clear the corresponding entry or use a policy with preservation disabled
289/// when those mutations must be encoded.
290#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
291#[serde(default)]
292pub struct PreservationMetadata {
293    /// Original request bodies keyed by source format.
294    pub requests: BTreeMap<FormatId, Value>,
295    /// Original response bodies keyed by source format.
296    pub responses: BTreeMap<FormatId, Value>,
297}
298
299/// Normalized request representation shared by Switchyard components.
300#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
301#[serde(default)]
302pub struct LlmRequest {
303    /// Model requested by the inbound client.
304    pub model: Option<String>,
305    /// System and developer instructions separated from conversation turns.
306    pub instructions: Vec<InstructionBlock>,
307    /// Ordered conversation messages.
308    pub messages: Vec<Message>,
309    /// Tools available to the model.
310    pub tools: Vec<ToolDefinition>,
311    /// Policy controlling model tool selection.
312    pub tool_choice: Option<ToolChoice>,
313    /// Common sampling controls.
314    pub sampling: SamplingParams,
315    /// Output budget and shape controls.
316    pub output: OutputParams,
317    /// Reasoning controls.
318    pub reasoning: ReasoningParams,
319    /// Whether the caller requested a streamed response.
320    pub stream: bool,
321    /// Provider fields without first-class normalized equivalents.
322    pub extensions: ProviderExtensions,
323    /// Exact provider bodies used by codecs for lossless same-format round trips.
324    /// This is separate from a host's optional
325    /// [`Request::raw_request`](crate::Request::raw_request).
326    pub preservation: PreservationMetadata,
327}
328
329/// Normalized token usage counts.
330#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
331pub struct Usage {
332    /// Non-cached input tokens. Provider codecs normalize aggregate OpenAI
333    /// input counts by subtracting the cache detail fields.
334    pub input_tokens: Option<u64>,
335    /// Cache-read and cache-creation token detail, when reported.
336    #[serde(flatten)]
337    pub cache: Option<Box<InputCacheUsage>>,
338    /// Generated output tokens, excluding reasoning detail when the provider reports it separately.
339    pub output_tokens: Option<u64>,
340    /// Provider-reported or codec-computed total token count.
341    ///
342    /// Codecs normalize OpenAI aggregate input counts before computing totals, so
343    /// this may equal non-cached input plus cache detail plus output.
344    pub total_tokens: Option<u64>,
345    /// Reasoning output tokens, when reported separately.
346    pub reasoning_tokens: Option<u64>,
347}
348
349/// Optional cache-token detail kept out of the common, cache-free usage allocation.
350#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
351pub struct InputCacheUsage {
352    /// Input tokens read from a provider cache.
353    pub cached_input_tokens: Option<u64>,
354    /// Input tokens written into a provider cache.
355    pub cache_creation_input_tokens: Option<u64>,
356}
357
358impl Usage {
359    /// Builds cache detail when at least one count is present.
360    pub fn cache_details(
361        cached_input_tokens: Option<u64>,
362        cache_creation_input_tokens: Option<u64>,
363    ) -> Option<Box<InputCacheUsage>> {
364        if cached_input_tokens.is_none() && cache_creation_input_tokens.is_none() {
365            return None;
366        }
367        Some(Box::new(InputCacheUsage {
368            cached_input_tokens,
369            cache_creation_input_tokens,
370        }))
371    }
372
373    /// Returns the cache-read input-token count.
374    pub fn cached_input_tokens(&self) -> Option<u64> {
375        self.cache
376            .as_ref()
377            .and_then(|cache| cache.cached_input_tokens)
378    }
379
380    /// Returns the cache-creation input-token count.
381    pub fn cache_creation_input_tokens(&self) -> Option<u64> {
382        self.cache
383            .as_ref()
384            .and_then(|cache| cache.cache_creation_input_tokens)
385    }
386
387    /// Sets the cache-read count, allocating cache detail when needed.
388    pub fn set_cached_input_tokens(&mut self, value: u64) {
389        self.cache
390            .get_or_insert_with(|| Box::new(InputCacheUsage::default()))
391            .cached_input_tokens = Some(value);
392    }
393
394    /// Sets the cache-creation count, allocating cache detail when needed.
395    pub fn set_cache_creation_input_tokens(&mut self, value: u64) {
396        self.cache
397            .get_or_insert_with(|| Box::new(InputCacheUsage::default()))
398            .cache_creation_input_tokens = Some(value);
399    }
400}
401
402/// Normalized reason a model stopped producing output.
403#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
404#[serde(rename_all = "snake_case")]
405pub enum StopReason {
406    /// Model completed its turn normally.
407    EndTurn,
408    /// Model reached the configured output limit.
409    MaxTokens,
410    /// Model stopped to request a tool call.
411    ToolUse,
412    /// Provider safety or content filtering stopped generation.
413    ContentFilter,
414    /// Generation terminated because of an error.
415    Error,
416    /// Provider stop reason with no normalized equivalent.
417    Unknown,
418}
419
420/// One assistant output item in a normalized response.
421#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
422pub struct ResponseOutput {
423    /// Actor that produced the output, normally [`Role::Assistant`].
424    pub role: Role,
425    /// Ordered output content.
426    pub content: Vec<ContentBlock>,
427    /// Why this output stopped, when known.
428    pub stop_reason: Option<StopReason>,
429}
430
431/// Normalized, fully-buffered response — the aggregate of a completed generation.
432/// This is the terminal form of a streamed [`LlmResponse`](crate::LlmResponse).
433#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
434#[serde(default)]
435pub struct AggLlmResponse {
436    /// Provider response identifier.
437    pub id: Option<String>,
438    /// Model reported by the provider.
439    pub model: Option<String>,
440    /// Ordered response output items.
441    pub outputs: Vec<ResponseOutput>,
442    /// Normalized token usage.
443    pub usage: Usage,
444    /// Provider response fields without normalized equivalents.
445    pub extensions: ProviderExtensions,
446    /// Exact provider bodies retained for lossless round trips.
447    pub preservation: PreservationMetadata,
448}
449
450impl AggLlmResponse {
451    /// Returns the first output item when a response has any output.
452    pub fn first_output(&self) -> Option<&ResponseOutput> {
453        self.outputs.first()
454    }
455}
456
457#[cfg(test)]
458mod tests {
459    use serde_json::json;
460
461    use super::*;
462
463    #[test]
464    fn serde_uses_python_friendly_dictionary_shapes() -> Result<(), serde_json::Error> {
465        let request: LlmRequest = serde_json::from_value(json!({
466            "model": "auto",
467            "messages": [{
468                "role": "user",
469                "content": [{"type": "text", "text": "hello"}]
470            }]
471        }))?;
472        assert_eq!(request.messages[0], Message::text(Role::User, "hello"));
473
474        let tool_call = ContentBlock::ToolCall(ToolCall {
475            id: "call-1".to_string(),
476            name: "lookup".to_string(),
477            arguments: json!({"query": "rust"}),
478        });
479        assert_eq!(
480            serde_json::to_value(tool_call)?,
481            json!({
482                "type": "tool_call",
483                "id": "call-1",
484                "name": "lookup",
485                "arguments": {"query": "rust"}
486            })
487        );
488        Ok(())
489    }
490}