@ai.ntellect/core 0.8.0 → 0.8.2

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.
@@ -51,11 +51,11 @@ describe("GraphFlow", function () {
51
51
 
52
52
  beforeEach(() => {
53
53
  eventEmitter = new EventEmitter();
54
- graph = new GraphFlow("TestGraph", {
54
+ graph = new GraphFlow({
55
55
  name: "TestGraph",
56
+ schema: TestSchema,
56
57
  nodes: [],
57
58
  context: { value: 0 },
58
- schema: TestSchema,
59
59
  eventEmitter: eventEmitter,
60
60
  });
61
61
  });
@@ -251,8 +251,8 @@ describe("GraphFlow", function () {
251
251
  },
252
252
  };
253
253
 
254
- const graph = new GraphFlow("test", {
255
- name: "test",
254
+ const graph = new GraphFlow({
255
+ name: "retryGraph",
256
256
  schema: TestSchema,
257
257
  context: { value: 0 },
258
258
  nodes: [retryNode],
@@ -292,10 +292,10 @@ describe("GraphFlow", function () {
292
292
 
293
293
  const newDefinition: GraphConfig<TestSchema> = {
294
294
  name: "TestGraph",
295
- entryNode: "A",
295
+ schema: TestSchema,
296
296
  nodes: [nodeA, nodeB],
297
297
  context: { value: 0 },
298
- schema: TestSchema,
298
+ entryNode: "A",
299
299
  };
300
300
 
301
301
  graph.load(newDefinition);
@@ -318,7 +318,7 @@ describe("GraphFlow", function () {
318
318
  },
319
319
  };
320
320
 
321
- const graph = new GraphFlow("test", {
321
+ const graph = new GraphFlow({
322
322
  name: "test",
323
323
  schema: TestSchema,
324
324
  context: { value: 0 },
@@ -336,8 +336,9 @@ describe("GraphFlow", function () {
336
336
  * Tests successful input/output validation flow
337
337
  */
338
338
  it("should successfully validate both params and outputs", async function () {
339
- const graph = new GraphFlow("test", {
339
+ const graph = new GraphFlow({
340
340
  name: "test",
341
+ schema: TestSchema,
341
342
  nodes: [
342
343
  {
343
344
  name: "validatedNode",
@@ -353,7 +354,6 @@ describe("GraphFlow", function () {
353
354
  },
354
355
  },
355
356
  ],
356
- schema: TestSchema,
357
357
  context: { value: 0, counter: 0, message: "" },
358
358
  });
359
359
 
@@ -371,8 +371,9 @@ describe("GraphFlow", function () {
371
371
  * Tests handling of missing required params
372
372
  */
373
373
  it("should throw error when required params are missing", async function () {
374
- const graph = new GraphFlow("test", {
374
+ const graph = new GraphFlow({
375
375
  name: "test",
376
+ schema: TestSchema,
376
377
  nodes: [
377
378
  {
378
379
  name: "requiredInputNode",
@@ -387,7 +388,6 @@ describe("GraphFlow", function () {
387
388
  },
388
389
  },
389
390
  ],
390
- schema: TestSchema,
391
391
  context: { value: 0, counter: 0, message: "" },
392
392
  });
393
393
 
@@ -483,11 +483,11 @@ describe("GraphFlow", function () {
483
483
  */
484
484
  it("should handle parallel workflows using GraphController", async function () {
485
485
  // Graph 1
486
- const graph1 = new GraphFlow("Graph1", {
486
+ const graph1 = new GraphFlow({
487
487
  name: "Graph1",
488
- nodes: [],
489
- context: { value: 0 },
490
488
  schema: TestSchema,
489
+ context: { value: 0 },
490
+ nodes: [],
491
491
  });
492
492
 
493
493
  const processNode1: GraphNodeConfig<TestSchema> = {
@@ -506,11 +506,11 @@ describe("GraphFlow", function () {
506
506
  };
507
507
 
508
508
  // Graph 2
509
- const graph2 = new GraphFlow("Graph2", {
509
+ const graph2 = new GraphFlow({
510
510
  name: "Graph2",
511
- nodes: [],
512
- context: { value: 0 },
513
511
  schema: TestSchema,
512
+ context: { value: 0 },
513
+ nodes: [],
514
514
  });
515
515
 
516
516
  const processNode2: GraphNodeConfig<TestSchema> = {
@@ -549,11 +549,11 @@ describe("GraphFlow", function () {
549
549
  */
550
550
  it("should handle sequential workflows using GraphController", async function () {
551
551
  // Graph 1
552
- const graph1 = new GraphFlow("Graph1", {
552
+ const graph1 = new GraphFlow({
553
553
  name: "Graph1",
554
- nodes: [],
555
- context: { value: 1 },
556
554
  schema: TestSchema,
555
+ context: { value: 1 },
556
+ nodes: [],
557
557
  });
558
558
 
559
559
  const startNode1: GraphNodeConfig<TestSchema> = {
@@ -564,11 +564,11 @@ describe("GraphFlow", function () {
564
564
  };
565
565
 
566
566
  // Graph 2
567
- const graph2 = new GraphFlow("Graph2", {
567
+ const graph2 = new GraphFlow({
568
568
  name: "Graph2",
569
- nodes: [],
570
- context: { value: 3 },
571
569
  schema: TestSchema,
570
+ context: { value: 3 },
571
+ nodes: [],
572
572
  });
573
573
 
574
574
  const startNode2: GraphNodeConfig<TestSchema> = {
@@ -631,8 +631,9 @@ describe("GraphFlow", function () {
631
631
 
632
632
  // Test de validation des paramètres
633
633
  it("should successfully validate params", async () => {
634
- const graph = new GraphFlow("test", {
634
+ const graph = new GraphFlow({
635
635
  name: "test",
636
+ schema: TestSchema,
636
637
  nodes: [
637
638
  {
638
639
  name: "validationNode",
@@ -647,7 +648,6 @@ describe("GraphFlow", function () {
647
648
  },
648
649
  },
649
650
  ],
650
- schema: TestSchema,
651
651
  context: { value: 0, counter: 0, message: "" },
652
652
  });
653
653
 
@@ -659,8 +659,9 @@ describe("GraphFlow", function () {
659
659
 
660
660
  // Test des paramètres requis
661
661
  it("should throw error when required params are missing", async () => {
662
- const graph = new GraphFlow("test", {
662
+ const graph = new GraphFlow({
663
663
  name: "test",
664
+ schema: TestSchema,
664
665
  nodes: [
665
666
  {
666
667
  name: "requiredInputNode",
@@ -675,7 +676,6 @@ describe("GraphFlow", function () {
675
676
  },
676
677
  },
677
678
  ],
678
- schema: TestSchema,
679
679
  context: { value: 0, counter: 0, message: "" },
680
680
  });
681
681
 
@@ -33,7 +33,7 @@ describe("GraphObserver", () => {
33
33
  let observer: GraphObserver<typeof TestSchema>;
34
34
 
35
35
  beforeEach(() => {
36
- graph = new GraphFlow("TestGraph", {
36
+ graph = new GraphFlow({
37
37
  name: "TestGraph",
38
38
  nodes: [],
39
39
  context: { value: 0 },
@@ -50,7 +50,8 @@ describe("GraphObserver", () => {
50
50
  graph,
51
51
  eventSubject,
52
52
  stateSubject,
53
- destroySubject
53
+ destroySubject,
54
+ graph["eventManager"]
54
55
  );
55
56
  });
56
57
 
@@ -319,33 +320,27 @@ describe("GraphObserver", () => {
319
320
  * - Events are captured in correct order
320
321
  */
321
322
  it("should wait for correlated events", async () => {
322
- // Create test events
323
- const eventA = {
323
+ const promise = observer.waitForCorrelatedEvents(
324
+ ["eventA", "eventB"],
325
+ 1000,
326
+ (events) => events.every((e) => e.payload?.status === "success")
327
+ );
328
+
329
+ // Emit events immediately
330
+ eventSubject.next({
324
331
  type: "eventA",
325
- payload: { eventPayload: { status: "success" } },
332
+ payload: { status: "success" },
326
333
  timestamp: Date.now(),
327
- } as GraphEvent<typeof TestSchema>;
334
+ });
328
335
 
329
- const eventB = {
336
+ eventSubject.next({
330
337
  type: "eventB",
331
- payload: { eventPayload: { status: "success" } },
338
+ payload: { status: "success" },
332
339
  timestamp: Date.now(),
333
- } as GraphEvent<typeof TestSchema>;
334
-
335
- // Emit events after a short delay
336
- setTimeout(() => {
337
- eventSubject.next(eventA);
338
- eventSubject.next(eventB);
339
- }, 100);
340
-
341
- const events = await observer.waitForCorrelatedEvents(
342
- ["eventA", "eventB"],
343
- 2000,
344
- (events) =>
345
- events.every((e) => e.payload.eventPayload?.status === "success")
346
- );
340
+ });
347
341
 
348
- expect(events.length).to.equal(2);
342
+ const events = await promise;
343
+ expect(events).to.have.length(2);
349
344
  expect(events[0].type).to.equal("eventA");
350
345
  expect(events[1].type).to.equal("eventB");
351
346
  });
package/types/index.ts CHANGED
@@ -77,13 +77,45 @@ export type GraphContext<T extends ZodSchema> = {
77
77
  };
78
78
 
79
79
  /**
80
- * Configuration for event handling in a node
80
+ * Configuration for event handling strategies in nodes
81
+ * @typedef {Object} EventStrategy
82
+ * @property {"single" | "all" | "correlate"} type - The type of event handling strategy
83
+ * - single: Waits for any single event from the specified events
84
+ * - all: Waits for all specified events to occur
85
+ * - correlate: Uses a correlation function to match related events
86
+ * @property {(events: any[]) => boolean} [correlation] - Optional correlation function for "correlate" strategy
81
87
  */
82
88
  export type EventStrategy = {
83
89
  type: "single" | "all" | "correlate";
84
90
  correlation?: (events: any[]) => boolean;
85
91
  };
86
92
 
93
+ /**
94
+ * Configuration for event handling in nodes
95
+ * @typedef {Object} EventConfig
96
+ * @property {string[]} events - Array of event names to wait for
97
+ * @property {number} [timeout] - Optional timeout in milliseconds
98
+ * @property {EventStrategy} strategy - Strategy for handling multiple events
99
+ * @property {(events: any[]) => Promise<void>} [onSuccess] - Optional callback when events are successfully received
100
+ * @property {() => Promise<void>} [onTimeout] - Optional callback when event waiting times out
101
+ * @example
102
+ * ```typescript
103
+ * const eventConfig: EventConfig = {
104
+ * events: ["payment.received", "order.validated"],
105
+ * timeout: 5000,
106
+ * strategy: {
107
+ * type: "correlate",
108
+ * correlation: (events) => events.every(e => e.transactionId === events[0].transactionId)
109
+ * },
110
+ * onSuccess: async (events) => {
111
+ * console.log("Correlated events received:", events);
112
+ * },
113
+ * onTimeout: async () => {
114
+ * console.log("Event waiting timed out");
115
+ * }
116
+ * };
117
+ * ```
118
+ */
87
119
  export type EventConfig = {
88
120
  events: string[];
89
121
  timeout?: number;
@@ -92,12 +124,62 @@ export type EventConfig = {
92
124
  onTimeout?: () => Promise<void>;
93
125
  };
94
126
 
127
+ /**
128
+ * Represents an event in the graph system
129
+ * @template T - Schema type for context validation
130
+ * @property {string} type - The type/name of the event
131
+ * @property {any} [payload] - Optional payload data
132
+ * @property {number} timestamp - Unix timestamp of when the event occurred
133
+ * @example
134
+ * ```typescript
135
+ * const event: GraphEvent<MySchema> = {
136
+ * type: "payment.received",
137
+ * payload: {
138
+ * transactionId: "tx123",
139
+ * amount: 100,
140
+ * currency: "USD"
141
+ * },
142
+ * timestamp: Date.now()
143
+ * };
144
+ * ```
145
+ */
146
+ export type GraphEvent<T extends ZodSchema> = {
147
+ type: string;
148
+ payload?: any;
149
+ timestamp: number;
150
+ };
151
+
152
+ /**
153
+ * Configuration for waiting on multiple events
154
+ * @template T - Schema type for context validation
155
+ * @property {string[]} events - Array of event names to wait for
156
+ * @property {number} [timeout] - Optional timeout in milliseconds
157
+ * @property {"all" | "any" | "race"} strategy - Strategy for handling multiple events
158
+ * @property {(context: GraphContext<T>) => Promise<void>} [onSuccess] - Optional success callback
159
+ * @example
160
+ * ```typescript
161
+ * const config: WaitForEvents<MySchema> = {
162
+ * events: ["event1", "event2"],
163
+ * timeout: 5000,
164
+ * strategy: "all",
165
+ * onSuccess: async (context) => {
166
+ * console.log("All events received");
167
+ * }
168
+ * };
169
+ * ```
170
+ */
171
+ export type WaitForEvents<T extends ZodSchema> = {
172
+ events: string[];
173
+ timeout?: number;
174
+ strategy: "all" | "any" | "race";
175
+ onSuccess?: <T extends ZodSchema>(context: GraphContext<T>) => Promise<void>;
176
+ };
177
+
95
178
  /**
96
179
  * Interface representing a node in the graph
97
180
  * @interface
98
181
  * @template T - Schema type
99
- * @template I - Input schema type
100
- * @template O - Output schema type
182
+ * @template P - Parameters type
101
183
  */
102
184
  export interface GraphNodeConfig<T extends ZodSchema, P = any> {
103
185
  /** Name of the node */
@@ -112,10 +194,9 @@ export interface GraphNodeConfig<T extends ZodSchema, P = any> {
112
194
  params?: P,
113
195
  tools?: { eventEmitter: IEventEmitter }
114
196
  ) => Promise<void>;
115
- /** Optional condition for node start execution */
197
+ /** Optional condition for node execution */
116
198
  condition?: (context: GraphContext<T>, params?: P) => boolean;
117
-
118
- /** Array of next node names or objects with conditions for the next node */
199
+ /** Array of next node names or objects with conditions */
119
200
  next?:
120
201
  | Array<
121
202
  | string
@@ -129,13 +210,9 @@ export interface GraphNodeConfig<T extends ZodSchema, P = any> {
129
210
  when?: EventConfig;
130
211
  /** Retry configuration */
131
212
  retry?: {
132
- /** Maximum number of retry attempts */
133
213
  maxAttempts: number;
134
- /** Delay between retries in milliseconds */
135
214
  delay: number;
136
- /** Error handler function */
137
215
  onRetryFailed?: (error: Error, context: GraphContext<T>) => Promise<void>;
138
- /** Continue execution on failed retry */
139
216
  continueOnFailed?: boolean;
140
217
  };
141
218
  /** Error handler function */
@@ -151,12 +228,12 @@ export interface GraphNodeConfig<T extends ZodSchema, P = any> {
151
228
  export type GraphConfig<T extends ZodSchema> = {
152
229
  /** Name of the graph */
153
230
  name: string;
154
- /** Array of nodes in the graph */
155
- nodes: GraphNodeConfig<T, any>[];
156
- /** Initial context */
157
- context: SchemaType<T>;
158
231
  /** Schema for validation */
159
232
  schema: T;
233
+ /** Initial context */
234
+ context: SchemaType<T>;
235
+ /** Array of nodes in the graph */
236
+ nodes: GraphNodeConfig<T, any>[];
160
237
  /** Global error handler */
161
238
  onError?: (error: Error, context: GraphContext<T>) => void;
162
239
  /** Entry node name */
@@ -205,19 +282,6 @@ export type MeilisearchSettings = {
205
282
  sortableAttributes?: string[];
206
283
  };
207
284
 
208
- export type GraphEvent<T extends ZodSchema> = {
209
- type: string;
210
- payload?: any;
211
- timestamp: number;
212
- };
213
-
214
- export type WaitForEvents<T extends ZodSchema> = {
215
- events: string[];
216
- timeout?: number;
217
- strategy: "all" | "any" | "race";
218
- onSuccess?: <T extends ZodSchema>(context: GraphContext<T>) => Promise<void>;
219
- };
220
-
221
285
  /**
222
286
  * Configuration interface for NLP Engine
223
287
  * @interface NLPConfig