@jsonfirst/sdk 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +59 -6
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -85,20 +85,73 @@
|
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
/**
|
|
88
|
-
* Validate a JSON
|
|
89
|
-
* @param {Object} json - The JSON to validate
|
|
90
|
-
* @returns {{ valid: boolean, errors: string[] }}
|
|
88
|
+
* Validate a JSONFIRST output JSON against the JSONFIRST v2 spec.
|
|
89
|
+
* @param {Object} json - The JSONFIRST JSON to validate
|
|
90
|
+
* @returns {{ valid: boolean, errors: string[], warnings: string[] }}
|
|
91
91
|
*/
|
|
92
92
|
validate(json) {
|
|
93
93
|
const errors = [];
|
|
94
|
+
const warnings = [];
|
|
94
95
|
if (!json || typeof json !== 'object') {
|
|
95
|
-
return { valid: false, errors: ['Input must be a JSON object'] };
|
|
96
|
+
return { valid: false, errors: ['Input must be a JSON object'], warnings: [] };
|
|
96
97
|
}
|
|
97
98
|
if (json.spec !== 'JSONFIRST') errors.push('Missing or incorrect "spec" field (expected "JSONFIRST")');
|
|
98
99
|
if (!json.version) errors.push('Missing "version" field');
|
|
99
|
-
if (!json.jdons || !Array.isArray(json.jdons))
|
|
100
|
+
if (!json.jdons || !Array.isArray(json.jdons)) {
|
|
101
|
+
errors.push('Missing "jdons" array');
|
|
102
|
+
} else if (json.jdons.length === 0) {
|
|
103
|
+
warnings.push('"jdons" array is empty — no intents parsed');
|
|
104
|
+
}
|
|
100
105
|
if (!json.execution || typeof json.execution !== 'object') errors.push('Missing "execution" object');
|
|
101
|
-
return { valid: errors.length === 0, errors };
|
|
106
|
+
return { valid: errors.length === 0, errors, warnings };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Validate a single JDON intent for completeness and executability.
|
|
111
|
+
* Checks required params, confidence threshold, and executable flag.
|
|
112
|
+
* @param {Object} intent - A single JDON object from jdons[]
|
|
113
|
+
* @param {Object} [options]
|
|
114
|
+
* @param {number} [options.minConfidence=0.5] - Minimum confidence threshold (0-1)
|
|
115
|
+
* @param {string[]} [options.requiredParams=[]] - List of required param keys in intent.object
|
|
116
|
+
* @returns {{ valid: boolean, executable: boolean, missing_params: string[], errors: string[], confidence: number }}
|
|
117
|
+
*/
|
|
118
|
+
validateIntent(intent, options = {}) {
|
|
119
|
+
const { minConfidence = 0.5, requiredParams = [] } = options;
|
|
120
|
+
const errors = [];
|
|
121
|
+
const missing_params = [];
|
|
122
|
+
|
|
123
|
+
if (!intent || typeof intent !== 'object') {
|
|
124
|
+
return { valid: false, executable: false, missing_params: [], errors: ['Intent must be a JSON object'], confidence: 0 };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Action check
|
|
128
|
+
if (!intent.action || !intent.action.normalized) errors.push('Missing action.normalized');
|
|
129
|
+
|
|
130
|
+
// Confidence check
|
|
131
|
+
const confidence = intent.confidence || 0;
|
|
132
|
+
if (confidence < minConfidence) {
|
|
133
|
+
errors.push(`Confidence ${(confidence * 100).toFixed(0)}% is below minimum ${(minConfidence * 100).toFixed(0)}%`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Required params check
|
|
137
|
+
const obj = intent.object || {};
|
|
138
|
+
for (const param of requiredParams) {
|
|
139
|
+
if (obj[param] === undefined || obj[param] === null) {
|
|
140
|
+
missing_params.push(param);
|
|
141
|
+
errors.push(`Missing required param: ${param}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Missing params from JSONFIRST spec
|
|
146
|
+
if (intent.missing_params && intent.missing_params.length > 0) {
|
|
147
|
+
for (const p of intent.missing_params) {
|
|
148
|
+
if (!missing_params.includes(p)) missing_params.push(p);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const executable = intent.executable === true && missing_params.length === 0 && errors.length === 0;
|
|
153
|
+
|
|
154
|
+
return { valid: errors.length === 0, executable, missing_params, errors, confidence };
|
|
102
155
|
}
|
|
103
156
|
|
|
104
157
|
/**
|