Skip to main content

switchyard_protocol/
lib.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4#![warn(missing_docs)]
5#![doc = include_str!("../README.md")]
6
7pub mod category;
8pub mod client;
9pub mod codex_namespaces;
10pub mod decision;
11pub mod envelope;
12pub mod format;
13pub mod infra;
14pub mod llm;
15pub mod metadata;
16pub mod model_id;
17pub mod stream;
18
19pub use category::*;
20pub use client::*;
21pub use decision::*;
22pub use envelope::*;
23pub use format::*;
24pub use infra::*;
25pub use llm::*;
26pub use metadata::*;
27pub use model_id::*;
28pub use stream::*;
29
30/// Builds a single-turn request: one user message carrying `prompt`, for `model`.
31///
32/// This deliberately creates only the common text shape. Construct [`LlmRequest`]
33/// directly for instructions, tools, multimodal content, or sampling controls.
34pub fn text_request(model: Option<String>, prompt: impl Into<String>) -> LlmRequest {
35    LlmRequest {
36        model,
37        messages: vec![Message::text(Role::User, prompt)],
38        ..LlmRequest::default()
39    }
40}
41
42/// Builds a single-turn response: one assistant message carrying `completion`, for `model`.
43///
44/// Construct [`AggLlmResponse`] directly when usage, tools, reasoning, or multiple
45/// output items must be represented.
46pub fn text_response(model: Option<String>, completion: impl Into<String>) -> AggLlmResponse {
47    AggLlmResponse {
48        model,
49        outputs: vec![ResponseOutput {
50            url_citations: Vec::new(),
51            role: Role::Assistant,
52            content: vec![ContentBlock::Text {
53                text: completion.into(),
54            }],
55            stop_reason: None,
56        }],
57        ..AggLlmResponse::default()
58    }
59}
60
61/// Returns a lossy text view of the first assistant output.
62///
63/// Only text blocks from the first output are concatenated. Refusals, reasoning,
64/// tools, media, and additional outputs are omitted. Returns an empty string when
65/// no such text exists.
66pub fn completion_text(response: &AggLlmResponse) -> String {
67    response
68        .outputs
69        .first()
70        .map(|output| {
71            output
72                .content
73                .iter()
74                .filter_map(|block| match block {
75                    ContentBlock::Text { text } => Some(text.as_str()),
76                    _ => None,
77                })
78                .collect::<String>()
79        })
80        .unwrap_or_default()
81}
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86
87    #[test]
88    fn text_request_builds_one_user_message() {
89        let req = text_request(Some("m".to_string()), "hello world");
90        assert_eq!(req.model.as_deref(), Some("m"));
91        assert_eq!(req.messages.len(), 1);
92        assert_eq!(req.messages[0].role, Role::User);
93        assert_eq!(
94            req.messages[0].text_content("\n").as_deref(),
95            Some("hello world")
96        );
97    }
98
99    #[test]
100    fn response_round_trips_completion_text() {
101        let resp = text_response(None, "the answer");
102        assert_eq!(completion_text(&resp), "the answer");
103    }
104
105    #[test]
106    fn empty_completion_text_is_empty_string() {
107        assert_eq!(completion_text(&AggLlmResponse::default()), "");
108    }
109}