@workglow/ai 0.2.0 → 0.2.1

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.
@@ -5,6 +5,7 @@
5
5
  */
6
6
  import { ITabularStorage } from "@workglow/storage";
7
7
  import { EventEmitter, EventParameters } from "@workglow/util";
8
+ import type { DataPortSchemaObject } from "@workglow/util/schema";
8
9
  import { ModelPrimaryKeyNames, ModelRecord, ModelRecordSchema } from "./ModelSchema";
9
10
  /**
10
11
  * Events that can be emitted by the ModelRepository
@@ -30,6 +31,19 @@ export declare class ModelRepository {
30
31
  constructor(modelTabularRepository: ITabularStorage<typeof ModelRecordSchema, typeof ModelPrimaryKeyNames, ModelRecord>);
31
32
  /** Event emitter for repository events */
32
33
  protected events: EventEmitter<ModelEventListeners>;
34
+ /** Cached compiled validation schema, lazily built on first validate call */
35
+ private compiledValidationSchema;
36
+ /**
37
+ * Returns the JSON schema used to validate model records before persistence.
38
+ * Subclasses can override this to accept additional properties (for example,
39
+ * owner/project IDs in repositories scoped to a user's workspace).
40
+ */
41
+ protected getValidationSchema(): DataPortSchemaObject;
42
+ /**
43
+ * Validates a model record against {@link getValidationSchema}.
44
+ * @throws if the record fails schema validation
45
+ */
46
+ protected validateModelRecord(model: ModelRecord): void;
33
47
  /**
34
48
  * Sets up the database for the repository.
35
49
  * Must be called before using any other methods.
@@ -61,14 +75,14 @@ export declare class ModelRepository {
61
75
  waitOn<Event extends ModelEvents>(name: Event): Promise<import("@workglow/util").EmittedReturnType<ModelEventListeners, Event>>;
62
76
  /**
63
77
  * Adds a new model to the repository.
64
- * Validates against ModelRecordSchema and rejects duplicates.
78
+ * Validates against the schema returned by {@link getValidationSchema} and rejects duplicates.
65
79
  * @param model - The model instance to add
66
80
  * @throws if the model fails schema validation or a model with the same model_id already exists
67
81
  */
68
82
  addModel(model: ModelRecord): Promise<ModelRecord>;
69
83
  /**
70
84
  * Updates an existing model in the repository.
71
- * Validates against ModelRecordSchema and requires the model to already exist.
85
+ * Validates against the schema returned by {@link getValidationSchema} and requires the model to already exist.
72
86
  * @param model - The model instance with updated fields
73
87
  * @throws if the model fails schema validation or the model_id does not exist
74
88
  */
@@ -1 +1 @@
1
- {"version":3,"file":"ModelRepository.d.ts","sourceRoot":"","sources":["../../src/model/ModelRepository.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAI/D,OAAO,EAAE,oBAAoB,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAsBrF;;GAEG;AAEH,MAAM,MAAM,mBAAmB,GAAG;IAChC,WAAW,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,CAAC;IAC1C,aAAa,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,CAAC;IAC5C,aAAa,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,CAAC;CAC7C,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG,MAAM,mBAAmB,CAAC;AAEpD,MAAM,MAAM,kBAAkB,CAAC,KAAK,SAAS,WAAW,IAAI,mBAAmB,CAAC,KAAK,CAAC,CAAC;AAEvF,MAAM,MAAM,oBAAoB,CAAC,KAAK,SAAS,WAAW,IAAI,eAAe,CAC3E,mBAAmB,EACnB,KAAK,CACN,CAAC;AAEF;;;;GAIG;AACH,qBAAa,eAAe;IAC1B;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,sBAAsB,EAAE,eAAe,CACxD,OAAO,iBAAiB,EACxB,OAAO,oBAAoB,CAC5B,CAAC;IACF,YACE,sBAAsB,EAAE,eAAe,CACrC,OAAO,iBAAiB,EACxB,OAAO,oBAAoB,EAC3B,WAAW,CACZ,EAGF;IAED,0CAA0C;IAC1C,SAAS,CAAC,MAAM,oCAA2C;IAE3D;;;OAGG;IACG,aAAa,IAA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}
package/dist/node.js CHANGED
@@ -485,31 +485,29 @@ var ModelRecordSchema = {
485
485
  var ModelPrimaryKeyNames = ["model_id"];
486
486
 
487
487
  // src/model/ModelRepository.ts
488
- var compiledModelRecordSchema;
489
- function getModelRecordSchemaNode() {
490
- if (!compiledModelRecordSchema) {
491
- compiledModelRecordSchema = compileSchema(ModelRecordSchema);
492
- }
493
- return compiledModelRecordSchema;
494
- }
495
- function validateModelRecord(model) {
496
- const schemaNode = getModelRecordSchemaNode();
497
- const result = schemaNode.validate(model);
498
- if (!result.valid) {
499
- const errorMessages = result.errors.map((e) => {
500
- const path = e.data?.pointer || "";
501
- return `${e.message}${path ? ` (${path})` : ""}`;
502
- });
503
- throw new Error(`Invalid model record: ${errorMessages.join(", ")}`);
504
- }
505
- }
506
-
507
488
  class ModelRepository {
508
489
  modelTabularRepository;
509
490
  constructor(modelTabularRepository) {
510
491
  this.modelTabularRepository = modelTabularRepository;
511
492
  }
512
493
  events = new EventEmitter;
494
+ compiledValidationSchema;
495
+ getValidationSchema() {
496
+ return ModelRecordSchema;
497
+ }
498
+ validateModelRecord(model) {
499
+ if (!this.compiledValidationSchema) {
500
+ this.compiledValidationSchema = compileSchema(this.getValidationSchema());
501
+ }
502
+ const result = this.compiledValidationSchema.validate(model);
503
+ if (!result.valid) {
504
+ const errorMessages = result.errors.map((e) => {
505
+ const path = e.data?.pointer || "";
506
+ return `${e.message}${path ? ` (${path})` : ""}`;
507
+ });
508
+ throw new Error(`Invalid model record: ${errorMessages.join(", ")}`);
509
+ }
510
+ }
513
511
  async setupDatabase() {
514
512
  await this.modelTabularRepository.setupDatabase?.();
515
513
  }
@@ -526,7 +524,7 @@ class ModelRepository {
526
524
  return this.events.waitOn(name);
527
525
  }
528
526
  async addModel(model) {
529
- validateModelRecord(model);
527
+ this.validateModelRecord(model);
530
528
  const existing = await this.modelTabularRepository.get({ model_id: model.model_id });
531
529
  if (existing) {
532
530
  throw new Error(`Model with id "${model.model_id}" already exists`);
@@ -536,7 +534,7 @@ class ModelRepository {
536
534
  return model;
537
535
  }
538
536
  async updateModel(model) {
539
- validateModelRecord(model);
537
+ this.validateModelRecord(model);
540
538
  const existing = await this.modelTabularRepository.get({ model_id: model.model_id });
541
539
  if (!existing) {
542
540
  throw new Error(`Model with id "${model.model_id}" not found`);
@@ -7579,4 +7577,4 @@ export {
7579
7577
  AgentInputSchema
7580
7578
  };
7581
7579
 
7582
- //# debugId=8A77F7A6C0C5C65D64756E2164756E21
7580
+ //# debugId=B28911D966FA5D9064756E2164756E21