cognis-core 0.2.0

Core traits and types for the Cognis LLM framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

use super::base::ToolSchema;

/// JSON Schema primitive types.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum JsonSchemaType {
    String,
    Number,
    Integer,
    Boolean,
    Array,
    Object,
    Null,
}

impl JsonSchemaType {
    /// Returns the JSON Schema type string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::String => "string",
            Self::Number => "number",
            Self::Integer => "integer",
            Self::Boolean => "boolean",
            Self::Array => "array",
            Self::Object => "object",
            Self::Null => "null",
        }
    }
}

/// Describes a single property in a JSON Schema object.
#[derive(Debug, Clone)]
pub struct PropertySchema {
    /// Property name.
    pub name: String,
    /// Human-readable description of the property.
    pub description: Option<String>,
    /// The JSON Schema type of this property.
    pub schema_type: JsonSchemaType,
    /// Whether this property is required.
    pub required: bool,
    /// Allowed enum values (for string enums).
    pub enum_values: Option<Vec<String>>,
    /// Schema for array items (only relevant when `schema_type` is `Array`).
    pub items: Option<Box<PropertySchema>>,
    /// Default value for this property.
    pub default_value: Option<Value>,
}

impl PropertySchema {
    /// Create a new property schema.
    pub fn new(name: impl Into<String>, schema_type: JsonSchemaType, required: bool) -> Self {
        Self {
            name: name.into(),
            description: None,
            schema_type,
            required,
            enum_values: None,
            items: None,
            default_value: None,
        }
    }

    /// Set the description.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Set the enum values.
    pub fn with_enum_values(mut self, values: Vec<String>) -> Self {
        self.enum_values = Some(values);
        self
    }

    /// Set the items schema (for array types).
    pub fn with_items(mut self, items: PropertySchema) -> Self {
        self.items = Some(Box::new(items));
        self
    }

    /// Set the default value.
    pub fn with_default(mut self, value: Value) -> Self {
        self.default_value = Some(value);
        self
    }

    /// Convert this property to its JSON Schema representation (without the name wrapper).
    fn to_schema_value(&self) -> Value {
        let mut schema = json!({
            "type": self.schema_type.as_str()
        });

        if let Some(ref desc) = self.description {
            schema["description"] = Value::String(desc.clone());
        }

        if let Some(ref values) = self.enum_values {
            schema["enum"] = json!(values);
        }

        if let Some(ref items) = self.items {
            schema["items"] = items.to_schema_value();
        }

        if let Some(ref default) = self.default_value {
            schema["default"] = default.clone();
        }

        schema
    }
}

/// Builder for constructing a `ToolSchema` with a well-formed JSON Schema
/// `parameters` object.
#[derive(Debug, Clone)]
pub struct ToolSchemaBuilder {
    name: String,
    description: String,
    properties: Vec<PropertySchema>,
}

impl ToolSchemaBuilder {
    /// Create a new builder.
    pub fn new(name: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            properties: Vec::new(),
        }
    }

    /// Add an arbitrary property.
    pub fn add_property(mut self, prop: PropertySchema) -> Self {
        self.properties.push(prop);
        self
    }

    /// Add a string property.
    pub fn add_string_property(
        self,
        name: impl Into<String>,
        description: impl Into<String>,
        required: bool,
    ) -> Self {
        let prop = PropertySchema::new(name, JsonSchemaType::String, required)
            .with_description(description);
        self.add_property(prop)
    }

    /// Add a number property.
    pub fn add_number_property(
        self,
        name: impl Into<String>,
        description: impl Into<String>,
        required: bool,
    ) -> Self {
        let prop = PropertySchema::new(name, JsonSchemaType::Number, required)
            .with_description(description);
        self.add_property(prop)
    }

    /// Add an integer property.
    pub fn add_integer_property(
        self,
        name: impl Into<String>,
        description: impl Into<String>,
        required: bool,
    ) -> Self {
        let prop = PropertySchema::new(name, JsonSchemaType::Integer, required)
            .with_description(description);
        self.add_property(prop)
    }

    /// Add a boolean property.
    pub fn add_boolean_property(
        self,
        name: impl Into<String>,
        description: impl Into<String>,
        required: bool,
    ) -> Self {
        let prop = PropertySchema::new(name, JsonSchemaType::Boolean, required)
            .with_description(description);
        self.add_property(prop)
    }

    /// Add an array property with a specified items type.
    pub fn add_array_property(
        self,
        name: impl Into<String>,
        description: impl Into<String>,
        items_type: JsonSchemaType,
        required: bool,
    ) -> Self {
        let items = PropertySchema::new("items", items_type, false);
        let prop = PropertySchema::new(name, JsonSchemaType::Array, required)
            .with_description(description)
            .with_items(items);
        self.add_property(prop)
    }

    /// Add a string enum property.
    pub fn add_enum_property(
        self,
        name: impl Into<String>,
        description: impl Into<String>,
        values: Vec<String>,
        required: bool,
    ) -> Self {
        let prop = PropertySchema::new(name, JsonSchemaType::String, required)
            .with_description(description)
            .with_enum_values(values);
        self.add_property(prop)
    }

    /// Build the final `ToolSchema`.
    pub fn build(self) -> ToolSchema {
        let parameters = json_schema_from_properties(&self.properties);
        ToolSchema {
            name: self.name,
            description: self.description,
            parameters: Some(parameters),
            extras: None,
        }
    }
}

/// Convert a slice of `PropertySchema` into a JSON Schema object value
/// with `"type": "object"`, `"properties"`, and `"required"` fields.
pub fn json_schema_from_properties(properties: &[PropertySchema]) -> Value {
    let mut props = serde_json::Map::new();
    let mut required: Vec<Value> = Vec::new();

    for prop in properties {
        props.insert(prop.name.clone(), prop.to_schema_value());
        if prop.required {
            required.push(Value::String(prop.name.clone()));
        }
    }

    let mut schema = json!({
        "type": "object",
        "properties": Value::Object(props),
    });

    if !required.is_empty() {
        schema["required"] = Value::Array(required);
    }

    schema
}

/// Convenience function for quickly defining a `ToolSchema` from a list of
/// parameter tuples.
///
/// Each tuple is `(name, description, type, required)`.
///
/// # Example
///
/// ```ignore
/// use cognis_core::tools::schema_gen::{quick_tool_schema, JsonSchemaType};
///
/// let schema = quick_tool_schema(
///     "search",
///     "Search for documents",
///     &[
///         ("query", "The search query", JsonSchemaType::String, true),
///         ("limit", "Max results to return", JsonSchemaType::Integer, false),
///     ],
/// );
/// ```
pub fn quick_tool_schema(
    name: impl Into<String>,
    description: impl Into<String>,
    params: &[(&str, &str, JsonSchemaType, bool)],
) -> ToolSchema {
    let properties: Vec<PropertySchema> = params
        .iter()
        .map(|(name, desc, schema_type, required)| {
            PropertySchema::new(*name, schema_type.clone(), *required).with_description(*desc)
        })
        .collect();

    let parameters = json_schema_from_properties(&properties);

    ToolSchema {
        name: name.into(),
        description: description.into(),
        parameters: Some(parameters),
        extras: None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_build_schema_with_string_property() {
        let schema = ToolSchemaBuilder::new("test", "A test tool")
            .add_string_property("name", "The user name", true)
            .build();

        assert_eq!(schema.name, "test");
        let params = schema.parameters.unwrap();
        let props = &params["properties"];
        assert_eq!(props["name"]["type"], "string");
        assert_eq!(props["name"]["description"], "The user name");
    }

    #[test]
    fn test_build_schema_with_multiple_property_types() {
        let schema = ToolSchemaBuilder::new("multi", "Multi-type tool")
            .add_string_property("name", "A name", true)
            .add_number_property("score", "A score", true)
            .add_integer_property("count", "A count", false)
            .add_boolean_property("active", "Is active", false)
            .build();

        let params = schema.parameters.unwrap();
        let props = &params["properties"];
        assert_eq!(props["name"]["type"], "string");
        assert_eq!(props["score"]["type"], "number");
        assert_eq!(props["count"]["type"], "integer");
        assert_eq!(props["active"]["type"], "boolean");
    }

    #[test]
    fn test_required_vs_optional_properties() {
        let schema = ToolSchemaBuilder::new("test", "Test")
            .add_string_property("required_field", "Required", true)
            .add_string_property("optional_field", "Optional", false)
            .build();

        let params = schema.parameters.unwrap();
        let required = params["required"].as_array().unwrap();
        assert_eq!(required.len(), 1);
        assert_eq!(required[0], "required_field");
        // The optional field should still be in properties
        assert!(params["properties"]["optional_field"].is_object());
    }

    #[test]
    fn test_array_property_with_items() {
        let schema = ToolSchemaBuilder::new("test", "Test")
            .add_array_property("tags", "List of tags", JsonSchemaType::String, true)
            .build();

        let params = schema.parameters.unwrap();
        let tags = &params["properties"]["tags"];
        assert_eq!(tags["type"], "array");
        assert_eq!(tags["items"]["type"], "string");
    }

    #[test]
    fn test_enum_property() {
        let schema = ToolSchemaBuilder::new("test", "Test")
            .add_enum_property(
                "color",
                "Pick a color",
                vec!["red".into(), "green".into(), "blue".into()],
                true,
            )
            .build();

        let params = schema.parameters.unwrap();
        let color = &params["properties"]["color"];
        assert_eq!(color["type"], "string");
        let enum_vals = color["enum"].as_array().unwrap();
        assert_eq!(enum_vals.len(), 3);
        assert_eq!(enum_vals[0], "red");
        assert_eq!(enum_vals[1], "green");
        assert_eq!(enum_vals[2], "blue");
    }

    #[test]
    fn test_build_generates_valid_json_schema_structure() {
        let schema = ToolSchemaBuilder::new("search", "Search tool")
            .add_string_property("query", "Search query", true)
            .add_integer_property("limit", "Max results", false)
            .build();

        let params = schema.parameters.unwrap();
        // Must have "type": "object" at top level
        assert_eq!(params["type"], "object");
        // Must have "properties" object
        assert!(params["properties"].is_object());
        // Required array should contain only required fields
        let required = params["required"].as_array().unwrap();
        assert!(required.contains(&json!("query")));
        assert!(!required.contains(&json!("limit")));
    }

    #[test]
    fn test_quick_tool_schema() {
        let schema = quick_tool_schema(
            "search",
            "Search for documents",
            &[
                ("query", "The search query", JsonSchemaType::String, true),
                ("limit", "Max results", JsonSchemaType::Integer, false),
            ],
        );

        assert_eq!(schema.name, "search");
        assert_eq!(schema.description, "Search for documents");
        let params = schema.parameters.unwrap();
        assert_eq!(params["properties"]["query"]["type"], "string");
        assert_eq!(params["properties"]["limit"]["type"], "integer");
        let required = params["required"].as_array().unwrap();
        assert_eq!(required.len(), 1);
        assert_eq!(required[0], "query");
    }

    #[test]
    fn test_generated_schema_has_object_type_wrapper() {
        let schema = ToolSchemaBuilder::new("tool", "A tool")
            .add_string_property("input", "The input", true)
            .build();

        let params = schema.parameters.unwrap();
        assert_eq!(params["type"], "object");
        assert!(params["properties"].is_object());
    }

    #[test]
    fn test_property_with_default_value() {
        let prop = PropertySchema::new("limit", JsonSchemaType::Integer, false)
            .with_description("Max results")
            .with_default(json!(10));

        let schema = ToolSchemaBuilder::new("test", "Test")
            .add_property(prop)
            .build();

        let params = schema.parameters.unwrap();
        assert_eq!(params["properties"]["limit"]["default"], 10);
    }

    #[test]
    fn test_no_required_array_when_all_optional() {
        let schema = ToolSchemaBuilder::new("test", "Test")
            .add_string_property("a", "Optional a", false)
            .add_string_property("b", "Optional b", false)
            .build();

        let params = schema.parameters.unwrap();
        assert_eq!(params["type"], "object");
        // "required" key should not be present
        assert!(params.get("required").is_none());
    }
}