autoagents-core 0.3.7

Agent Framework for Building Autonomous Agents
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
use serde::{Serialize, de::DeserializeOwned};

/// Trait for agent output types that can generate structured output schemas
pub trait AgentOutputT: Serialize + DeserializeOwned + Send + Sync {
    /// Get the JSON schema string for this output type
    fn output_schema() -> &'static str;

    /// Get the structured output format as a JSON value
    fn structured_output_format() -> serde_json::Value;
}

/// Implementation of AgentOutputT for String type (when no output is specified)
impl AgentOutputT for String {
    fn output_schema() -> &'static str {
        "{}"
    }

    fn structured_output_format() -> serde_json::Value {
        serde_json::Value::Null
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::{Deserialize, Serialize};
    use serde_json::json;

    #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
    struct TestOutput {
        message: String,
        count: u32,
    }

    impl AgentOutputT for TestOutput {
        fn output_schema() -> &'static str {
            r#"{"type":"object","properties":{"message":{"type":"string"},"count":{"type":"integer"}},"required":["message","count"]}"#
        }

        fn structured_output_format() -> serde_json::Value {
            json!({
                "type": "object",
                "properties": {
                    "message": {"type": "string"},
                    "count": {"type": "integer"}
                },
                "required": ["message", "count"]
            })
        }
    }

    #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
    struct SimpleOutput {
        value: String,
    }

    impl AgentOutputT for SimpleOutput {
        fn output_schema() -> &'static str {
            r#"{"type":"object","properties":{"value":{"type":"string"}},"required":["value"]}"#
        }

        fn structured_output_format() -> serde_json::Value {
            json!({
                "type": "object",
                "properties": {
                    "value": {"type": "string"}
                },
                "required": ["value"]
            })
        }
    }

    #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
    struct ComplexOutput {
        name: String,
        age: u32,
        active: bool,
        tags: Vec<String>,
    }

    impl AgentOutputT for ComplexOutput {
        fn output_schema() -> &'static str {
            r#"{"type":"object","properties":{"name":{"type":"string"},"age":{"type":"integer"},"active":{"type":"boolean"},"tags":{"type":"array","items":{"type":"string"}}},"required":["name","age","active","tags"]}"#
        }

        fn structured_output_format() -> serde_json::Value {
            json!({
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "age": {"type": "integer"},
                    "active": {"type": "boolean"},
                    "tags": {
                        "type": "array",
                        "items": {"type": "string"}
                    }
                },
                "required": ["name", "age", "active", "tags"]
            })
        }
    }

    #[test]
    fn test_agent_output_trait_basic() {
        // Test that the trait is defined correctly
        fn assert_agent_output<T: AgentOutputT>() {}
        assert_agent_output::<String>();
        assert_agent_output::<TestOutput>();
    }

    #[test]
    fn test_string_output_schema() {
        let schema = String::output_schema();
        assert_eq!(schema, "{}");
    }

    #[test]
    fn test_string_structured_output_format() {
        let format = String::structured_output_format();
        assert_eq!(format, serde_json::Value::Null);
    }

    #[test]
    fn test_test_output_schema() {
        let schema = TestOutput::output_schema();
        assert!(schema.contains("object"));
        assert!(schema.contains("message"));
        assert!(schema.contains("count"));
        assert!(schema.contains("string"));
        assert!(schema.contains("integer"));
        assert!(schema.contains("required"));
    }

    #[test]
    fn test_test_output_structured_format() {
        let format = TestOutput::structured_output_format();
        assert_eq!(format["type"], "object");
        assert_eq!(format["properties"]["message"]["type"], "string");
        assert_eq!(format["properties"]["count"]["type"], "integer");
        assert_eq!(format["required"][0], "message");
        assert_eq!(format["required"][1], "count");
    }

    #[test]
    fn test_test_output_serialization() {
        let output = TestOutput {
            message: "Hello World".to_string(),
            count: 42,
        };
        let serialized = serde_json::to_string(&output).unwrap();
        assert!(serialized.contains("Hello World"));
        assert!(serialized.contains("42"));
    }

    #[test]
    fn test_test_output_deserialization() {
        let json = r#"{"message":"Test Message","count":123}"#;
        let output: TestOutput = serde_json::from_str(json).unwrap();
        assert_eq!(output.message, "Test Message");
        assert_eq!(output.count, 123);
    }

    #[test]
    fn test_test_output_clone() {
        let output = TestOutput {
            message: "Clone Test".to_string(),
            count: 999,
        };
        let cloned = output.clone();
        assert_eq!(output, cloned);
    }

    #[test]
    fn test_test_output_debug() {
        let output = TestOutput {
            message: "Debug Test".to_string(),
            count: 456,
        };
        let debug_str = format!("{output:?}");
        assert!(debug_str.contains("TestOutput"));
        assert!(debug_str.contains("Debug Test"));
        assert!(debug_str.contains("456"));
    }

    #[test]
    fn test_simple_output_schema() {
        let schema = SimpleOutput::output_schema();
        assert!(schema.contains("object"));
        assert!(schema.contains("value"));
        assert!(schema.contains("string"));
    }

    #[test]
    fn test_simple_output_structured_format() {
        let format = SimpleOutput::structured_output_format();
        assert_eq!(format["type"], "object");
        assert_eq!(format["properties"]["value"]["type"], "string");
        assert_eq!(format["required"][0], "value");
    }

    #[test]
    fn test_simple_output_serialization() {
        let output = SimpleOutput {
            value: "simple value".to_string(),
        };
        let serialized = serde_json::to_string(&output).unwrap();
        assert!(serialized.contains("simple value"));
    }

    #[test]
    fn test_simple_output_deserialization() {
        let json = r#"{"value":"deserialized value"}"#;
        let output: SimpleOutput = serde_json::from_str(json).unwrap();
        assert_eq!(output.value, "deserialized value");
    }

    #[test]
    fn test_complex_output_schema() {
        let schema = ComplexOutput::output_schema();
        assert!(schema.contains("object"));
        assert!(schema.contains("name"));
        assert!(schema.contains("age"));
        assert!(schema.contains("active"));
        assert!(schema.contains("tags"));
        assert!(schema.contains("array"));
        assert!(schema.contains("boolean"));
    }

    #[test]
    fn test_complex_output_structured_format() {
        let format = ComplexOutput::structured_output_format();
        assert_eq!(format["type"], "object");
        assert_eq!(format["properties"]["name"]["type"], "string");
        assert_eq!(format["properties"]["age"]["type"], "integer");
        assert_eq!(format["properties"]["active"]["type"], "boolean");
        assert_eq!(format["properties"]["tags"]["type"], "array");
        assert_eq!(format["properties"]["tags"]["items"]["type"], "string");
    }

    #[test]
    fn test_complex_output_serialization() {
        let output = ComplexOutput {
            name: "Test User".to_string(),
            age: 25,
            active: true,
            tags: vec!["tag1".to_string(), "tag2".to_string()],
        };
        let serialized = serde_json::to_string(&output).unwrap();
        assert!(serialized.contains("Test User"));
        assert!(serialized.contains("25"));
        assert!(serialized.contains("true"));
        assert!(serialized.contains("tag1"));
        assert!(serialized.contains("tag2"));
    }

    #[test]
    fn test_complex_output_deserialization() {
        let json = r#"{"name":"Complex User","age":30,"active":false,"tags":["work","personal"]}"#;
        let output: ComplexOutput = serde_json::from_str(json).unwrap();
        assert_eq!(output.name, "Complex User");
        assert_eq!(output.age, 30);
        assert!(!output.active);
        assert_eq!(output.tags, vec!["work", "personal"]);
    }

    #[test]
    fn test_complex_output_with_empty_tags() {
        let output = ComplexOutput {
            name: "Empty Tags".to_string(),
            age: 35,
            active: true,
            tags: vec![],
        };
        let serialized = serde_json::to_string(&output).unwrap();
        let deserialized: ComplexOutput = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized.tags.len(), 0);
    }

    #[test]
    fn test_output_with_special_characters() {
        let output = TestOutput {
            message: "Special chars: !@#$%^&*()".to_string(),
            count: 0,
        };
        let serialized = serde_json::to_string(&output).unwrap();
        let deserialized: TestOutput = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized.message, "Special chars: !@#$%^&*()");
    }

    #[test]
    fn test_output_with_unicode() {
        let output = TestOutput {
            message: "Unicode: 你好世界 🌍".to_string(),
            count: 42,
        };
        let serialized = serde_json::to_string(&output).unwrap();
        let deserialized: TestOutput = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized.message, "Unicode: 你好世界 🌍");
    }

    #[test]
    fn test_output_with_newlines() {
        let output = TestOutput {
            message: "Multi\nLine\nMessage".to_string(),
            count: 3,
        };
        let serialized = serde_json::to_string(&output).unwrap();
        let deserialized: TestOutput = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized.message, "Multi\nLine\nMessage");
    }

    #[test]
    fn test_output_with_large_count() {
        let output = TestOutput {
            message: "Large count".to_string(),
            count: u32::MAX,
        };
        let serialized = serde_json::to_string(&output).unwrap();
        let deserialized: TestOutput = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized.count, u32::MAX);
    }

    #[test]
    fn test_output_with_zero_count() {
        let output = TestOutput {
            message: "Zero count".to_string(),
            count: 0,
        };
        let serialized = serde_json::to_string(&output).unwrap();
        let deserialized: TestOutput = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized.count, 0);
    }

    #[test]
    fn test_output_with_empty_string() {
        let output = TestOutput {
            message: "".to_string(),
            count: 100,
        };
        let serialized = serde_json::to_string(&output).unwrap();
        let deserialized: TestOutput = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized.message, "");
        assert_eq!(deserialized.count, 100);
    }

    #[test]
    fn test_output_equality() {
        let output1 = TestOutput {
            message: "Equal test".to_string(),
            count: 50,
        };
        let output2 = TestOutput {
            message: "Equal test".to_string(),
            count: 50,
        };
        let output3 = TestOutput {
            message: "Different test".to_string(),
            count: 50,
        };
        assert_eq!(output1, output2);
        assert_ne!(output1, output3);
    }

    #[test]
    fn test_multiple_output_types() {
        let _test_output = TestOutput {
            message: "test".to_string(),
            count: 1,
        };
        let _simple_output = SimpleOutput {
            value: "simple".to_string(),
        };
        let _complex_output = ComplexOutput {
            name: "complex".to_string(),
            age: 25,
            active: true,
            tags: vec!["tag".to_string()],
        };

        // Test that all implement the trait
        assert_ne!(TestOutput::output_schema(), SimpleOutput::output_schema());
        assert_ne!(
            SimpleOutput::output_schema(),
            ComplexOutput::output_schema()
        );
        assert_ne!(TestOutput::output_schema(), ComplexOutput::output_schema());
    }

    #[test]
    fn test_schema_json_validity() {
        // Test that schemas are valid JSON
        let schemas = vec![
            String::output_schema(),
            TestOutput::output_schema(),
            SimpleOutput::output_schema(),
            ComplexOutput::output_schema(),
        ];

        for schema in schemas {
            if schema != "{}" {
                let _: serde_json::Value = serde_json::from_str(schema).unwrap();
            }
        }
    }

    #[test]
    fn test_structured_format_json_validity() {
        // Test that structured formats are valid JSON
        let formats = vec![
            String::structured_output_format(),
            TestOutput::structured_output_format(),
            SimpleOutput::structured_output_format(),
            ComplexOutput::structured_output_format(),
        ];

        for format in formats {
            if format != serde_json::Value::Null {
                assert!(format.is_object() || format.is_null());
            }
        }
    }

    #[test]
    fn test_send_sync_traits() {
        // Test that our types implement Send and Sync
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<TestOutput>();
        assert_send_sync::<SimpleOutput>();
        assert_send_sync::<ComplexOutput>();
    }

    #[test]
    fn test_round_trip_serialization() {
        let outputs = vec![
            TestOutput {
                message: "Round trip test".to_string(),
                count: 789,
            },
            TestOutput {
                message: "Another test".to_string(),
                count: 0,
            },
        ];

        for output in outputs {
            let serialized = serde_json::to_string(&output).unwrap();
            let deserialized: TestOutput = serde_json::from_str(&serialized).unwrap();
            assert_eq!(output, deserialized);
        }
    }
}