Skip to main content

switchyard_translation/
request_edit.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Edits to normalized requests and provider request bodies.
5
6use std::collections::BTreeMap;
7
8use serde_json::{Map, Value, json};
9use switchyard_protocol::{ContentBlock, InstructionBlock, LlmRequest, Message, Role, WireFormat};
10
11/// Rewrites a caller-supplied reasoning effort using the selected target's map.
12///
13/// Apply this to the outgoing provider body after translation. Supported formats
14/// are `openai_chat`, `openai_responses`, `anthropic_messages`, and
15/// `gemini_generate_content`. Unknown formats, absent efforts, and efforts with
16/// no matching key leave the body unchanged. Keys are case-sensitive.
17pub fn remap_reasoning_effort(
18    body: &mut Value,
19    format: &str,
20    effort_map: &BTreeMap<String, String>,
21) {
22    let pointer = match format {
23        "openai_chat" => "/reasoning_effort",
24        "openai_responses" => "/reasoning/effort",
25        "anthropic_messages" => "/output_config/effort",
26        "gemini_generate_content" => "/generationConfig/thinkingConfig/thinkingLevel",
27        _ => return,
28    };
29    let Some(effort) = body.pointer_mut(pointer) else {
30        return;
31    };
32    if let Some(mapped) = effort.as_str().and_then(|effort| effort_map.get(effort)) {
33        *effort = Value::String(mapped.clone());
34    }
35}
36
37/// This function appends temporary guidance and updates preserved provider bodies.
38/// It removes bodies it cannot edit so encoding rebuilds them from the updated request.
39pub fn append_note(request: &mut LlmRequest, note: &str) {
40    // A separate message preserves the caller's final message boundary for OpenAI caching.
41    request.messages.push(Message::text(Role::User, note));
42    request.temporary_note_count += 1;
43    request.preservation.requests.retain(|format, body| {
44        let Some(body) = body.as_object_mut() else {
45            return false;
46        };
47        let (key, content_type) = match format.as_str() {
48            "openai_chat" | "anthropic_messages" => ("messages", "text"),
49            "openai_responses" => ("input", "input_text"),
50            _ => return false,
51        };
52        // Responses also accepts a string as a single user input.
53        if key == "input"
54            && let Some(Value::String(text)) = body.get_mut(key)
55        {
56            let original = std::mem::take(text);
57            body.insert(key.into(), json!([{"role":"user", "content":original}]));
58        }
59        if key == "input" && body.get(key).is_none_or(Value::is_null) {
60            body.insert(key.into(), Value::Array(Vec::new()));
61        }
62        let Some(messages) = body.get_mut(key).and_then(Value::as_array_mut) else {
63            return false;
64        };
65        if format.as_str() == WireFormat::AnthropicMessages.as_str()
66            && let Some(last) = messages.last_mut()
67            && last.get("role").and_then(Value::as_str) == Some("user")
68        {
69            // Anthropic guidance follows existing content, including any tool result.
70            match last.get_mut("content") {
71                Some(Value::String(text)) => {
72                    let original = std::mem::take(text);
73                    last["content"] = json!([
74                        {"type":"text", "text":original},
75                        {"type":"text", "text":note}
76                    ]);
77                }
78                Some(Value::Array(blocks)) => blocks.push(json!({"type":"text", "text":note})),
79                _ => return false,
80            }
81        } else {
82            messages.push(json!({"role":"user", "content":[{"type":content_type,"text":note}]}));
83        }
84        true
85    });
86}
87
88/// This function prepends a system prompt and updates supported preserved provider bodies.
89/// It removes bodies it cannot edit so encoding rebuilds them from the updated request.
90pub fn prepend_system_prompt(request: &mut LlmRequest, prompt: &str) {
91    request.instructions.insert(
92        0,
93        InstructionBlock {
94            role: Role::System,
95            content: vec![ContentBlock::Text {
96                text: prompt.to_string(),
97            }],
98        },
99    );
100    request.preservation.requests.retain(|format, body| {
101        let Some(body) = body.as_object_mut() else {
102            return false;
103        };
104        match format.as_str() {
105            "openai_chat" => prepend_openai_chat_system(body, prompt),
106            "openai_responses" => prepend_text_field(body, "instructions", prompt),
107            "anthropic_messages" => {
108                body.remove("switchyard_anthropic_request");
109                prepend_anthropic_system(body, prompt)
110            }
111            _ => false,
112        }
113    });
114}
115
116// This helper inserts a system message while keeping the existing Chat messages intact.
117fn prepend_openai_chat_system(body: &mut Map<String, Value>, prompt: &str) -> bool {
118    let message = json!({"role": "system", "content": prompt});
119    match body.get_mut("messages") {
120        None | Some(Value::Null) => {
121            body.insert("messages".to_string(), Value::Array(vec![message]));
122        }
123        Some(Value::Array(messages)) => messages.insert(0, message),
124        Some(_) => return false,
125    }
126    true
127}
128
129// Preserved Responses instructions and Anthropic system prompts can be strings, missing, or null.
130fn prepend_text_field(body: &mut Map<String, Value>, key: &str, prompt: &str) -> bool {
131    match body.get_mut(key) {
132        None | Some(Value::Null) => {
133            body.insert(key.to_string(), Value::String(prompt.to_string()));
134        }
135        Some(Value::String(text)) if text.is_empty() => {
136            *text = prompt.to_string();
137        }
138        Some(Value::String(text)) => text.insert_str(0, &format!("{prompt}\n\n")),
139        Some(_) => return false,
140    }
141    true
142}
143
144// This helper retains existing Anthropic system blocks when it prepends a prompt.
145fn prepend_anthropic_system(body: &mut Map<String, Value>, prompt: &str) -> bool {
146    if let Some(Value::Array(blocks)) = body.get_mut("system") {
147        blocks.insert(0, json!({"type":"text","text":prompt}));
148        true
149    } else {
150        prepend_text_field(body, "system", prompt)
151    }
152}