Skip to main content

supercode_runtime/
request.rs

1//! Provider-neutral request types for the native runtime.
2
3use serde::Serialize;
4use supercode_interchange::ChatMessage;
5
6/// A tool advertised to a model.
7#[derive(Debug, Clone, PartialEq, Serialize)]
8pub struct ToolSchema {
9    /// Tool name.
10    pub name: String,
11    /// Description the model uses to decide when to call it.
12    pub description: String,
13    /// JSON Schema for the tool's input object.
14    pub parameters: serde_json::Value,
15}
16
17impl ToolSchema {
18    /// Construct a tool schema.
19    pub fn new(
20        name: impl Into<String>,
21        description: impl Into<String>,
22        parameters: serde_json::Value,
23    ) -> Self {
24        Self {
25            name: name.into(),
26            description: description.into(),
27            parameters,
28        }
29    }
30}
31
32/// A single model-completion request.
33#[derive(Debug, Clone, PartialEq)]
34pub struct ChatRequest {
35    /// Model id.
36    pub model: String,
37    /// Full conversation so far.
38    pub messages: Vec<ChatMessage>,
39    /// Tools to advertise (may be empty).
40    pub tools: Vec<ToolSchema>,
41    /// Optional sampling temperature.
42    pub temperature: Option<f32>,
43    /// Optional output token cap.
44    pub max_tokens: Option<u32>,
45    /// Reasoning/effort level, such as `"low"` or `"high"`.
46    pub effort: Option<String>,
47    /// Structured-output constraint sent as `response_format`.
48    pub response_format: Option<serde_json::Value>,
49    /// Arbitrary provider-native request fields.
50    pub extra_body: serde_json::Map<String, serde_json::Value>,
51}
52
53impl ChatRequest {
54    /// Construct a minimal request with a model and conversation.
55    pub fn new(model: impl Into<String>, messages: Vec<ChatMessage>) -> Self {
56        Self {
57            model: model.into(),
58            messages,
59            tools: Vec::new(),
60            temperature: None,
61            max_tokens: None,
62            effort: None,
63            response_format: None,
64            extra_body: serde_json::Map::new(),
65        }
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn minimal_request_has_no_optional_runtime_controls() {
75        let request = ChatRequest::new("example/model", vec![ChatMessage::user("hello")]);
76        assert_eq!(request.model, "example/model");
77        assert_eq!(request.messages.len(), 1);
78        assert!(request.tools.is_empty());
79        assert_eq!(request.temperature, None);
80        assert_eq!(request.max_tokens, None);
81        assert!(request.extra_body.is_empty());
82    }
83
84    #[test]
85    fn schema_constructor_preserves_provider_json() {
86        let parameters = serde_json::json!({"type": "object"});
87        let schema = ToolSchema::new("read", "Read a file", parameters.clone());
88        assert_eq!(schema.name, "read");
89        assert_eq!(schema.parameters, parameters);
90    }
91}