oxify-model 0.1.0

Data models and types for OxiFY workflows, execution, and configuration
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! GraphQL schema generation for workflow models
//!
//! This module provides functionality to generate GraphQL Schema Definition Language (SDL)
//! from workflow models for API integration.

use std::collections::HashMap;

#[cfg(feature = "openapi")]
use utoipa::ToSchema;

/// GraphQL type definition
#[derive(Debug, Clone)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct GraphQLType {
    /// Type name
    pub name: String,

    /// Type kind (Object, Enum, Interface, etc.)
    pub kind: GraphQLTypeKind,

    /// Description
    pub description: Option<String>,

    /// Fields for object types
    pub fields: Vec<GraphQLField>,

    /// Enum values for enum types
    pub enum_values: Vec<String>,

    /// Implemented interfaces
    pub interfaces: Vec<String>,
}

/// GraphQL type kind
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub enum GraphQLTypeKind {
    /// Object type
    Object,
    /// Enum type
    Enum,
    /// Interface type
    Interface,
    /// Input object type
    Input,
}

/// GraphQL field definition
#[derive(Debug, Clone)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct GraphQLField {
    /// Field name
    pub name: String,

    /// Field type
    pub field_type: String,

    /// Whether the field is required (non-null)
    pub required: bool,

    /// Whether the field is a list
    pub is_list: bool,

    /// Description
    pub description: Option<String>,

    /// Arguments for the field
    pub arguments: Vec<GraphQLArgument>,
}

/// GraphQL argument definition
#[derive(Debug, Clone)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct GraphQLArgument {
    /// Argument name
    pub name: String,

    /// Argument type
    pub arg_type: String,

    /// Whether the argument is required
    pub required: bool,

    /// Default value
    pub default_value: Option<String>,
}

impl GraphQLType {
    /// Create a new GraphQL type
    pub fn new(name: String, kind: GraphQLTypeKind) -> Self {
        Self {
            name,
            kind,
            description: None,
            fields: Vec::new(),
            enum_values: Vec::new(),
            interfaces: Vec::new(),
        }
    }

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

    /// Add a field
    pub fn add_field(&mut self, field: GraphQLField) {
        self.fields.push(field);
    }

    /// Add an enum value
    pub fn add_enum_value(&mut self, value: String) {
        self.enum_values.push(value);
    }

    /// Add an interface
    pub fn add_interface(&mut self, interface: String) {
        self.interfaces.push(interface);
    }

    /// Convert to SDL string
    pub fn to_sdl(&self) -> String {
        let mut lines = Vec::new();

        // Add description if present
        if let Some(desc) = &self.description {
            lines.push(format!("\"\"\"{}\"\"\"", desc));
        }

        // Add type definition
        match self.kind {
            GraphQLTypeKind::Object => {
                let interfaces = if self.interfaces.is_empty() {
                    String::new()
                } else {
                    format!(" implements {}", self.interfaces.join(" & "))
                };
                lines.push(format!("type {}{} {{", self.name, interfaces));
                for field in &self.fields {
                    lines.push(format!("  {}", field.to_sdl()));
                }
                lines.push("}".to_string());
            }
            GraphQLTypeKind::Enum => {
                lines.push(format!("enum {} {{", self.name));
                for value in &self.enum_values {
                    lines.push(format!("  {}", value));
                }
                lines.push("}".to_string());
            }
            GraphQLTypeKind::Interface => {
                lines.push(format!("interface {} {{", self.name));
                for field in &self.fields {
                    lines.push(format!("  {}", field.to_sdl()));
                }
                lines.push("}".to_string());
            }
            GraphQLTypeKind::Input => {
                lines.push(format!("input {} {{", self.name));
                for field in &self.fields {
                    lines.push(format!("  {}", field.to_sdl()));
                }
                lines.push("}".to_string());
            }
        }

        lines.join("\n")
    }
}

impl GraphQLField {
    /// Create a new GraphQL field
    pub fn new(name: String, field_type: String) -> Self {
        Self {
            name,
            field_type,
            required: false,
            is_list: false,
            description: None,
            arguments: Vec::new(),
        }
    }

    /// Set required flag
    pub fn required(mut self) -> Self {
        self.required = true;
        self
    }

    /// Set list flag
    pub fn list(mut self) -> Self {
        self.is_list = true;
        self
    }

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

    /// Add an argument
    pub fn add_argument(&mut self, argument: GraphQLArgument) {
        self.arguments.push(argument);
    }

    /// Convert to SDL string
    pub fn to_sdl(&self) -> String {
        let mut result = String::new();

        // Add description if present
        if let Some(desc) = &self.description {
            result.push_str(&format!("\"\"\"{}\"\"\" ", desc));
        }

        result.push_str(&self.name);

        // Add arguments
        if !self.arguments.is_empty() {
            result.push('(');
            let args: Vec<String> = self.arguments.iter().map(|a| a.to_sdl()).collect();
            result.push_str(&args.join(", "));
            result.push(')');
        }

        result.push_str(": ");

        // Add type with list/required modifiers
        let type_str = if self.is_list {
            format!("[{}]", self.field_type)
        } else {
            self.field_type.clone()
        };

        result.push_str(&type_str);

        if self.required {
            result.push('!');
        }

        result
    }
}

impl GraphQLArgument {
    /// Create a new GraphQL argument
    pub fn new(name: String, arg_type: String) -> Self {
        Self {
            name,
            arg_type,
            required: false,
            default_value: None,
        }
    }

    /// Set required flag
    pub fn required(mut self) -> Self {
        self.required = true;
        self
    }

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

    /// Convert to SDL string
    pub fn to_sdl(&self) -> String {
        let mut result = format!("{}: {}", self.name, self.arg_type);

        if self.required {
            result.push('!');
        }

        if let Some(default) = &self.default_value {
            result.push_str(&format!(" = {}", default));
        }

        result
    }
}

/// GraphQL schema generator for workflows
pub struct GraphQLSchemaGenerator {
    /// Include descriptions
    pub include_descriptions: bool,

    /// Generated types
    types: HashMap<String, GraphQLType>,
}

impl GraphQLSchemaGenerator {
    /// Create a new schema generator
    pub fn new() -> Self {
        Self {
            include_descriptions: true,
            types: HashMap::new(),
        }
    }

    /// Generate GraphQL schema for workflows
    pub fn generate_workflow_schema(&mut self) -> String {
        // Generate core types
        self.generate_workflow_type();
        self.generate_metadata_type();
        self.generate_node_type();
        self.generate_edge_type();
        self.generate_node_kind_enum();
        self.generate_execution_state_enum();
        self.generate_query_type();
        self.generate_mutation_type();

        // Generate SDL
        self.to_sdl()
    }

    /// Generate the Workflow type
    fn generate_workflow_type(&mut self) {
        let mut workflow_type = GraphQLType::new("Workflow".to_string(), GraphQLTypeKind::Object);

        if self.include_descriptions {
            workflow_type = workflow_type
                .with_description("A workflow defining a sequence of operations".to_string());
        }

        workflow_type.add_field(GraphQLField::new("id".to_string(), "ID".to_string()).required());

        workflow_type.add_field(
            GraphQLField::new("metadata".to_string(), "WorkflowMetadata".to_string()).required(),
        );

        workflow_type.add_field(
            GraphQLField::new("nodes".to_string(), "Node".to_string())
                .list()
                .required(),
        );

        workflow_type.add_field(
            GraphQLField::new("edges".to_string(), "Edge".to_string())
                .list()
                .required(),
        );

        self.types.insert("Workflow".to_string(), workflow_type);
    }

    /// Generate the WorkflowMetadata type
    fn generate_metadata_type(&mut self) {
        let mut metadata_type =
            GraphQLType::new("WorkflowMetadata".to_string(), GraphQLTypeKind::Object);

        if self.include_descriptions {
            metadata_type =
                metadata_type.with_description("Workflow metadata and description".to_string());
        }

        metadata_type
            .add_field(GraphQLField::new("name".to_string(), "String".to_string()).required());

        metadata_type.add_field(GraphQLField::new(
            "description".to_string(),
            "String".to_string(),
        ));

        metadata_type
            .add_field(GraphQLField::new("version".to_string(), "String".to_string()).required());

        metadata_type.add_field(
            GraphQLField::new("createdAt".to_string(), "DateTime".to_string()).required(),
        );

        metadata_type.add_field(
            GraphQLField::new("updatedAt".to_string(), "DateTime".to_string()).required(),
        );

        metadata_type.add_field(
            GraphQLField::new("tags".to_string(), "String".to_string())
                .list()
                .required(),
        );

        self.types
            .insert("WorkflowMetadata".to_string(), metadata_type);
    }

    /// Generate the Node type
    fn generate_node_type(&mut self) {
        let mut node_type = GraphQLType::new("Node".to_string(), GraphQLTypeKind::Object);

        if self.include_descriptions {
            node_type = node_type.with_description("A workflow node".to_string());
        }

        node_type.add_field(GraphQLField::new("id".to_string(), "ID".to_string()).required());

        node_type.add_field(GraphQLField::new("name".to_string(), "String".to_string()).required());

        node_type
            .add_field(GraphQLField::new("kind".to_string(), "NodeKind".to_string()).required());

        self.types.insert("Node".to_string(), node_type);
    }

    /// Generate the Edge type
    fn generate_edge_type(&mut self) {
        let mut edge_type = GraphQLType::new("Edge".to_string(), GraphQLTypeKind::Object);

        if self.include_descriptions {
            edge_type = edge_type.with_description("An edge connecting two nodes".to_string());
        }

        edge_type.add_field(GraphQLField::new("id".to_string(), "ID".to_string()).required());

        edge_type.add_field(GraphQLField::new("from".to_string(), "ID".to_string()).required());

        edge_type.add_field(GraphQLField::new("to".to_string(), "ID".to_string()).required());

        self.types.insert("Edge".to_string(), edge_type);
    }

    /// Generate the NodeKind enum
    fn generate_node_kind_enum(&mut self) {
        let mut node_kind = GraphQLType::new("NodeKind".to_string(), GraphQLTypeKind::Enum);

        if self.include_descriptions {
            node_kind = node_kind.with_description("Types of nodes in a workflow".to_string());
        }

        node_kind.add_enum_value("START".to_string());
        node_kind.add_enum_value("END".to_string());
        node_kind.add_enum_value("LLM".to_string());
        node_kind.add_enum_value("RETRIEVER".to_string());
        node_kind.add_enum_value("CODE".to_string());
        node_kind.add_enum_value("IF_ELSE".to_string());
        node_kind.add_enum_value("TOOL".to_string());
        node_kind.add_enum_value("LOOP".to_string());
        node_kind.add_enum_value("TRY_CATCH".to_string());
        node_kind.add_enum_value("SUB_WORKFLOW".to_string());
        node_kind.add_enum_value("SWITCH".to_string());
        node_kind.add_enum_value("PARALLEL".to_string());
        node_kind.add_enum_value("APPROVAL".to_string());
        node_kind.add_enum_value("FORM".to_string());

        self.types.insert("NodeKind".to_string(), node_kind);
    }

    /// Generate the ExecutionState enum
    fn generate_execution_state_enum(&mut self) {
        let mut exec_state = GraphQLType::new("ExecutionState".to_string(), GraphQLTypeKind::Enum);

        if self.include_descriptions {
            exec_state = exec_state.with_description("State of a workflow execution".to_string());
        }

        exec_state.add_enum_value("PENDING".to_string());
        exec_state.add_enum_value("RUNNING".to_string());
        exec_state.add_enum_value("COMPLETED".to_string());
        exec_state.add_enum_value("FAILED".to_string());
        exec_state.add_enum_value("CANCELLED".to_string());

        self.types.insert("ExecutionState".to_string(), exec_state);
    }

    /// Generate the Query type
    fn generate_query_type(&mut self) {
        let mut query_type = GraphQLType::new("Query".to_string(), GraphQLTypeKind::Object);

        // Get workflow by ID
        let mut get_workflow = GraphQLField::new("workflow".to_string(), "Workflow".to_string());
        get_workflow
            .add_argument(GraphQLArgument::new("id".to_string(), "ID".to_string()).required());
        query_type.add_field(get_workflow);

        // List workflows
        let mut list_workflows = GraphQLField::new("workflows".to_string(), "Workflow".to_string())
            .list()
            .required();
        list_workflows.add_argument(
            GraphQLArgument::new("limit".to_string(), "Int".to_string())
                .with_default("10".to_string()),
        );
        list_workflows.add_argument(
            GraphQLArgument::new("offset".to_string(), "Int".to_string())
                .with_default("0".to_string()),
        );
        query_type.add_field(list_workflows);

        self.types.insert("Query".to_string(), query_type);
    }

    /// Generate the Mutation type
    fn generate_mutation_type(&mut self) {
        let mut mutation_type = GraphQLType::new("Mutation".to_string(), GraphQLTypeKind::Object);

        // Create workflow
        let mut create_workflow =
            GraphQLField::new("createWorkflow".to_string(), "Workflow".to_string()).required();
        create_workflow.add_argument(
            GraphQLArgument::new("name".to_string(), "String".to_string()).required(),
        );
        create_workflow.add_argument(GraphQLArgument::new(
            "description".to_string(),
            "String".to_string(),
        ));
        mutation_type.add_field(create_workflow);

        // Delete workflow
        let mut delete_workflow =
            GraphQLField::new("deleteWorkflow".to_string(), "Boolean".to_string()).required();
        delete_workflow
            .add_argument(GraphQLArgument::new("id".to_string(), "ID".to_string()).required());
        mutation_type.add_field(delete_workflow);

        self.types.insert("Mutation".to_string(), mutation_type);
    }

    /// Convert all types to SDL
    pub fn to_sdl(&self) -> String {
        let mut sdl = Vec::new();

        // Add custom scalars
        sdl.push("scalar DateTime".to_string());
        sdl.push("".to_string());

        // Add types in order: enums, objects, query, mutation
        let type_order = vec![
            "NodeKind",
            "ExecutionState",
            "Edge",
            "Node",
            "WorkflowMetadata",
            "Workflow",
            "Query",
            "Mutation",
        ];

        for type_name in type_order {
            if let Some(gql_type) = self.types.get(type_name) {
                sdl.push(gql_type.to_sdl());
                sdl.push("".to_string());
            }
        }

        sdl.join("\n")
    }
}

impl Default for GraphQLSchemaGenerator {
    fn default() -> Self {
        Self::new()
    }
}

/// Generate a GraphQL schema for workflows
pub fn generate_graphql_schema() -> String {
    let mut generator = GraphQLSchemaGenerator::new();
    generator.generate_workflow_schema()
}

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

    #[test]
    fn test_graphql_field_sdl() {
        let field = GraphQLField::new("name".to_string(), "String".to_string()).required();
        assert_eq!(field.to_sdl(), "name: String!");
    }

    #[test]
    fn test_graphql_field_list() {
        let field = GraphQLField::new("tags".to_string(), "String".to_string())
            .list()
            .required();
        assert_eq!(field.to_sdl(), "tags: [String]!");
    }

    #[test]
    fn test_graphql_argument() {
        let arg = GraphQLArgument::new("id".to_string(), "ID".to_string()).required();
        assert_eq!(arg.to_sdl(), "id: ID!");
    }

    #[test]
    fn test_graphql_argument_with_default() {
        let arg = GraphQLArgument::new("limit".to_string(), "Int".to_string())
            .with_default("10".to_string());
        assert_eq!(arg.to_sdl(), "limit: Int = 10");
    }

    #[test]
    fn test_enum_type_sdl() {
        let mut enum_type = GraphQLType::new("Status".to_string(), GraphQLTypeKind::Enum);
        enum_type.add_enum_value("ACTIVE".to_string());
        enum_type.add_enum_value("INACTIVE".to_string());

        let sdl = enum_type.to_sdl();
        assert!(sdl.contains("enum Status"));
        assert!(sdl.contains("ACTIVE"));
        assert!(sdl.contains("INACTIVE"));
    }

    #[test]
    fn test_object_type_sdl() {
        let mut object_type = GraphQLType::new("User".to_string(), GraphQLTypeKind::Object);
        object_type.add_field(GraphQLField::new("id".to_string(), "ID".to_string()).required());
        object_type
            .add_field(GraphQLField::new("name".to_string(), "String".to_string()).required());

        let sdl = object_type.to_sdl();
        assert!(sdl.contains("type User"));
        assert!(sdl.contains("id: ID!"));
        assert!(sdl.contains("name: String!"));
    }

    #[test]
    fn test_generate_workflow_schema() {
        let mut generator = GraphQLSchemaGenerator::new();
        let schema = generator.generate_workflow_schema();

        assert!(schema.contains("type Workflow"));
        assert!(schema.contains("type Node"));
        assert!(schema.contains("type Edge"));
        assert!(schema.contains("enum NodeKind"));
        assert!(schema.contains("type Query"));
        assert!(schema.contains("type Mutation"));
    }

    #[test]
    fn test_schema_has_node_kinds() {
        let mut generator = GraphQLSchemaGenerator::new();
        let schema = generator.generate_workflow_schema();

        assert!(schema.contains("START"));
        assert!(schema.contains("END"));
        assert!(schema.contains("LLM"));
        assert!(schema.contains("RETRIEVER"));
    }

    #[test]
    fn test_schema_has_execution_states() {
        let mut generator = GraphQLSchemaGenerator::new();
        let schema = generator.generate_workflow_schema();

        assert!(schema.contains("PENDING"));
        assert!(schema.contains("RUNNING"));
        assert!(schema.contains("COMPLETED"));
        assert!(schema.contains("FAILED"));
    }

    #[test]
    fn test_query_type_has_workflow_field() {
        let mut generator = GraphQLSchemaGenerator::new();
        generator.generate_query_type();

        if let Some(query_type) = generator.types.get("Query") {
            assert!(query_type.fields.iter().any(|f| f.name == "workflow"));
            assert!(query_type.fields.iter().any(|f| f.name == "workflows"));
        } else {
            panic!("Query type not found");
        }
    }

    #[test]
    fn test_mutation_type_has_create_workflow() {
        let mut generator = GraphQLSchemaGenerator::new();
        generator.generate_mutation_type();

        if let Some(mutation_type) = generator.types.get("Mutation") {
            assert!(mutation_type
                .fields
                .iter()
                .any(|f| f.name == "createWorkflow"));
            assert!(mutation_type
                .fields
                .iter()
                .any(|f| f.name == "deleteWorkflow"));
        } else {
            panic!("Mutation type not found");
        }
    }

    #[test]
    fn test_field_with_description() {
        let field = GraphQLField::new("name".to_string(), "String".to_string())
            .with_description("The name of the user".to_string())
            .required();

        let sdl = field.to_sdl();
        assert!(sdl.contains("The name of the user"));
    }

    #[test]
    fn test_type_with_description() {
        let type_def = GraphQLType::new("User".to_string(), GraphQLTypeKind::Object)
            .with_description("A user in the system".to_string());

        let sdl = type_def.to_sdl();
        assert!(sdl.contains("A user in the system"));
    }

    #[test]
    fn test_field_with_arguments() {
        let mut field = GraphQLField::new("users".to_string(), "User".to_string()).list();
        field.add_argument(GraphQLArgument::new("limit".to_string(), "Int".to_string()));
        field.add_argument(GraphQLArgument::new(
            "offset".to_string(),
            "Int".to_string(),
        ));

        let sdl = field.to_sdl();
        assert!(sdl.contains("users("));
        assert!(sdl.contains("limit: Int"));
        assert!(sdl.contains("offset: Int"));
    }
}