Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | const mongoose = require('mongoose');
/**
* Schéma pour les champs d'un JSON Schema (mode 'data')
* Supporte la récursion pour les objets imbriqués
*/
const schemaFieldSchema = new mongoose.Schema({
type: {
type: String,
enum: ['string', 'number', 'integer', 'boolean', 'array', 'object'],
required: true
},
description: {
type: String,
default: ''
},
required: {
type: Boolean,
default: false
},
// Pour les arrays: type des items
items: {
type: mongoose.Schema.Types.Mixed,
default: null
},
// Pour les objets: propriétés imbriquées
properties: {
type: mongoose.Schema.Types.Mixed,
default: null
}
}, { _id: false });
/**
* Schéma pour la spécification selon le mode
*/
const specSchema = new mongoose.Schema({
// Mode ASSERTION: instruction textuelle
instruction: {
type: String,
default: ''
},
// Champ DB conservé comme 'maxFacts' pour la rétrocompat; la notion est 'maxAssertions'
maxFacts: {
type: Number,
default: 100,
min: 1,
max: 1000
},
// Mode DATA: JSON Schema avec descriptions
schema: {
type: mongoose.Schema.Types.Mixed,
default: null
}
}, { _id: false });
specSchema.virtual('maxAssertions').get(function () {
return this.maxFacts;
}).set(function (v) {
this.maxFacts = v;
});
/**
* Modèle Workflow unifié
* Représente une config métier de synthèse (visible utilisateur)
* Supporte deux modes: 'assertion' et 'data'
*/
const workflowSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
index: true
},
name: {
type: String,
required: true,
trim: true
},
description: {
type: String,
default: ''
},
// Mode de synthèse: 'assertion' ou 'data' ('fact' conservé pour rétrocompat DB)
mode: {
type: String,
enum: ['assertion', 'data', 'fact'],
required: true,
index: true
},
// Spécification selon le mode
spec: {
type: specSchema,
required: true
},
// Router optionnel pour le routage des résultats
routerId: {
type: String,
default: ''
},
// Bases de connaissances cibles
targetKnowledgeBases: [{
type: String
}],
// Statut actif
isActive: {
type: Boolean,
default: true
}
}, {
timestamps: true
});
// Index composé pour les requêtes courantes
workflowSchema.index({ userId: 1, isActive: 1, mode: 1 });
// Méthode d'instance pour obtenir le schéma formaté (mode data)
workflowSchema.methods.getFormattedSchema = function() {
if (this.mode !== 'data' || !this.spec || !this.spec.schema) {
return null;
}
// Retourne le schéma avec les descriptions pour le prompt LLM
return this.spec.schema;
};
// Méthode d'instance pour obtenir l'instruction (mode assertion; 'fact' legacy accepté)
workflowSchema.methods.getInstruction = function() {
const isAssertionMode = this.mode === 'assertion' || this.mode === 'fact';
if (!isAssertionMode || !this.spec) {
return '';
}
return this.spec.instruction || '';
};
// Méthode statique pour valider la cohérence mode/spec
workflowSchema.statics.validateSpec = function(mode, spec) {
const errors = [];
if (!spec) {
errors.push('La spécification (spec) est requise');
return errors;
}
// Normalisation du mode: 'fact' (legacy) → 'assertion'
const normalizedMode = mode === 'fact' ? 'assertion' : mode;
if (normalizedMode === 'assertion') {
// Mode ASSERTION: instruction requise
if (!spec.instruction || spec.instruction.trim() === '') {
errors.push('Mode ASSERTION: l\'instruction est requise');
}
const maxAssertions = spec.maxAssertions ?? spec.maxFacts;
if (!maxAssertions || maxAssertions < 1 || maxAssertions > 1000) {
errors.push('Mode ASSERTION: maxAssertions doit être entre 1 et 1000');
}
} else if (normalizedMode === 'data') {
// Mode DATA: schema requis et valide
if (!spec.schema) {
errors.push('Mode DATA: le schéma JSON est requis');
} else {
// Validation basique du JSON Schema
if (!spec.schema.type) {
errors.push('Mode DATA: le schéma doit avoir un type');
}
if (spec.schema.type !== 'object' && spec.schema.type !== 'array') {
errors.push('Mode DATA: le type racine doit être "object" ou "array"');
}
}
}
return errors;
};
module.exports = mongoose.model('Workflow', workflowSchema);
|