@meta-aiml/parser
Version:
AIML Parser SDK v2.0.0 - Comprehensive schema validation for Meta-AIML.org entity schemas
3 lines (2 loc) • 13.1 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).AIMLParser=t()}(this,(function(){"use strict";function e(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,i=Array(t);n<t;n++)i[n]=e[n];return i}function t(e,t,n){return t&&function(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,i(r.key),r)}}(e.prototype,t),Object.defineProperty(e,"prototype",{writable:!1}),e}function n(t){return function(t){if(Array.isArray(t))return e(t)}(t)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(t)||function(t,n){if(t){if("string"==typeof t)return e(t,n);var i={}.toString.call(t).slice(8,-1);return"Object"===i&&t.constructor&&(i=t.constructor.name),"Map"===i||"Set"===i?Array.from(t):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?e(t,n):void 0}}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function i(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var i=n.call(e,t);if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e,"string");return"symbol"==typeof t?t:t+""}function r(e){return r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},r(e)}function a(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var o="https://schemas.meta-aiml.org/v2.0.0/context.jsonld",s="2.0.0",c=["organization","product_offering","service","creative_work","community","financial_product"],l={organization:["clinic","education_platform","fitness_platform","hotel","restaurant","store"],product_offering:["ecommerce_store","marketplace","product","software_product"],service:["business_services","generative_ai_platform","real_estate_platform","ridesharing_service","task_management_app","telemedicine_platform","virtual_event_platform","web_app","website_services"],creative_work:["blog","event","file_hosting","gaming_platform","news","personal_website","photo_hosting","streaming_platform","video_hosting"],community:["dating_platform","social_network"],financial_product:["online_banking"]},u={ecommerce_platform:["ecommerce_store","marketplace","store"],hospitality:["hotel","restaurant"],healthcare_services:["clinic","fitness_platform","telemedicine_platform"],education_services:["education_platform"],ai_platform:["generative_ai_platform"],professional_services:["business_services"],ridesharing_services:["ridesharing_service"],website_services:["website_services"],property_services:["real_estate_platform"],physical_product:["product"],digital_product:["file_hosting","personal_website","software_product","task_management_app","web_app"],media_entertainment:["blog","gaming_platform","news","photo_hosting","streaming_platform","video_hosting"],social_platform:["dating_platform","social_network"],event_platform:["event","virtual_event_platform"],financial_services:["online_banking"]},d={professional_services:{allowedCategories:["service"],description:"Professional and business services"},event_platform:{allowedCategories:["creative_work","service"],description:"Event organization and management platforms"},physical_product:{allowedCategories:["product_offering"],description:"Physical goods and products"},website_services:{allowedCategories:["service"],description:"Website development and maintenance services"},gaming_platform:{allowedCategories:["creative_work"],description:"Gaming and interactive entertainment platforms"}},g=["auth","compliance","location","logistics","multilingual","notification","payments","recommendations","search","security","streaming","subscription","user_management","warranty"],m=["cultural_context","geographical_context","regulatory_context"],f=function(){return t((function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.config={debug:t.debug||!1,strict:t.strict||!1,version:t.version||s},this.errors=[],this.warnings=[],this.suggestions=[]}),[{key:"validate",value:function(e){var t;this.errors=[],this.warnings=[],this.suggestions=[];try{t="string"==typeof e?JSON.parse(e):e}catch(e){return this._addError("JSON","Invalid JSON syntax","structure","Please check for missing commas, brackets, or quotes"),this._buildResult(!1,null)}this._validateRequiredFields(t),this._validateContext(t),this._validateVersioning(t),this._validateEntityStructure(t),this._validateNames(t),this._validateModules(t),this._validateSemanticProperties(t),this._validateBestPractices(t);var n=this._extractEntityInfo(t),i=0===this.errors.length;return this._buildResult(i,n,t)}},{key:"isValid",value:function(e){return this.validate(e).isValid}},{key:"getEntityInfo",value:function(e){return this.validate(e).entityInfo}},{key:"_validateRequiredFields",value:function(e){var t=this;["@context","entityType","entityCategory","name","description"].forEach((function(n){e[n]||t._addError(n,"Required field '".concat(n,"' is missing"),"structure","Add the ".concat(n," field to your schema"))}));["@id","@type","schemaVersion","url","shortDescription"].forEach((function(n){e[n]||t._addWarning(n,"Recommended field '".concat(n,"' is missing"),"best_practice","Consider adding ".concat(n," for better schema completeness"))}))}},{key:"_validateContext",value:function(e){e["@context"]&&e["@context"]!==o&&this._addError("@context","Invalid @context value","structure",'Use "'.concat(o,'" for AIML v2.0.0 schemas'))}},{key:"_validateVersioning",value:function(e){e.schemaVersion&&e.schemaVersion!==s&&this._addWarning("schemaVersion","Schema version ".concat(e.schemaVersion," might not be current"),"schema","Consider using version ".concat(s," for latest features"))}},{key:"_validateEntityStructure",value:function(e){if(e.entityCategory&&!c.includes(e.entityCategory)&&this._addError("entityCategory","Invalid entity category: ".concat(e.entityCategory),"schema","Use one of: ".concat(c.join(", "))),e.entityType&&e.entityCategory){var t=l[e.entityCategory]||[];t.length>0&&!t.includes(e.entityType)&&this._addError("entityType","Entity type '".concat(e.entityType,"' is not valid for category '").concat(e.entityCategory,"'"),"schema","Valid types for ".concat(e.entityCategory,": ").concat(t.join(", ")))}if(e.subcategory){var n=Object.keys(u);if(n.includes(e.subcategory)||this._addWarning("subcategory","Subcategory '".concat(e.subcategory,"' might not be standard"),"schema","Common subcategories: ".concat(n.join(", "))),e.entityCategory&&d[e.subcategory]){var i=d[e.subcategory];i.allowedCategories.includes(e.entityCategory)||this._addError("subcategory","Subcategory '".concat(e.subcategory,"' is not valid for category '").concat(e.entityCategory,"'"),"schema","Valid categories for ".concat(e.subcategory,": ").concat(i.allowedCategories.join(", ")))}if(e.entityType&&u[e.subcategory]){var r=u[e.subcategory];r.includes(e.entityType)||this._addError("entityType","Entity type '".concat(e.entityType,"' is not valid for subcategory '").concat(e.subcategory,"'"),"schema","Valid types for ".concat(e.subcategory,": ").concat(r.join(", ")))}}}},{key:"_validateNames",value:function(e){var t=this;e.name&&("object"===r(e.name)?(e.name.en||this._addWarning("name","English name (en) is recommended in multilingual names","best_practice",'Add an "en" field for better international compatibility'),Object.keys(e.name).forEach((function(e){/^[a-z]{2}(-[A-Z]{2})?$/.test(e)||t._addWarning("name","Language code '".concat(e,"' might not be valid ISO 639-1 format"),"best_practice",'Use ISO 639-1 language codes (e.g., "en", "es", "fr", "en-US")')}))):"string"==typeof e.name&&this._addSuggestion("name","Consider using multilingual name object for better i18n support","best_practice",'Convert string to object: {"en": "Your Name"}')),e.description&&"object"===r(e.description)&&(e.description.en||this._addWarning("description","English description (en) is recommended","best_practice"))}},{key:"_validateModules",value:function(e){var t=this;e.modules&&Object.keys(e.modules).forEach((function(n){g.includes(n)||t._addWarning("modules","Module '".concat(n,"' is not a standard AIML module"),"schema","Standard modules: ".concat(g.join(", ")));var i=e.modules[n];i&&"object"===r(i)&&(i.version||t._addWarning("modules.".concat(n),"Module should include version field","best_practice","Add version field for better compatibility tracking"),i.enabled||t._addSuggestion("modules.".concat(n),"Consider adding enabled field to module","best_practice",'Add "enabled": true/false for better module management'))}))}},{key:"_validateSemanticProperties",value:function(e){var t=this;if(e.semanticProperties){var n=e.semanticProperties;n.subjectiveQualities&&Object.values(n.subjectiveQualities).forEach((function(e){e&&"object"===r(e)&&e.rating&&(e.rating<0||e.rating>1)&&t._addError("semanticProperties","Semantic quality ratings should be between 0 and 1","semantic","Use decimal values between 0.0 and 1.0")})),n.intentAlignment&&Object.values(n.intentAlignment).forEach((function(e){e&&"object"===r(e)&&e.alignment&&(e.alignment<0||e.alignment>1)&&t._addError("semanticProperties","Intent alignment should be between 0 and 1","semantic")}))}else this._addSuggestion("semanticProperties","Consider adding semantic properties for better AI understanding","semantic","Add subjectiveQualities and intentAlignment for enhanced semantic analysis")}},{key:"_validateBestPractices",value:function(e){var t=this;if(e.url&&!/^https?:\/\/.+/.test(e.url)&&this._addWarning("url","URL should start with http:// or https://","best_practice"),e.description){var n="string"==typeof e.description?e.description:e.description.en;n&&n.length<50&&this._addSuggestion("description","Description is quite short, consider adding more detail","best_practice","Aim for at least 50-100 characters for better SEO and understanding")}e.appliedContexts&&e.appliedContexts.forEach((function(e){m.includes(e)||t._addWarning("appliedContexts","Context '".concat(e,"' is not a standard AIML context"),"schema","Available contexts: ".concat(m.join(", ")))}))}},{key:"_extractEntityInfo",value:function(e){if(!e.entityType||!e.entityCategory)return null;var t=e.modules?Object.keys(e.modules):[],n=e.appliedContexts||[];return{entityType:e.entityType,entityCategory:e.entityCategory,subcategory:e.subcategory,baseSchema:e.entityCategory,modules:t,contexts:n,hasSemanticProperties:!!e.semanticProperties,hasIntentContext:!!e.intentContext}}},{key:"_buildResult",value:function(e,t,n){var i=this._calculateScore(),r=this._calculateCompleteness(n),a=this._calculatePerformance(n);return{isValid:e,errors:this.errors,warnings:this.warnings,suggestions:this.suggestions,entityInfo:t,score:i,completeness:r,performance:a}}},{key:"_calculateScore",value:function(){var e=30*this.errors.length+10*this.warnings.length+5*this.suggestions.length;return Math.max(0,100-e)}},{key:"_calculateCompleteness",value:function(e){if(!e)return 0;var t=["@context","@id","@type","schemaVersion","entityType","entityCategory","subcategory","name","description","url","shortDescription","properties","modules","semanticProperties","intentContext","appliedContexts"],n=t.filter((function(t){return void 0!==e[t]}));return Math.round(n.length/t.length*100)}},{key:"_calculatePerformance",value:function(e){if(!e)return{schemaSize:0,complexity:"low",moduleCount:0};var t=JSON.stringify(e).length,n=e.modules?Object.keys(e.modules).length:0,i="low";return n>5||t>5e3?i="high":(n>2||t>2e3)&&(i="medium"),{schemaSize:t,complexity:i,moduleCount:n}}},{key:"_addError",value:function(e,t,n,i){this.errors.push({field:e,message:t,severity:"error",category:n,suggestion:i})}},{key:"_addWarning",value:function(e,t,n,i){this.warnings.push({field:e,message:t,severity:"warning",category:n,suggestion:i})}},{key:"_addSuggestion",value:function(e,t,n,i){this.suggestions.push({field:e,message:t,severity:"info",category:n,suggestion:i})}}])}();return f.validate=function(e){return new f(arguments.length>1&&void 0!==arguments[1]?arguments[1]:{}).validate(e)},f.createProduction=function(){return new f({debug:!1,strict:!0})},f.createDevelopment=function(){return new f({debug:!0,strict:!1})},f.getVersion=function(){return s},f.getEntityTypes=function(){var e=[];return Object.values(l).forEach((function(t){e.push.apply(e,n(t))})),e.sort()},f.getEntityCategories=function(){return c},f.getModules=function(){return g},f.getContexts=function(){return m},f.getSubcategories=function(){return Object.keys(u)},f.validateConfig=function(e){return!(!e||"object"!==r(e))},a(f)}));
//# sourceMappingURL=aiml-parser.min.js.map