stigmergy 0.1.0

stigmergy provides emergent agent behavior
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! # Component Definition System
//!
//! This module implements the component definition system for stigmergy, providing
//! schema-based validation for component data structures. Component definitions
//! establish the structure and validation rules for component types.
//!
//! ## Key Features
//!
//! - **Schema Validation**: JSON Schema-based validation for component data
//! - **Type Safety**: Component types must follow Rust naming conventions
//! - **Flexible Schemas**: Support for complex schemas including oneOf unions and enums
//! - **HTTP API**: Complete REST API for component definition management
//! - **JSON and YAML Support**: Accept both formats based on Content-Type header

use std::collections::HashMap;

use axum::Router;
use axum::async_trait;
use axum::body::Bytes;
use axum::extract::{FromRequest, Path, Query, Request, State};
use axum::http::StatusCode;
use axum::response::Json;
use axum::routing::get;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{Component, ValidationError, validate_value};

/// A component definition that associates a component type with its JSON schema.
///
/// Component definitions establish the structure and validation rules for component data.
/// Each definition consists of a component type identifier and a JSON schema that
/// describes what data is valid for that component type.
///
/// # Examples
///
/// ```rust
/// use stigmergy::{Component, ComponentDefinition};
/// use serde_json::json;
///
/// // Create a simple component definition
/// let health_component = Component::new("Health").unwrap();
/// let health_schema = json!({
///     "type": "object",
///     "properties": {
///         "hp": { "type": "integer", "minimum": 0 },
///         "max_hp": { "type": "integer", "minimum": 1 }
///     },
///     "required": ["hp", "max_hp"]
/// });
///
/// let definition = ComponentDefinition::new(health_component, health_schema);
///
/// // Validate the schema structure
/// assert!(definition.validate_schema().is_ok());
///
/// // Validate component data against the schema
/// let valid_data = json!({"hp": 100, "max_hp": 100});
/// assert!(definition.validate_component_data(&valid_data).is_ok());
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ComponentDefinition {
    /// The component type this definition applies to
    pub component: Component,
    /// The JSON schema that validates component data
    pub schema: serde_json::Value,
}

impl ComponentDefinition {
    /// Creates a new component definition.
    ///
    /// # Arguments
    /// * `component` - The component type identifier
    /// * `schema` - The JSON schema for validating component data
    ///
    /// # Examples
    /// ```rust
    /// # use stigmergy::{Component, ComponentDefinition};
    /// # use serde_json::json;
    /// let component = Component::new("Position").unwrap();
    /// let schema = json!({"type": "object", "properties": {"x": {"type": "number"}}});
    /// let definition = ComponentDefinition::new(component, schema);
    /// ```
    pub fn new(component: Component, schema: Value) -> Self {
        Self { component, schema }
    }

    /// Validates that the schema structure is well-formed.
    ///
    /// This method checks that the JSON schema follows the expected format and
    /// contains valid schema constructs. It validates the schema structure
    /// recursively to ensure all nested schemas are also valid.
    ///
    /// # Returns
    /// * `Ok(())` - If the schema is valid
    /// * `Err(ValidationError)` - If the schema structure is invalid
    ///
    /// # Examples
    /// ```rust
    /// # use stigmergy::{Component, ComponentDefinition};
    /// # use serde_json::json;
    /// let component = Component::new("Test").unwrap();
    ///
    /// // Valid schema
    /// let valid_schema = json!({"type": "string"});
    /// let definition = ComponentDefinition::new(component.clone(), valid_schema);
    /// assert!(definition.validate_schema().is_ok());
    ///
    /// // Invalid schema (unknown type)
    /// let invalid_schema = json!({"type": "invalid_type"});
    /// let definition = ComponentDefinition::new(component, invalid_schema);
    /// assert!(definition.validate_schema().is_err());
    /// ```
    pub fn validate_schema(&self) -> Result<(), ValidationError> {
        validate_schema_structure(&self.schema)
    }

    /// Validates component data against this definition's schema.
    ///
    /// This method checks that the provided data conforms to the JSON schema
    /// defined for this component type. It performs comprehensive validation
    /// including type checking, required fields, and nested structure validation.
    ///
    /// # Arguments
    /// * `data` - The component data to validate
    ///
    /// # Returns
    /// * `Ok(())` - If the data is valid according to the schema
    /// * `Err(ValidationError)` - If the data doesn't match the schema
    ///
    /// # Examples
    /// ```rust
    /// # use stigmergy::{Component, ComponentDefinition};
    /// # use serde_json::json;
    /// let component = Component::new("Health").unwrap();
    /// let schema = json!({
    ///     "type": "object",
    ///     "properties": {"hp": {"type": "integer"}},
    ///     "required": ["hp"]
    /// });
    /// let definition = ComponentDefinition::new(component, schema);
    ///
    /// // Valid data
    /// assert!(definition.validate_component_data(&json!({"hp": 100})).is_ok());
    ///
    /// // Invalid data (wrong type)
    /// assert!(definition.validate_component_data(&json!({"hp": "high"})).is_err());
    ///
    /// // Invalid data (missing required field)
    /// assert!(definition.validate_component_data(&json!({})).is_err());
    /// ```
    pub fn validate_component_data(&self, data: &Value) -> Result<(), ValidationError> {
        validate_value(data, &self.schema)
    }
}

/// A wrapper that extracts ComponentDefinition from either JSON or YAML based on Content-Type.
pub struct ComponentDefinitionExtractor(pub ComponentDefinition);

#[async_trait]
impl<S> FromRequest<S> for ComponentDefinitionExtractor
where
    S: Send + Sync,
{
    type Rejection = (StatusCode, &'static str);

    async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
        let (parts, body) = req.into_parts();
        let content_type = parts
            .headers
            .get("content-type")
            .and_then(|v| v.to_str().ok())
            .unwrap_or("application/json")
            .to_string();

        let bytes = Bytes::from_request(Request::from_parts(parts, body), state)
            .await
            .map_err(|_| (StatusCode::BAD_REQUEST, "failed to read request body"))?;

        let definition = if content_type.contains("yaml") || content_type.contains("yml") {
            serde_yml::from_slice::<ComponentDefinition>(&bytes)
                .map_err(|_| (StatusCode::BAD_REQUEST, "invalid yaml"))?
        } else {
            serde_json::from_slice::<ComponentDefinition>(&bytes)
                .map_err(|_| (StatusCode::BAD_REQUEST, "invalid json"))?
        };

        Ok(ComponentDefinitionExtractor(definition))
    }
}

/// Validates the structure of a JSON schema to ensure it's well-formed.
///
/// This function recursively validates JSON schema objects to ensure they follow
/// the expected format and contain valid schema constructs. It supports:
/// - Basic types (null, boolean, integer, number, string)
/// - Complex types (array, object)
/// - Union types via oneOf
/// - Nested schemas and recursive validation
///
/// # Arguments
/// * `schema` - The JSON schema value to validate
///
/// # Returns
/// * `Ok(())` - If the schema structure is valid
/// * `Err(ValidationError::InvalidSchema)` - If the schema structure is malformed
fn validate_schema_structure(schema: &Value) -> Result<(), ValidationError> {
    if !schema.is_object() {
        return Err(ValidationError::InvalidSchema(
            "Schema must be an object".to_string(),
        ));
    }

    let schema_obj = schema.as_object().unwrap();

    if let Some(one_of) = schema_obj.get("oneOf") {
        if !one_of.is_array() {
            return Err(ValidationError::InvalidSchema(
                "oneOf must be an array".to_string(),
            ));
        }

        for (i, sub_schema) in one_of.as_array().unwrap().iter().enumerate() {
            validate_schema_structure(sub_schema).map_err(|e| {
                ValidationError::InvalidSchema(format!(
                    "Invalid oneOf schema at index {}: {}",
                    i, e
                ))
            })?;
        }
        return Ok(());
    }

    if let Some(schema_type) = schema_obj.get("type") {
        if !schema_type.is_string() {
            return Err(ValidationError::InvalidSchema(
                "Schema type must be a string".to_string(),
            ));
        }

        let type_str = schema_type.as_str().unwrap();
        match type_str {
            "null" | "boolean" | "integer" | "number" | "string" => Ok(()),
            "array" => {
                if let Some(items) = schema_obj.get("items") {
                    validate_schema_structure(items)
                } else {
                    Ok(())
                }
            }
            "object" => {
                if let Some(properties) = schema_obj.get("properties") {
                    if !properties.is_object() {
                        return Err(ValidationError::InvalidSchema(
                            "Properties must be an object".to_string(),
                        ));
                    }

                    for (prop_name, prop_schema) in properties.as_object().unwrap() {
                        validate_schema_structure(prop_schema).map_err(|e| {
                            ValidationError::InvalidSchema(format!(
                                "Invalid property schema '{}': {}",
                                prop_name, e
                            ))
                        })?;
                    }
                }
                Ok(())
            }
            _ => Err(ValidationError::InvalidSchema(format!(
                "Unknown schema type: {}",
                type_str
            ))),
        }
    } else {
        Err(ValidationError::InvalidSchema(
            "Schema must have either 'type' or 'oneOf'".to_string(),
        ))
    }
}

async fn get_component_definitions(
    State(pool): State<sqlx::PgPool>,
    Query(_params): Query<HashMap<String, String>>,
) -> Result<Json<Vec<ComponentDefinition>>, (StatusCode, &'static str)> {
    let mut tx = pool.begin().await.map_err(|_e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            "failed to begin transaction",
        )
    })?;

    match crate::sql::component_definition::list(&mut tx).await {
        Ok(definitions) => {
            tx.commit().await.map_err(|_e| {
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "failed to commit transaction",
                )
            })?;
            Ok(Json(definitions))
        }
        Err(_) => Err((
            StatusCode::INTERNAL_SERVER_ERROR,
            "failed to list component definitions",
        )),
    }
}

async fn create_component_definition(
    State(pool): State<sqlx::PgPool>,
    ComponentDefinitionExtractor(definition): ComponentDefinitionExtractor,
) -> Result<Json<ComponentDefinition>, (StatusCode, &'static str)> {
    if let Err(_e) = definition.validate_schema() {
        return Err((StatusCode::BAD_REQUEST, "invalid schema"));
    }

    let mut tx = pool.begin().await.map_err(|_e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            "failed to begin transaction",
        )
    })?;

    match crate::sql::component_definition::create(&mut tx, &definition).await {
        Ok(()) => {
            tx.commit().await.map_err(|_e| {
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "failed to commit transaction",
                )
            })?;
            Ok(Json(definition))
        }
        Err(crate::DataStoreError::AlreadyExists) => Err((StatusCode::CONFLICT, "already exists")),
        Err(_) => Err((StatusCode::INTERNAL_SERVER_ERROR, "internal server error")),
    }
}

async fn update_component_definition(
    State(pool): State<sqlx::PgPool>,
    ComponentDefinitionExtractor(definition): ComponentDefinitionExtractor,
) -> Result<Json<ComponentDefinition>, (StatusCode, &'static str)> {
    if let Err(_e) = definition.validate_schema() {
        return Err((StatusCode::BAD_REQUEST, "invalid schema"));
    }

    let mut tx = pool.begin().await.map_err(|_e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            "failed to begin transaction",
        )
    })?;

    match crate::sql::component_definition::update(&mut tx, &definition).await {
        Ok(_) => {
            tx.commit().await.map_err(|_e| {
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "failed to commit transaction",
                )
            })?;
            Ok(Json(definition))
        }
        Err(_) => Err((
            StatusCode::INTERNAL_SERVER_ERROR,
            "failed to update component definition",
        )),
    }
}

async fn patch_component_definition(
    State(pool): State<sqlx::PgPool>,
    Json(patch): Json<Value>,
) -> Result<Json<ComponentDefinition>, (StatusCode, &'static str)> {
    let component = Component::new("PatchedComponent").unwrap();
    let definition = ComponentDefinition {
        component: component.clone(),
        schema: patch.clone(),
    };

    if let Err(_e) = definition.validate_schema() {
        return Err((StatusCode::BAD_REQUEST, "invalid schema"));
    }

    let mut tx = pool.begin().await.map_err(|_e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            "failed to begin transaction",
        )
    })?;

    match crate::sql::component_definition::update(&mut tx, &definition).await {
        Ok(_) => {
            tx.commit().await.map_err(|_e| {
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "failed to commit transaction",
                )
            })?;
            Ok(Json(definition))
        }
        Err(_) => Err((StatusCode::INTERNAL_SERVER_ERROR, "internal server error")),
    }
}

async fn delete_component_definitions(
    State(pool): State<sqlx::PgPool>,
) -> Result<StatusCode, (StatusCode, &'static str)> {
    let mut tx = pool.begin().await.map_err(|_e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            "failed to begin transaction",
        )
    })?;

    let definitions = match crate::sql::component_definition::list(&mut tx).await {
        Ok(defs) => defs,
        Err(_) => return Err((StatusCode::INTERNAL_SERVER_ERROR, "internal server error")),
    };

    for definition in definitions {
        if crate::sql::component_definition::delete(&mut tx, &definition.component)
            .await
            .is_err()
        {
            return Err((StatusCode::INTERNAL_SERVER_ERROR, "internal server error"));
        }
    }

    tx.commit().await.map_err(|_e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            "failed to commit transaction",
        )
    })?;
    Ok(StatusCode::NO_CONTENT)
}

async fn get_component_definition_by_id(
    State(pool): State<sqlx::PgPool>,
    Path(id): Path<String>,
) -> Result<Json<ComponentDefinition>, (StatusCode, &'static str)> {
    let component =
        Component::new(&id).ok_or((StatusCode::BAD_REQUEST, "invalid component name"))?;

    let mut tx = pool.begin().await.map_err(|_e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            "failed to begin transaction",
        )
    })?;

    match crate::sql::component_definition::get(&mut tx, &component).await {
        Ok(Some(record)) => {
            tx.commit().await.map_err(|_e| {
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "failed to commit transaction",
                )
            })?;
            Ok(Json(record.definition))
        }
        Ok(None) => Err((StatusCode::NOT_FOUND, "not found")),
        Err(_) => Err((StatusCode::INTERNAL_SERVER_ERROR, "internal server error")),
    }
}

async fn update_component_definition_by_id(
    State(pool): State<sqlx::PgPool>,
    Path(id): Path<String>,
    ComponentDefinitionExtractor(definition): ComponentDefinitionExtractor,
) -> Result<Json<ComponentDefinition>, (StatusCode, &'static str)> {
    let component =
        Component::new(&id).ok_or((StatusCode::BAD_REQUEST, "invalid component name"))?;

    if let Err(_e) = definition.validate_schema() {
        return Err((StatusCode::BAD_REQUEST, "invalid schema"));
    }

    if component != definition.component {
        return Err((StatusCode::BAD_REQUEST, "component name mismatch"));
    }

    let mut tx = pool.begin().await.map_err(|_e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            "failed to begin transaction",
        )
    })?;

    match crate::sql::component_definition::update(&mut tx, &definition).await {
        Ok(_) => {
            tx.commit().await.map_err(|_e| {
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "failed to commit transaction",
                )
            })?;
            Ok(Json(definition))
        }
        Err(_) => Err((StatusCode::INTERNAL_SERVER_ERROR, "internal server error")),
    }
}

async fn patch_component_definition_by_id(
    State(pool): State<sqlx::PgPool>,
    Path(id): Path<String>,
    Json(patch): Json<Value>,
) -> Result<Json<ComponentDefinition>, (StatusCode, &'static str)> {
    let component =
        Component::new(&id).ok_or((StatusCode::BAD_REQUEST, "invalid component name"))?;
    let definition = ComponentDefinition {
        component: component.clone(),
        schema: patch.clone(),
    };

    if let Err(_e) = definition.validate_schema() {
        return Err((StatusCode::BAD_REQUEST, "invalid schema"));
    }

    let mut tx = pool.begin().await.map_err(|_e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            "failed to begin transaction",
        )
    })?;

    match crate::sql::component_definition::update(&mut tx, &definition).await {
        Ok(_) => {
            tx.commit().await.map_err(|_e| {
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "failed to commit transaction",
                )
            })?;
            Ok(Json(definition))
        }
        Err(_) => Err((StatusCode::INTERNAL_SERVER_ERROR, "internal server error")),
    }
}

async fn delete_component_definition_by_id(
    State(pool): State<sqlx::PgPool>,
    Path(id): Path<String>,
) -> Result<StatusCode, (StatusCode, &'static str)> {
    let component =
        Component::new(&id).ok_or((StatusCode::BAD_REQUEST, "invalid component name"))?;

    let mut tx = pool.begin().await.map_err(|_e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            "failed to begin transaction",
        )
    })?;

    match crate::sql::component_definition::delete(&mut tx, &component).await {
        Ok(true) => {
            tx.commit().await.map_err(|_e| {
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "failed to commit transaction",
                )
            })?;
            Ok(StatusCode::NO_CONTENT)
        }
        Ok(false) => Err((StatusCode::NOT_FOUND, "not found")),
        Err(_) => Err((StatusCode::INTERNAL_SERVER_ERROR, "internal server error")),
    }
}

/// Creates the HTTP router for component definition endpoints.
///
/// This function sets up all the routes for managing component definitions using PostgreSQL.
///
/// # Arguments
/// * `pool` - PostgreSQL connection pool
///
/// # Returns
/// An Axum Router configured with component definition routes
pub fn create_component_definition_router(pool: sqlx::PgPool) -> Router {
    Router::new()
        .route(
            "/componentdefinition",
            get(get_component_definitions)
                .post(create_component_definition)
                .put(update_component_definition)
                .patch(patch_component_definition)
                .delete(delete_component_definitions),
        )
        .route(
            "/componentdefinition/:id",
            get(get_component_definition_by_id)
                .put(update_component_definition_by_id)
                .patch(patch_component_definition_by_id)
                .delete(delete_component_definition_by_id),
        )
        .with_state(pool)
}