@cadenza.io/core 3.19.4 → 3.20.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/dist/index.d.mts CHANGED
@@ -342,7 +342,8 @@ type SchemaDefinition = {
342
342
  description?: string;
343
343
  strict?: boolean;
344
344
  };
345
- type Schema = SchemaDefinition | SchemaDefinition[];
345
+ type SchemaMap = Record<string, SchemaDefinition>;
346
+ type Schema = SchemaDefinition | SchemaMap;
346
347
 
347
348
  interface Intent {
348
349
  name: string;
@@ -934,6 +935,10 @@ declare class Task extends SignalEmitter implements Graph {
934
935
  * @return {void} This method does not return any value.
935
936
  */
936
937
  emitMetricsWithMetadata(signal: string, ctx?: AnyObject): void;
938
+ private isSchemaDefinition;
939
+ private isDefaultAnyObjectSchema;
940
+ private mergeSchemaVariant;
941
+ private validateValueAgainstSchema;
937
942
  /**
938
943
  * Validates a data object against a specified schema definition and returns validation results.
939
944
  *
package/dist/index.d.ts CHANGED
@@ -342,7 +342,8 @@ type SchemaDefinition = {
342
342
  description?: string;
343
343
  strict?: boolean;
344
344
  };
345
- type Schema = SchemaDefinition | SchemaDefinition[];
345
+ type SchemaMap = Record<string, SchemaDefinition>;
346
+ type Schema = SchemaDefinition | SchemaMap;
346
347
 
347
348
  interface Intent {
348
349
  name: string;
@@ -934,6 +935,10 @@ declare class Task extends SignalEmitter implements Graph {
934
935
  * @return {void} This method does not return any value.
935
936
  */
936
937
  emitMetricsWithMetadata(signal: string, ctx?: AnyObject): void;
938
+ private isSchemaDefinition;
939
+ private isDefaultAnyObjectSchema;
940
+ private mergeSchemaVariant;
941
+ private validateValueAgainstSchema;
937
942
  /**
938
943
  * Validates a data object against a specified schema definition and returns validation results.
939
944
  *
package/dist/index.js CHANGED
@@ -3753,8 +3753,8 @@ var Task = class _Task extends SignalEmitter {
3753
3753
  isSubMeta: this.isSubMeta,
3754
3754
  validateInputContext: this.validateInputContext,
3755
3755
  validateOutputContext: this.validateOutputContext,
3756
- // inputContextSchemaId: this.inputContextSchema,
3757
- // outputContextSchemaId: this.outputContextSchema,
3756
+ inputContextSchema: this.inputContextSchema,
3757
+ outputContextSchema: this.outputContextSchema,
3758
3758
  signals: {
3759
3759
  emits: Array.from(this.emitsSignals),
3760
3760
  signalsToEmitAfter: Array.from(this.signalsToEmitAfter),
@@ -3883,6 +3883,51 @@ var Task = class _Task extends SignalEmitter {
3883
3883
  }
3884
3884
  this.emitMetrics(signal, data);
3885
3885
  }
3886
+ isSchemaDefinition(schema) {
3887
+ return !!schema && typeof schema === "object" && "type" in schema;
3888
+ }
3889
+ isDefaultAnyObjectSchema(schema) {
3890
+ return schema.type === "object" && !schema.strict && !schema.properties && !schema.required?.length && !schema.items && !schema.constraints && !schema.description;
3891
+ }
3892
+ mergeSchemaVariant(currentSchema, schemaKey, variant) {
3893
+ const schemaMap = {};
3894
+ if (this.isSchemaDefinition(currentSchema)) {
3895
+ if (!this.isDefaultAnyObjectSchema(currentSchema)) {
3896
+ schemaMap.default = currentSchema;
3897
+ }
3898
+ } else if (currentSchema && typeof currentSchema === "object") {
3899
+ Object.assign(schemaMap, currentSchema);
3900
+ }
3901
+ schemaMap[schemaKey] = variant;
3902
+ return schemaMap;
3903
+ }
3904
+ validateValueAgainstSchema(schema, key, value, path = "context") {
3905
+ if (this.isSchemaDefinition(schema)) {
3906
+ return this.validateProp(schema, key, value, path);
3907
+ }
3908
+ const errors = {};
3909
+ let checkedSchemas = 0;
3910
+ for (const candidate of Object.values(schema)) {
3911
+ if (!candidate || typeof candidate !== "object") {
3912
+ continue;
3913
+ }
3914
+ checkedSchemas += 1;
3915
+ const candidateErrors = this.validateValueAgainstSchema(
3916
+ candidate,
3917
+ key,
3918
+ value,
3919
+ path
3920
+ );
3921
+ if (Object.keys(candidateErrors).length === 0) {
3922
+ return {};
3923
+ }
3924
+ Object.assign(errors, candidateErrors);
3925
+ }
3926
+ if (checkedSchemas === 0) {
3927
+ return {};
3928
+ }
3929
+ return errors;
3930
+ }
3886
3931
  /**
3887
3932
  * Validates a data object against a specified schema definition and returns validation results.
3888
3933
  *
@@ -3894,10 +3939,16 @@ var Task = class _Task extends SignalEmitter {
3894
3939
  */
3895
3940
  validateSchema(data, schema, path = "context") {
3896
3941
  const errors = {};
3897
- if (!schema || typeof schema !== "object" && !Array.isArray(schema))
3942
+ if (!schema || typeof schema !== "object") {
3898
3943
  return { valid: true, errors };
3899
- if (Array.isArray(schema)) {
3900
- for (const s of schema) {
3944
+ }
3945
+ if (!this.isSchemaDefinition(schema)) {
3946
+ let checkedSchemas = 0;
3947
+ for (const s of Object.values(schema)) {
3948
+ if (!s || typeof s !== "object") {
3949
+ continue;
3950
+ }
3951
+ checkedSchemas += 1;
3901
3952
  const subValidation = this.validateSchema(data, s, path);
3902
3953
  if (!subValidation.valid) {
3903
3954
  Object.assign(errors, subValidation.errors);
@@ -3906,6 +3957,13 @@ var Task = class _Task extends SignalEmitter {
3906
3957
  return { valid: true, errors: {} };
3907
3958
  }
3908
3959
  }
3960
+ if (checkedSchemas === 0) {
3961
+ return { valid: true, errors };
3962
+ }
3963
+ return { valid: false, errors };
3964
+ }
3965
+ if (schema.type === "object" && (typeof data !== "object" || data === null || Array.isArray(data))) {
3966
+ errors[path] = `Expected 'object' for '${path}', got '${typeof data}'`;
3909
3967
  return { valid: false, errors };
3910
3968
  }
3911
3969
  const required = schema.required || [];
@@ -3918,13 +3976,10 @@ var Task = class _Task extends SignalEmitter {
3918
3976
  for (const [key, value] of Object.entries(data)) {
3919
3977
  if (key in properties) {
3920
3978
  const prop = properties[key];
3921
- if (Array.isArray(prop)) {
3922
- for (const p of prop) {
3923
- Object.assign(errors, this.validateProp(p, key, value, path));
3924
- }
3925
- } else {
3926
- Object.assign(errors, this.validateProp(prop, key, value, path));
3927
- }
3979
+ Object.assign(
3980
+ errors,
3981
+ this.validateValueAgainstSchema(prop, key, value, path)
3982
+ );
3928
3983
  } else if (schema.strict) {
3929
3984
  errors[`${path}.${key}`] = `Key '${key}' is not allowed`;
3930
3985
  }
@@ -3955,13 +4010,13 @@ var Task = class _Task extends SignalEmitter {
3955
4010
  errors[`${path}.${key}`] = `Expected 'object' for '${key}', got '${typeof value}'`;
3956
4011
  } else if (propType === "array" && prop.items) {
3957
4012
  value.forEach((item, index) => {
3958
- const subValidation = this.validateSchema(
4013
+ const validation = this.validateSchema(
3959
4014
  item,
3960
4015
  prop.items,
3961
4016
  `${path}.${key}[${index}]`
3962
4017
  );
3963
- if (!subValidation.valid) {
3964
- Object.assign(errors, subValidation.errors);
4018
+ if (!validation.valid) {
4019
+ Object.assign(errors, validation.errors);
3965
4020
  }
3966
4021
  });
3967
4022
  } else if (propType === "object" && !Array.isArray(value) && value !== null) {
@@ -4444,10 +4499,18 @@ var Task = class _Task extends SignalEmitter {
4444
4499
  Cadenza.inquiryBroker.observe(intentName, this);
4445
4500
  const intent = Cadenza.inquiryBroker.intents.get(intentName);
4446
4501
  if (intent?.input) {
4447
- this.inputContextSchema = intent.input;
4502
+ this.inputContextSchema = this.mergeSchemaVariant(
4503
+ this.inputContextSchema,
4504
+ intentName,
4505
+ intent.input
4506
+ );
4448
4507
  }
4449
4508
  if (intent?.output) {
4450
- this.outputContextSchema = intent.output;
4509
+ this.outputContextSchema = this.mergeSchemaVariant(
4510
+ this.outputContextSchema,
4511
+ intentName,
4512
+ intent.output
4513
+ );
4451
4514
  }
4452
4515
  }
4453
4516
  return this;
@@ -5832,12 +5895,14 @@ var InquiryBroker = class _InquiryBroker extends SignalEmitter {
5832
5895
  tasks: /* @__PURE__ */ new Set(),
5833
5896
  registered: false
5834
5897
  });
5835
- this.addIntent({
5836
- name: inquiry,
5837
- description: "",
5838
- input: { type: "object" },
5839
- output: { type: "object" }
5840
- });
5898
+ if (!this.intents.has(inquiry)) {
5899
+ this.addIntent({
5900
+ name: inquiry,
5901
+ description: "",
5902
+ input: { type: "object" },
5903
+ output: { type: "object" }
5904
+ });
5905
+ }
5841
5906
  }
5842
5907
  }
5843
5908
  addIntent(intent) {